installpanda.py 13 KB

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