installpanda.py 12 KB

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