/Lib/distutils/command/install_lib.py

http://unladen-swallow.googlecode.com/ · Python · 224 lines · 136 code · 44 blank · 44 comment · 25 complexity · 482a53ba450fb9dbb3417ca1c56d9a4d MD5 · raw file

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