ยปCore Development>Code coverage>Lib/distutils/command/install_lib.py

Python code coverage for Lib/distutils/command/install_lib.py

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