installpanda.py 14 KB

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