install_lib.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. """distutils.command.install_lib
  2. Implements the Distutils 'install_lib' command
  3. (install all Python modules)."""
  4. import os
  5. import importlib.util
  6. import sys
  7. from distutils.core import Command
  8. from distutils.errors import DistutilsOptionError
  9. # Extension for Python source files.
  10. PYTHON_SOURCE_EXTENSION = ".py"
  11. class install_lib(Command):
  12. description = "install all Python modules (extensions and pure Python)"
  13. # The byte-compilation options are a tad confusing. Here are the
  14. # possible scenarios:
  15. # 1) no compilation at all (--no-compile --no-optimize)
  16. # 2) compile .pyc only (--compile --no-optimize; default)
  17. # 3) compile .pyc and "opt-1" .pyc (--compile --optimize)
  18. # 4) compile "opt-1" .pyc only (--no-compile --optimize)
  19. # 5) compile .pyc and "opt-2" .pyc (--compile --optimize-more)
  20. # 6) compile "opt-2" .pyc only (--no-compile --optimize-more)
  21. #
  22. # The UI for this is two options, 'compile' and 'optimize'.
  23. # 'compile' is strictly boolean, and only decides whether to
  24. # generate .pyc files. 'optimize' is three-way (0, 1, or 2), and
  25. # decides both whether to generate .pyc files and what level of
  26. # optimization to use.
  27. user_options = [
  28. ('install-dir=', 'd', "directory to install to"),
  29. ('build-dir=', 'b', "build directory (where to install from)"),
  30. ('force', 'f', "force installation (overwrite existing files)"),
  31. ('compile', 'c', "compile .py to .pyc [default]"),
  32. ('no-compile', None, "don't compile .py files"),
  33. (
  34. 'optimize=',
  35. 'O',
  36. "also compile with optimization: -O1 for \"python -O\", "
  37. "-O2 for \"python -OO\", and -O0 to disable [default: -O0]",
  38. ),
  39. ('skip-build', None, "skip the build steps"),
  40. ]
  41. boolean_options = ['force', 'compile', 'skip-build']
  42. negative_opt = {'no-compile': 'compile'}
  43. def initialize_options(self):
  44. # let the 'install' command dictate our installation directory
  45. self.install_dir = None
  46. self.build_dir = None
  47. self.force = 0
  48. self.compile = None
  49. self.optimize = None
  50. self.skip_build = None
  51. def finalize_options(self):
  52. # Get all the information we need to install pure Python modules
  53. # from the umbrella 'install' command -- build (source) directory,
  54. # install (target) directory, and whether to compile .py files.
  55. self.set_undefined_options(
  56. 'install',
  57. ('build_lib', 'build_dir'),
  58. ('install_lib', 'install_dir'),
  59. ('force', 'force'),
  60. ('compile', 'compile'),
  61. ('optimize', 'optimize'),
  62. ('skip_build', 'skip_build'),
  63. )
  64. if self.compile is None:
  65. self.compile = True
  66. if self.optimize is None:
  67. self.optimize = False
  68. if not isinstance(self.optimize, int):
  69. try:
  70. self.optimize = int(self.optimize)
  71. if self.optimize not in (0, 1, 2):
  72. raise AssertionError
  73. except (ValueError, AssertionError):
  74. raise DistutilsOptionError("optimize must be 0, 1, or 2")
  75. def run(self):
  76. # Make sure we have built everything we need first
  77. self.build()
  78. # Install everything: simply dump the entire contents of the build
  79. # directory to the installation directory (that's the beauty of
  80. # having a build directory!)
  81. outfiles = self.install()
  82. # (Optionally) compile .py to .pyc
  83. if outfiles is not None and self.distribution.has_pure_modules():
  84. self.byte_compile(outfiles)
  85. # -- Top-level worker functions ------------------------------------
  86. # (called from 'run()')
  87. def build(self):
  88. if not self.skip_build:
  89. if self.distribution.has_pure_modules():
  90. self.run_command('build_py')
  91. if self.distribution.has_ext_modules():
  92. self.run_command('build_ext')
  93. def install(self):
  94. if os.path.isdir(self.build_dir):
  95. outfiles = self.copy_tree(self.build_dir, self.install_dir)
  96. else:
  97. self.warn(
  98. "'%s' does not exist -- no Python modules to install" % self.build_dir
  99. )
  100. return
  101. return outfiles
  102. def byte_compile(self, files):
  103. if sys.dont_write_bytecode:
  104. self.warn('byte-compiling is disabled, skipping.')
  105. return
  106. from distutils.util import byte_compile
  107. # Get the "--root" directory supplied to the "install" command,
  108. # and use it as a prefix to strip off the purported filename
  109. # encoded in bytecode files. This is far from complete, but it
  110. # should at least generate usable bytecode in RPM distributions.
  111. install_root = self.get_finalized_command('install').root
  112. if self.compile:
  113. byte_compile(
  114. files,
  115. optimize=0,
  116. force=self.force,
  117. prefix=install_root,
  118. dry_run=self.dry_run,
  119. )
  120. if self.optimize > 0:
  121. byte_compile(
  122. files,
  123. optimize=self.optimize,
  124. force=self.force,
  125. prefix=install_root,
  126. verbose=self.verbose,
  127. dry_run=self.dry_run,
  128. )
  129. # -- Utility methods -----------------------------------------------
  130. def _mutate_outputs(self, has_any, build_cmd, cmd_option, output_dir):
  131. if not has_any:
  132. return []
  133. build_cmd = self.get_finalized_command(build_cmd)
  134. build_files = build_cmd.get_outputs()
  135. build_dir = getattr(build_cmd, cmd_option)
  136. prefix_len = len(build_dir) + len(os.sep)
  137. outputs = []
  138. for file in build_files:
  139. outputs.append(os.path.join(output_dir, file[prefix_len:]))
  140. return outputs
  141. def _bytecode_filenames(self, py_filenames):
  142. bytecode_files = []
  143. for py_file in py_filenames:
  144. # Since build_py handles package data installation, the
  145. # list of outputs can contain more than just .py files.
  146. # Make sure we only report bytecode for the .py files.
  147. ext = os.path.splitext(os.path.normcase(py_file))[1]
  148. if ext != PYTHON_SOURCE_EXTENSION:
  149. continue
  150. if self.compile:
  151. bytecode_files.append(
  152. importlib.util.cache_from_source(py_file, optimization='')
  153. )
  154. if self.optimize > 0:
  155. bytecode_files.append(
  156. importlib.util.cache_from_source(
  157. py_file, optimization=self.optimize
  158. )
  159. )
  160. return bytecode_files
  161. # -- External interface --------------------------------------------
  162. # (called by outsiders)
  163. def get_outputs(self):
  164. """Return the list of files that would be installed if this command
  165. were actually run. Not affected by the "dry-run" flag or whether
  166. modules have actually been built yet.
  167. """
  168. pure_outputs = self._mutate_outputs(
  169. self.distribution.has_pure_modules(),
  170. 'build_py',
  171. 'build_lib',
  172. self.install_dir,
  173. )
  174. if self.compile:
  175. bytecode_outputs = self._bytecode_filenames(pure_outputs)
  176. else:
  177. bytecode_outputs = []
  178. ext_outputs = self._mutate_outputs(
  179. self.distribution.has_ext_modules(),
  180. 'build_ext',
  181. 'build_lib',
  182. self.install_dir,
  183. )
  184. return pure_outputs + bytecode_outputs + ext_outputs
  185. def get_inputs(self):
  186. """Get the list of files that are input to this command, ie. the
  187. files that get installed as they are named in the build tree.
  188. The files in this list correspond one-to-one to the output
  189. filenames returned by 'get_outputs()'.
  190. """
  191. inputs = []
  192. if self.distribution.has_pure_modules():
  193. build_py = self.get_finalized_command('build_py')
  194. inputs.extend(build_py.get_outputs())
  195. if self.distribution.has_ext_modules():
  196. build_ext = self.get_finalized_command('build_ext')
  197. inputs.extend(build_ext.get_outputs())
  198. return inputs