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