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