installpanda.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #!/usr/bin/env python
  2. ########################################################################
  3. #
  4. # To install panda using this script, type 'installpanda.py'.
  5. # To specify an alternate location than the filesystem root /,
  6. # either pass it as only argument or set the DESTDIR environment
  7. # variable. This script only functions on Linux, for now.
  8. #
  9. ########################################################################
  10. import os
  11. import sys
  12. from optparse import OptionParser
  13. from makepandacore import *
  14. from locations import get_python_lib
  15. MIME_INFO = (
  16. # ext, mime, desc, app, magic
  17. ("egg", "model/x-egg", "EGG model file", "pview", None),
  18. ("bam", "model/x-bam", "Panda3D binary model file", "pview", None),
  19. ("egg.pz", "model/x-compressed-egg", "Compressed EGG model file", "pview", None),
  20. ("bam.pz", "model/x-compressed-bam", "Compressed Panda3D binary model file", "pview", None),
  21. ("pstats", "application/vnd.panda3d.pstats", "PStats session file", "pstats", b"pstat\0\n\r"),
  22. )
  23. APP_INFO = (
  24. ("pview", "Panda3D Model Viewer", ("egg", "bam", "egg.pz", "bam.pz"), True),
  25. ("pstats", "Panda3D Profiling Tool", ("pstats",), False),
  26. )
  27. EXCLUDE_BINARIES = ["deploy-stub", "deploy-stubw", "run_tests"]
  28. def WriteApplicationsFile(fname, appinfo, mimeinfo, bindir):
  29. fhandle = open(fname, "w")
  30. for app, desc, exts, multiple in appinfo:
  31. if not os.path.isfile(os.path.join(bindir, app)):
  32. continue
  33. fhandle.write("%s\n" % (app))
  34. fhandle.write("\tcommand=%s\n" % (app))
  35. fhandle.write("\tname=%s\n" % (desc))
  36. fhandle.write("\tcan_open_multiple_files=%s\n" % ('true' if multiple else 'false'))
  37. fhandle.write("\tstartup_notify=true\n")
  38. fhandle.write("\texpects_uris=false\n")
  39. fhandle.write("\trequires_terminal=false\n")
  40. fhandle.write("\tmime_types=")
  41. first = True
  42. for ext, mime, desc2, app2, magic in mimeinfo:
  43. if app == app2 and ext in exts:
  44. if first:
  45. fhandle.write(mime)
  46. first = False
  47. else:
  48. fhandle.write("," + mime)
  49. fhandle.write("\n\n")
  50. fhandle.close()
  51. def WriteMimeXMLFile(fname, info, bindir):
  52. fhandle = open(fname, "w")
  53. fhandle.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  54. fhandle.write("<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n")
  55. for ext, mime, desc, app, magic in info:
  56. if not os.path.isfile(os.path.join(bindir, app)):
  57. continue
  58. fhandle.write("\t<mime-type type=\"%s\">\n" % (mime))
  59. fhandle.write("\t\t<comment xml:lang=\"en\">%s</comment>\n" % (desc))
  60. if app == "pstats":
  61. fhandle.write("\t\t<generic-icon name=\"x-office-spreadsheet\"/>\n")
  62. if magic:
  63. magic = magic.decode('latin-1').encode('unicode-escape').decode('latin-1')
  64. fhandle.write("\t\t<magic>\n\t\t\t<match type=\"string\" offset=\"0\" value=\"%s\"/>\n\t\t</magic>\n" % (magic))
  65. fhandle.write("\t\t<glob pattern=\"*.%s\"/>\n" % (ext))
  66. fhandle.write("\t</mime-type>\n")
  67. fhandle.write("</mime-info>\n")
  68. fhandle.close()
  69. def WriteMimeFile(fname, info, bindir):
  70. fhandle = open(fname, "w")
  71. for ext, mime, desc, app, magic in info:
  72. if not os.path.isfile(os.path.join(bindir, app)):
  73. continue
  74. fhandle.write("%s\n" % (mime))
  75. if "." in ext:
  76. fhandle.write("\tregex,2: \\.%s$\n" % (ext.replace(".", "\\.")))
  77. fhandle.write("\text: %s\n" % (ext))
  78. fhandle.write("\n")
  79. fhandle.close()
  80. def WriteKeysFile(fname, info, bindir):
  81. fhandle = open(fname, "w")
  82. for ext, mime, desc, app, magic in info:
  83. if not os.path.isfile(os.path.join(bindir, app)):
  84. continue
  85. fhandle.write("%s\n" % (mime))
  86. fhandle.write("\tdescription=%s\n" % (desc))
  87. fhandle.write("\tdefault_action_type=application\n")
  88. fhandle.write("\tshort_list_application_ids_for_novice_user_level=%s\n" % (app))
  89. fhandle.write("\tshort_list_application_ids_for_intermediate_user_level=%s\n" % (app))
  90. fhandle.write("\tshort_list_application_ids_for_advanced_user_level=%s\n" % (app))
  91. fhandle.write("\topen=%s %%f\n" % (app))
  92. fhandle.write("\tview=%s %%f\n" % (app))
  93. fhandle.write("\n")
  94. fhandle.close()
  95. def GetDebLibDir():
  96. """ Returns the lib dir according to the debian system. """
  97. # We're on Debian or Ubuntu, which use multiarch directories.
  98. # Call dpkg-architecture to get the multiarch libdir.
  99. handle = os.popen("dpkg-architecture -qDEB_HOST_MULTIARCH")
  100. multiarch = handle.read().strip()
  101. if handle.close():
  102. # It failed. Old Debian/Ubuntu version?
  103. pass
  104. elif len(multiarch) > 0:
  105. return "lib/" + multiarch
  106. return "lib"
  107. def GetRPMLibDir():
  108. """ Returns the lib dir according to the rpm system. """
  109. handle = os.popen("rpm -E '%_lib'")
  110. result = handle.read().strip()
  111. handle.close()
  112. if len(result) > 0:
  113. assert result == "lib64" or result == "lib"
  114. return result
  115. else:
  116. return "lib"
  117. def GetLibDir():
  118. """Returns the directory to install architecture-dependent
  119. libraries in, relative to the prefix directory. This may be
  120. something like "lib" or "lib64" or in some cases, something
  121. similar to "lib/x86_64-linux-gnu"."""
  122. if sys.platform in ("darwin", "win32", "cygwin"):
  123. return "lib"
  124. # This one's a bit tricky. Some systems require us to install
  125. # 64-bits libraries into /usr/lib64, some into /usr/lib.
  126. # Debian forbids installing to lib64 nowadays, and the only distros
  127. # I know of that use lib64 are all RPM-based. So, the 'solution'
  128. # seems to be to use the rpm command to give us the libdir for now,
  129. # unless we know we're on debian, since rpm may be installed on
  130. # Debian and will give the wrong result. Ugh.
  131. if os.environ.get("DEB_HOST_MULTIARCH"):
  132. # We're building inside the Debian build environment.
  133. return "lib/" + os.environ["DEB_HOST_MULTIARCH"]
  134. if os.path.isfile('/etc/debian_version'):
  135. return GetDebLibDir()
  136. elif os.path.isfile('/etc/arch-release'):
  137. # ArchLinux has lib64, but it is a symlink to lib.
  138. return "lib"
  139. else:
  140. # Okay, maybe we're on an RPM-based system?
  141. return GetRPMLibDir()
  142. # If Python is installed into /usr/lib64, it's probably safe
  143. # to assume that we should install there as well.
  144. python_lib = get_python_lib(1)
  145. if python_lib.startswith('/usr/lib64/') or \
  146. python_lib.startswith('/usr/local/lib64/'):
  147. return "lib64"
  148. return "lib"
  149. def InstallPanda(destdir="", prefix="/usr", outputdir="built", libdir=GetLibDir(), python_versions=[]):
  150. if not prefix.startswith("/"):
  151. prefix = "/" + prefix
  152. libdir = prefix + "/" + libdir
  153. dest_prefix = destdir + prefix
  154. dest_libdir = destdir + libdir
  155. # Create the directory structure that we will be putting our files in.
  156. # Don't use os.makedirs or mkdir -p; neither properly set permissions for
  157. # created intermediate directories.
  158. MakeDirectory(dest_prefix + "/bin", mode=0o755, recursive=True)
  159. MakeDirectory(dest_prefix + "/include", mode=0o755)
  160. MakeDirectory(dest_prefix + "/include/panda3d", mode=0o755)
  161. MakeDirectory(dest_prefix + "/share", mode=0o755)
  162. MakeDirectory(dest_prefix + "/share/panda3d", mode=0o755)
  163. MakeDirectory(dest_prefix + "/share/mime-info", mode=0o755)
  164. MakeDirectory(dest_prefix + "/share/mime", mode=0o755)
  165. MakeDirectory(dest_prefix + "/share/mime/packages", mode=0o755)
  166. MakeDirectory(dest_prefix + "/share/application-registry", mode=0o755)
  167. MakeDirectory(dest_prefix + "/share/applications", mode=0o755)
  168. MakeDirectory(dest_libdir + "/panda3d", mode=0o755, recursive=True)
  169. for python_version in python_versions:
  170. MakeDirectory(destdir + python_version["purelib"], mode=0o755, recursive=True)
  171. MakeDirectory(destdir + python_version["platlib"] + "/panda3d", mode=0o755, recursive=True)
  172. if sys.platform.startswith("freebsd"):
  173. MakeDirectory(dest_prefix + "/etc", mode=0o755)
  174. MakeDirectory(destdir + "/usr/local/libdata/ldconfig", mode=0o755, recursive=True)
  175. else:
  176. MakeDirectory(destdir + "/etc/ld.so.conf.d", mode=0o755, recursive=True)
  177. # Write the Config.prc file.
  178. Configrc = ReadFile(outputdir + "/etc/Config.prc")
  179. Configrc = Configrc.replace("model-path $THIS_PRC_DIR/..", "model-path " + prefix + "/share/panda3d")
  180. if sys.platform.startswith("freebsd"):
  181. WriteFile(dest_prefix + "/etc/Config.prc", Configrc)
  182. oscmd(f"cp {outputdir}/etc/Confauto.prc {dest_prefix}/etc/Confauto.prc")
  183. else:
  184. WriteFile(destdir+"/etc/Config.prc", Configrc)
  185. oscmd(f"cp {outputdir}/etc/Confauto.prc {destdir}/etc/Confauto.prc")
  186. oscmd(f"cp -R {outputdir}/include/* {dest_prefix}/include/panda3d/")
  187. oscmd(f"cp -R {outputdir}/pandac {dest_prefix}/share/panda3d/")
  188. oscmd(f"cp -R {outputdir}/models {dest_prefix}/share/panda3d/")
  189. if os.path.isdir("samples"):
  190. oscmd(f"cp -R samples {dest_prefix}/share/panda3d/")
  191. if os.path.isdir(outputdir + "/direct"):
  192. oscmd(f"cp -R {outputdir}/direct {dest_prefix}/share/panda3d/")
  193. if os.path.isdir(outputdir + "/Pmw"):
  194. oscmd(f"cp -R {outputdir}/Pmw {dest_prefix}/share/panda3d/")
  195. if os.path.isdir(outputdir + "/plugins"):
  196. oscmd(f"cp -R {outputdir}/plugins {dest_prefix}/share/panda3d/")
  197. for python_version in python_versions:
  198. for base in os.listdir(outputdir + "/panda3d"):
  199. suffix = python_version["ext_suffix"]
  200. platlib = python_version["platlib"]
  201. if base.endswith(".py") or (base.endswith(suffix) and '.' not in base[:-len(suffix)]):
  202. oscmd(f"cp {outputdir}/panda3d/{base} {destdir}{platlib}/panda3d/{base}")
  203. bindir = outputdir + "/bin"
  204. WriteMimeFile(dest_prefix + "/share/mime-info/panda3d.mime", MIME_INFO, bindir)
  205. WriteKeysFile(dest_prefix + "/share/mime-info/panda3d.keys", MIME_INFO, bindir)
  206. WriteMimeXMLFile(dest_prefix + "/share/mime/packages/panda3d.xml", MIME_INFO, bindir)
  207. WriteApplicationsFile(dest_prefix + "/share/application-registry/panda3d.applications", APP_INFO, MIME_INFO, bindir)
  208. if os.path.isfile(outputdir + "/bin/pview"):
  209. oscmd(f"cp makepanda/pview.desktop {dest_prefix}/share/applications/pview.desktop")
  210. if os.path.isfile(outputdir + "/bin/pstats"):
  211. oscmd(f"cp makepanda/pstats.desktop {dest_prefix}/share/applications/pstats.desktop")
  212. oscmd(f"cp doc/ReleaseNotes {dest_prefix}/share/panda3d/ReleaseNotes")
  213. for python_version in python_versions:
  214. oscmd(f"echo '{prefix}/share/panda3d' > {destdir}{python_version['purelib']}/panda3d.pth")
  215. if os.path.isdir(outputdir + "/panda3d.dist-info"):
  216. oscmd(f"cp -R {outputdir}/panda3d.dist-info {destdir}{python_version['platlib']}")
  217. if sys.platform.startswith("freebsd"):
  218. oscmd(f"echo '{libdir}/panda3d' > {destdir}/usr/local/libdata/ldconfig/panda3d")
  219. else:
  220. oscmd(f"echo '{libdir}/panda3d' > {destdir}/etc/ld.so.conf.d/panda3d.conf")
  221. for base in os.listdir(outputdir + "/lib"):
  222. if not base.endswith(".a"):
  223. # We really need to specify -R in order not to follow symlinks on non-GNU
  224. oscmd(f"cp -R -P {outputdir}/lib/{base} {dest_libdir}/panda3d/{base}")
  225. for base in os.listdir(outputdir + "/bin"):
  226. if base not in EXCLUDE_BINARIES:
  227. oscmd(f"cp -R -P {outputdir}/bin/{base} {dest_prefix}/bin/{base}")
  228. DeleteVCS(dest_prefix + "/share/panda3d")
  229. DeleteBuildFiles(dest_prefix + "/share/panda3d")
  230. DeleteEmptyDirs(dest_prefix + "/share/panda3d")
  231. DeleteVCS(dest_prefix + "/include/panda3d")
  232. DeleteBuildFiles(dest_prefix + "/include/panda3d")
  233. DeleteEmptyDirs(dest_prefix + "/include/panda3d")
  234. # Change permissions on include directory.
  235. os.chmod(dest_prefix + "/include/panda3d", 0o755)
  236. for root, dirs, files in os.walk(dest_prefix + "/include/panda3d"):
  237. for basename in dirs:
  238. os.chmod(os.path.join(root, basename), 0o755)
  239. for basename in files:
  240. os.chmod(os.path.join(root, basename), 0o644)
  241. # rpmlint doesn't like this file, for some reason.
  242. if os.path.isfile(dest_prefix + "/share/panda3d/direct/leveleditor/copyfiles.pl"):
  243. os.remove(dest_prefix + "/share/panda3d/direct/leveleditor/copyfiles.pl")
  244. if __name__ == "__main__":
  245. if sys.platform.startswith("win") or sys.platform == "darwin":
  246. exit("This script is not supported on Windows or Mac OS X at the moment!")
  247. destdir = os.environ.get("DESTDIR", "/")
  248. parser = OptionParser()
  249. parser.add_option(
  250. '',
  251. '--outputdir',
  252. dest='outputdir',
  253. help='Makepanda\'s output directory (default: built)',
  254. default='built',
  255. )
  256. parser.add_option(
  257. '',
  258. '--destdir',
  259. dest='destdir',
  260. help='Destination directory [default=%s]' % destdir,
  261. default=destdir,
  262. )
  263. parser.add_option(
  264. '',
  265. '--prefix',
  266. dest='prefix',
  267. help='Prefix [default=/usr/local]',
  268. default='/usr/local',
  269. )
  270. parser.add_option(
  271. '',
  272. '--verbose',
  273. dest='verbose',
  274. help='Print commands that are executed [default=no]',
  275. action='store_true',
  276. default=False,
  277. )
  278. (options, args) = parser.parse_args()
  279. destdir = options.destdir
  280. if destdir.endswith("/"):
  281. destdir = destdir[:-1]
  282. if destdir == "/":
  283. destdir = ""
  284. if destdir != "" and not os.path.isdir(destdir):
  285. exit("Directory '%s' does not exist!" % destdir)
  286. SetOutputDir(options.outputdir)
  287. if options.verbose:
  288. SetVerbose(True)
  289. print("Installing Panda3D SDK into " + destdir + options.prefix)
  290. InstallPanda(
  291. destdir=destdir,
  292. prefix=options.prefix,
  293. outputdir=options.outputdir,
  294. python_versions=ReadPythonVersionInfoFile(),
  295. )
  296. print("Installation finished!")
  297. if not destdir:
  298. warn_prefix = "%sNote:%s " % (GetColor("red"), GetColor())
  299. print(warn_prefix + "You may need to call these commands to update system caches:")
  300. print(" sudo ldconfig")
  301. print(" sudo update-desktop-database")
  302. print(" sudo update-mime-database -n %s/share/mime" % (options.prefix))