install_lib.py 8.3 KB

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