1 """distutils.command.install_lib
3 Implements the Distutils 'install_lib' command
4 (install all Python modules)."""
11 from distutils
.core
import Command
12 from distutils
.errors
import DistutilsOptionError
15 # Extension for Python source files.
16 if hasattr(os
, 'extsep'):
17 PYTHON_SOURCE_EXTENSION
= os
.extsep
+ "py"
19 PYTHON_SOURCE_EXTENSION
= ".py"
21 class install_lib(Command
):
23 description
= "install all Python modules (extensions and pure Python)"
25 # The byte-compilation options are a tad confusing. Here are the
27 # 1) no compilation at all (--no-compile --no-optimize)
28 # 2) compile .pyc only (--compile --no-optimize; default)
29 # 3) compile .pyc and "level 1" .pyo (--compile --optimize)
30 # 4) compile "level 1" .pyo only (--no-compile --optimize)
31 # 5) compile .pyc and "level 2" .pyo (--compile --optimize-more)
32 # 6) compile "level 2" .pyo only (--no-compile --optimize-more)
34 # The UI for this is two option, 'compile' and 'optimize'.
35 # 'compile' is strictly boolean, and only decides whether to
36 # generate .pyc files. 'optimize' is three-way (0, 1, or 2), and
37 # decides both whether to generate .pyo files and what level of
38 # optimization to use.
41 ('install-dir=', 'd', "directory to install to"),
42 ('build-dir=','b', "build directory (where to install from)"),
43 ('force', 'f', "force installation (overwrite existing files)"),
44 ('compile', 'c', "compile .py to .pyc [default]"),
45 ('no-compile', None, "don't compile .py files"),
47 "also compile with optimization: -O1 for \"python -O\", "
48 "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
49 ('skip-build', None, "skip the build steps"),
52 boolean_options
= ['force', 'compile', 'skip-build']
53 negative_opt
= {'no-compile' : 'compile'}
55 def initialize_options(self
):
56 # let the 'install' command dictate our installation directory
57 self
.install_dir
= None
62 self
.skip_build
= None
64 def finalize_options(self
):
65 # Get all the information we need to install pure Python modules
66 # from the umbrella 'install' command -- build (source) directory,
67 # install (target) directory, and whether to compile .py files.
68 self
.set_undefined_options('install',
69 ('build_lib', 'build_dir'),
70 ('install_lib', 'install_dir'),
72 ('compile', 'compile'),
73 ('optimize', 'optimize'),
74 ('skip_build', 'skip_build'),
77 if self
.compile is None:
79 if self
.optimize
is None:
82 if not isinstance(self
.optimize
, int):
84 self
.optimize
= int(self
.optimize
)
85 if self
.optimize
not in (0, 1, 2):
87 except (ValueError, AssertionError):
88 raise DistutilsOptionError
, "optimize must be 0, 1, or 2"
91 # Make sure we have built everything we need first
94 # Install everything: simply dump the entire contents of the build
95 # directory to the installation directory (that's the beauty of
96 # having a build directory!)
97 outfiles
= self
.install()
99 # (Optionally) compile .py to .pyc
100 if outfiles
is not None and self
.distribution
.has_pure_modules():
101 self
.byte_compile(outfiles
)
103 # -- Top-level worker functions ------------------------------------
104 # (called from 'run()')
107 if not self
.skip_build
:
108 if self
.distribution
.has_pure_modules():
109 self
.run_command('build_py')
110 if self
.distribution
.has_ext_modules():
111 self
.run_command('build_ext')
114 if os
.path
.isdir(self
.build_dir
):
115 outfiles
= self
.copy_tree(self
.build_dir
, self
.install_dir
)
117 self
.warn("'%s' does not exist -- no Python modules to install" %
122 def byte_compile(self
, files
):
123 if sys
.dont_write_bytecode
:
124 self
.warn('byte-compiling is disabled, skipping.')
127 from distutils
.util
import byte_compile
129 # Get the "--root" directory supplied to the "install" command,
130 # and use it as a prefix to strip off the purported filename
131 # encoded in bytecode files. This is far from complete, but it
132 # should at least generate usable bytecode in RPM distributions.
133 install_root
= self
.get_finalized_command('install').root
136 byte_compile(files
, optimize
=0,
137 force
=self
.force
, prefix
=install_root
,
138 dry_run
=self
.dry_run
)
139 if self
.optimize
> 0:
140 byte_compile(files
, optimize
=self
.optimize
,
141 force
=self
.force
, prefix
=install_root
,
142 verbose
=self
.verbose
, dry_run
=self
.dry_run
)
145 # -- Utility methods -----------------------------------------------
147 def _mutate_outputs(self
, has_any
, build_cmd
, cmd_option
, output_dir
):
151 build_cmd
= self
.get_finalized_command(build_cmd
)
152 build_files
= build_cmd
.get_outputs()
153 build_dir
= getattr(build_cmd
, cmd_option
)
155 prefix_len
= len(build_dir
) + len(os
.sep
)
157 for file in build_files
:
158 outputs
.append(os
.path
.join(output_dir
, file[prefix_len
:]))
162 def _bytecode_filenames(self
, py_filenames
):
164 for py_file
in py_filenames
:
165 # Since build_py handles package data installation, the
166 # list of outputs can contain more than just .py files.
167 # Make sure we only report bytecode for the .py files.
168 ext
= os
.path
.splitext(os
.path
.normcase(py_file
))[1]
169 if ext
!= PYTHON_SOURCE_EXTENSION
:
172 bytecode_files
.append(py_file
+ "c")
173 if self
.optimize
> 0:
174 bytecode_files
.append(py_file
+ "o")
176 return bytecode_files
179 # -- External interface --------------------------------------------
180 # (called by outsiders)
182 def get_outputs(self
):
183 """Return the list of files that would be installed if this command
184 were actually run. Not affected by the "dry-run" flag or whether
185 modules have actually been built yet.
188 self
._mutate
_outputs
(self
.distribution
.has_pure_modules(),
189 'build_py', 'build_lib',
192 bytecode_outputs
= self
._bytecode
_filenames
(pure_outputs
)
194 bytecode_outputs
= []
197 self
._mutate
_outputs
(self
.distribution
.has_ext_modules(),
198 'build_ext', 'build_lib',
201 return pure_outputs
+ bytecode_outputs
+ ext_outputs
203 def get_inputs(self
):
204 """Get the list of files that are input to this command, ie. the
205 files that get installed as they are named in the build tree.
206 The files in this list correspond one-to-one to the output
207 filenames returned by 'get_outputs()'.
211 if self
.distribution
.has_pure_modules():
212 build_py
= self
.get_finalized_command('build_py')
213 inputs
.extend(build_py
.get_outputs())
215 if self
.distribution
.has_ext_modules():
216 build_ext
= self
.get_finalized_command('build_ext')
217 inputs
.extend(build_ext
.get_outputs())