installpanda.py 12 KB

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