1 | n/a | """distutils.command.build_py |
---|
2 | n/a | |
---|
3 | n/a | Implements the Distutils 'build_py' command.""" |
---|
4 | n/a | |
---|
5 | n/a | import os |
---|
6 | n/a | import importlib.util |
---|
7 | n/a | import sys |
---|
8 | n/a | from glob import glob |
---|
9 | n/a | |
---|
10 | n/a | from distutils.core import Command |
---|
11 | n/a | from distutils.errors import * |
---|
12 | n/a | from distutils.util import convert_path, Mixin2to3 |
---|
13 | n/a | from distutils import log |
---|
14 | n/a | |
---|
15 | n/a | class build_py (Command): |
---|
16 | n/a | |
---|
17 | n/a | description = "\"build\" pure Python modules (copy to build directory)" |
---|
18 | n/a | |
---|
19 | n/a | user_options = [ |
---|
20 | n/a | ('build-lib=', 'd', "directory to \"build\" (copy) to"), |
---|
21 | n/a | ('compile', 'c', "compile .py to .pyc"), |
---|
22 | n/a | ('no-compile', None, "don't compile .py files [default]"), |
---|
23 | n/a | ('optimize=', 'O', |
---|
24 | n/a | "also compile with optimization: -O1 for \"python -O\", " |
---|
25 | n/a | "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"), |
---|
26 | n/a | ('force', 'f', "forcibly build everything (ignore file timestamps)"), |
---|
27 | n/a | ] |
---|
28 | n/a | |
---|
29 | n/a | boolean_options = ['compile', 'force'] |
---|
30 | n/a | negative_opt = {'no-compile' : 'compile'} |
---|
31 | n/a | |
---|
32 | n/a | def initialize_options(self): |
---|
33 | n/a | self.build_lib = None |
---|
34 | n/a | self.py_modules = None |
---|
35 | n/a | self.package = None |
---|
36 | n/a | self.package_data = None |
---|
37 | n/a | self.package_dir = None |
---|
38 | n/a | self.compile = 0 |
---|
39 | n/a | self.optimize = 0 |
---|
40 | n/a | self.force = None |
---|
41 | n/a | |
---|
42 | n/a | def finalize_options(self): |
---|
43 | n/a | self.set_undefined_options('build', |
---|
44 | n/a | ('build_lib', 'build_lib'), |
---|
45 | n/a | ('force', 'force')) |
---|
46 | n/a | |
---|
47 | n/a | # Get the distribution options that are aliases for build_py |
---|
48 | n/a | # options -- list of packages and list of modules. |
---|
49 | n/a | self.packages = self.distribution.packages |
---|
50 | n/a | self.py_modules = self.distribution.py_modules |
---|
51 | n/a | self.package_data = self.distribution.package_data |
---|
52 | n/a | self.package_dir = {} |
---|
53 | n/a | if self.distribution.package_dir: |
---|
54 | n/a | for name, path in self.distribution.package_dir.items(): |
---|
55 | n/a | self.package_dir[name] = convert_path(path) |
---|
56 | n/a | self.data_files = self.get_data_files() |
---|
57 | n/a | |
---|
58 | n/a | # Ick, copied straight from install_lib.py (fancy_getopt needs a |
---|
59 | n/a | # type system! Hell, *everything* needs a type system!!!) |
---|
60 | n/a | if not isinstance(self.optimize, int): |
---|
61 | n/a | try: |
---|
62 | n/a | self.optimize = int(self.optimize) |
---|
63 | n/a | assert 0 <= self.optimize <= 2 |
---|
64 | n/a | except (ValueError, AssertionError): |
---|
65 | n/a | raise DistutilsOptionError("optimize must be 0, 1, or 2") |
---|
66 | n/a | |
---|
67 | n/a | def run(self): |
---|
68 | n/a | # XXX copy_file by default preserves atime and mtime. IMHO this is |
---|
69 | n/a | # the right thing to do, but perhaps it should be an option -- in |
---|
70 | n/a | # particular, a site administrator might want installed files to |
---|
71 | n/a | # reflect the time of installation rather than the last |
---|
72 | n/a | # modification time before the installed release. |
---|
73 | n/a | |
---|
74 | n/a | # XXX copy_file by default preserves mode, which appears to be the |
---|
75 | n/a | # wrong thing to do: if a file is read-only in the working |
---|
76 | n/a | # directory, we want it to be installed read/write so that the next |
---|
77 | n/a | # installation of the same module distribution can overwrite it |
---|
78 | n/a | # without problems. (This might be a Unix-specific issue.) Thus |
---|
79 | n/a | # we turn off 'preserve_mode' when copying to the build directory, |
---|
80 | n/a | # since the build directory is supposed to be exactly what the |
---|
81 | n/a | # installation will look like (ie. we preserve mode when |
---|
82 | n/a | # installing). |
---|
83 | n/a | |
---|
84 | n/a | # Two options control which modules will be installed: 'packages' |
---|
85 | n/a | # and 'py_modules'. The former lets us work with whole packages, not |
---|
86 | n/a | # specifying individual modules at all; the latter is for |
---|
87 | n/a | # specifying modules one-at-a-time. |
---|
88 | n/a | |
---|
89 | n/a | if self.py_modules: |
---|
90 | n/a | self.build_modules() |
---|
91 | n/a | if self.packages: |
---|
92 | n/a | self.build_packages() |
---|
93 | n/a | self.build_package_data() |
---|
94 | n/a | |
---|
95 | n/a | self.byte_compile(self.get_outputs(include_bytecode=0)) |
---|
96 | n/a | |
---|
97 | n/a | def get_data_files(self): |
---|
98 | n/a | """Generate list of '(package,src_dir,build_dir,filenames)' tuples""" |
---|
99 | n/a | data = [] |
---|
100 | n/a | if not self.packages: |
---|
101 | n/a | return data |
---|
102 | n/a | for package in self.packages: |
---|
103 | n/a | # Locate package source directory |
---|
104 | n/a | src_dir = self.get_package_dir(package) |
---|
105 | n/a | |
---|
106 | n/a | # Compute package build directory |
---|
107 | n/a | build_dir = os.path.join(*([self.build_lib] + package.split('.'))) |
---|
108 | n/a | |
---|
109 | n/a | # Length of path to strip from found files |
---|
110 | n/a | plen = 0 |
---|
111 | n/a | if src_dir: |
---|
112 | n/a | plen = len(src_dir)+1 |
---|
113 | n/a | |
---|
114 | n/a | # Strip directory from globbed filenames |
---|
115 | n/a | filenames = [ |
---|
116 | n/a | file[plen:] for file in self.find_data_files(package, src_dir) |
---|
117 | n/a | ] |
---|
118 | n/a | data.append((package, src_dir, build_dir, filenames)) |
---|
119 | n/a | return data |
---|
120 | n/a | |
---|
121 | n/a | def find_data_files(self, package, src_dir): |
---|
122 | n/a | """Return filenames for package's data files in 'src_dir'""" |
---|
123 | n/a | globs = (self.package_data.get('', []) |
---|
124 | n/a | + self.package_data.get(package, [])) |
---|
125 | n/a | files = [] |
---|
126 | n/a | for pattern in globs: |
---|
127 | n/a | # Each pattern has to be converted to a platform-specific path |
---|
128 | n/a | filelist = glob(os.path.join(src_dir, convert_path(pattern))) |
---|
129 | n/a | # Files that match more than one pattern are only added once |
---|
130 | n/a | files.extend([fn for fn in filelist if fn not in files |
---|
131 | n/a | and os.path.isfile(fn)]) |
---|
132 | n/a | return files |
---|
133 | n/a | |
---|
134 | n/a | def build_package_data(self): |
---|
135 | n/a | """Copy data files into build directory""" |
---|
136 | n/a | lastdir = None |
---|
137 | n/a | for package, src_dir, build_dir, filenames in self.data_files: |
---|
138 | n/a | for filename in filenames: |
---|
139 | n/a | target = os.path.join(build_dir, filename) |
---|
140 | n/a | self.mkpath(os.path.dirname(target)) |
---|
141 | n/a | self.copy_file(os.path.join(src_dir, filename), target, |
---|
142 | n/a | preserve_mode=False) |
---|
143 | n/a | |
---|
144 | n/a | def get_package_dir(self, package): |
---|
145 | n/a | """Return the directory, relative to the top of the source |
---|
146 | n/a | distribution, where package 'package' should be found |
---|
147 | n/a | (at least according to the 'package_dir' option, if any).""" |
---|
148 | n/a | path = package.split('.') |
---|
149 | n/a | |
---|
150 | n/a | if not self.package_dir: |
---|
151 | n/a | if path: |
---|
152 | n/a | return os.path.join(*path) |
---|
153 | n/a | else: |
---|
154 | n/a | return '' |
---|
155 | n/a | else: |
---|
156 | n/a | tail = [] |
---|
157 | n/a | while path: |
---|
158 | n/a | try: |
---|
159 | n/a | pdir = self.package_dir['.'.join(path)] |
---|
160 | n/a | except KeyError: |
---|
161 | n/a | tail.insert(0, path[-1]) |
---|
162 | n/a | del path[-1] |
---|
163 | n/a | else: |
---|
164 | n/a | tail.insert(0, pdir) |
---|
165 | n/a | return os.path.join(*tail) |
---|
166 | n/a | else: |
---|
167 | n/a | # Oops, got all the way through 'path' without finding a |
---|
168 | n/a | # match in package_dir. If package_dir defines a directory |
---|
169 | n/a | # for the root (nameless) package, then fallback on it; |
---|
170 | n/a | # otherwise, we might as well have not consulted |
---|
171 | n/a | # package_dir at all, as we just use the directory implied |
---|
172 | n/a | # by 'tail' (which should be the same as the original value |
---|
173 | n/a | # of 'path' at this point). |
---|
174 | n/a | pdir = self.package_dir.get('') |
---|
175 | n/a | if pdir is not None: |
---|
176 | n/a | tail.insert(0, pdir) |
---|
177 | n/a | |
---|
178 | n/a | if tail: |
---|
179 | n/a | return os.path.join(*tail) |
---|
180 | n/a | else: |
---|
181 | n/a | return '' |
---|
182 | n/a | |
---|
183 | n/a | def check_package(self, package, package_dir): |
---|
184 | n/a | # Empty dir name means current directory, which we can probably |
---|
185 | n/a | # assume exists. Also, os.path.exists and isdir don't know about |
---|
186 | n/a | # my "empty string means current dir" convention, so we have to |
---|
187 | n/a | # circumvent them. |
---|
188 | n/a | if package_dir != "": |
---|
189 | n/a | if not os.path.exists(package_dir): |
---|
190 | n/a | raise DistutilsFileError( |
---|
191 | n/a | "package directory '%s' does not exist" % package_dir) |
---|
192 | n/a | if not os.path.isdir(package_dir): |
---|
193 | n/a | raise DistutilsFileError( |
---|
194 | n/a | "supposed package directory '%s' exists, " |
---|
195 | n/a | "but is not a directory" % package_dir) |
---|
196 | n/a | |
---|
197 | n/a | # Require __init__.py for all but the "root package" |
---|
198 | n/a | if package: |
---|
199 | n/a | init_py = os.path.join(package_dir, "__init__.py") |
---|
200 | n/a | if os.path.isfile(init_py): |
---|
201 | n/a | return init_py |
---|
202 | n/a | else: |
---|
203 | n/a | log.warn(("package init file '%s' not found " + |
---|
204 | n/a | "(or not a regular file)"), init_py) |
---|
205 | n/a | |
---|
206 | n/a | # Either not in a package at all (__init__.py not expected), or |
---|
207 | n/a | # __init__.py doesn't exist -- so don't return the filename. |
---|
208 | n/a | return None |
---|
209 | n/a | |
---|
210 | n/a | def check_module(self, module, module_file): |
---|
211 | n/a | if not os.path.isfile(module_file): |
---|
212 | n/a | log.warn("file %s (for module %s) not found", module_file, module) |
---|
213 | n/a | return False |
---|
214 | n/a | else: |
---|
215 | n/a | return True |
---|
216 | n/a | |
---|
217 | n/a | def find_package_modules(self, package, package_dir): |
---|
218 | n/a | self.check_package(package, package_dir) |
---|
219 | n/a | module_files = glob(os.path.join(package_dir, "*.py")) |
---|
220 | n/a | modules = [] |
---|
221 | n/a | setup_script = os.path.abspath(self.distribution.script_name) |
---|
222 | n/a | |
---|
223 | n/a | for f in module_files: |
---|
224 | n/a | abs_f = os.path.abspath(f) |
---|
225 | n/a | if abs_f != setup_script: |
---|
226 | n/a | module = os.path.splitext(os.path.basename(f))[0] |
---|
227 | n/a | modules.append((package, module, f)) |
---|
228 | n/a | else: |
---|
229 | n/a | self.debug_print("excluding %s" % setup_script) |
---|
230 | n/a | return modules |
---|
231 | n/a | |
---|
232 | n/a | def find_modules(self): |
---|
233 | n/a | """Finds individually-specified Python modules, ie. those listed by |
---|
234 | n/a | module name in 'self.py_modules'. Returns a list of tuples (package, |
---|
235 | n/a | module_base, filename): 'package' is a tuple of the path through |
---|
236 | n/a | package-space to the module; 'module_base' is the bare (no |
---|
237 | n/a | packages, no dots) module name, and 'filename' is the path to the |
---|
238 | n/a | ".py" file (relative to the distribution root) that implements the |
---|
239 | n/a | module. |
---|
240 | n/a | """ |
---|
241 | n/a | # Map package names to tuples of useful info about the package: |
---|
242 | n/a | # (package_dir, checked) |
---|
243 | n/a | # package_dir - the directory where we'll find source files for |
---|
244 | n/a | # this package |
---|
245 | n/a | # checked - true if we have checked that the package directory |
---|
246 | n/a | # is valid (exists, contains __init__.py, ... ?) |
---|
247 | n/a | packages = {} |
---|
248 | n/a | |
---|
249 | n/a | # List of (package, module, filename) tuples to return |
---|
250 | n/a | modules = [] |
---|
251 | n/a | |
---|
252 | n/a | # We treat modules-in-packages almost the same as toplevel modules, |
---|
253 | n/a | # just the "package" for a toplevel is empty (either an empty |
---|
254 | n/a | # string or empty list, depending on context). Differences: |
---|
255 | n/a | # - don't check for __init__.py in directory for empty package |
---|
256 | n/a | for module in self.py_modules: |
---|
257 | n/a | path = module.split('.') |
---|
258 | n/a | package = '.'.join(path[0:-1]) |
---|
259 | n/a | module_base = path[-1] |
---|
260 | n/a | |
---|
261 | n/a | try: |
---|
262 | n/a | (package_dir, checked) = packages[package] |
---|
263 | n/a | except KeyError: |
---|
264 | n/a | package_dir = self.get_package_dir(package) |
---|
265 | n/a | checked = 0 |
---|
266 | n/a | |
---|
267 | n/a | if not checked: |
---|
268 | n/a | init_py = self.check_package(package, package_dir) |
---|
269 | n/a | packages[package] = (package_dir, 1) |
---|
270 | n/a | if init_py: |
---|
271 | n/a | modules.append((package, "__init__", init_py)) |
---|
272 | n/a | |
---|
273 | n/a | # XXX perhaps we should also check for just .pyc files |
---|
274 | n/a | # (so greedy closed-source bastards can distribute Python |
---|
275 | n/a | # modules too) |
---|
276 | n/a | module_file = os.path.join(package_dir, module_base + ".py") |
---|
277 | n/a | if not self.check_module(module, module_file): |
---|
278 | n/a | continue |
---|
279 | n/a | |
---|
280 | n/a | modules.append((package, module_base, module_file)) |
---|
281 | n/a | |
---|
282 | n/a | return modules |
---|
283 | n/a | |
---|
284 | n/a | def find_all_modules(self): |
---|
285 | n/a | """Compute the list of all modules that will be built, whether |
---|
286 | n/a | they are specified one-module-at-a-time ('self.py_modules') or |
---|
287 | n/a | by whole packages ('self.packages'). Return a list of tuples |
---|
288 | n/a | (package, module, module_file), just like 'find_modules()' and |
---|
289 | n/a | 'find_package_modules()' do.""" |
---|
290 | n/a | modules = [] |
---|
291 | n/a | if self.py_modules: |
---|
292 | n/a | modules.extend(self.find_modules()) |
---|
293 | n/a | if self.packages: |
---|
294 | n/a | for package in self.packages: |
---|
295 | n/a | package_dir = self.get_package_dir(package) |
---|
296 | n/a | m = self.find_package_modules(package, package_dir) |
---|
297 | n/a | modules.extend(m) |
---|
298 | n/a | return modules |
---|
299 | n/a | |
---|
300 | n/a | def get_source_files(self): |
---|
301 | n/a | return [module[-1] for module in self.find_all_modules()] |
---|
302 | n/a | |
---|
303 | n/a | def get_module_outfile(self, build_dir, package, module): |
---|
304 | n/a | outfile_path = [build_dir] + list(package) + [module + ".py"] |
---|
305 | n/a | return os.path.join(*outfile_path) |
---|
306 | n/a | |
---|
307 | n/a | def get_outputs(self, include_bytecode=1): |
---|
308 | n/a | modules = self.find_all_modules() |
---|
309 | n/a | outputs = [] |
---|
310 | n/a | for (package, module, module_file) in modules: |
---|
311 | n/a | package = package.split('.') |
---|
312 | n/a | filename = self.get_module_outfile(self.build_lib, package, module) |
---|
313 | n/a | outputs.append(filename) |
---|
314 | n/a | if include_bytecode: |
---|
315 | n/a | if self.compile: |
---|
316 | n/a | outputs.append(importlib.util.cache_from_source( |
---|
317 | n/a | filename, optimization='')) |
---|
318 | n/a | if self.optimize > 0: |
---|
319 | n/a | outputs.append(importlib.util.cache_from_source( |
---|
320 | n/a | filename, optimization=self.optimize)) |
---|
321 | n/a | |
---|
322 | n/a | outputs += [ |
---|
323 | n/a | os.path.join(build_dir, filename) |
---|
324 | n/a | for package, src_dir, build_dir, filenames in self.data_files |
---|
325 | n/a | for filename in filenames |
---|
326 | n/a | ] |
---|
327 | n/a | |
---|
328 | n/a | return outputs |
---|
329 | n/a | |
---|
330 | n/a | def build_module(self, module, module_file, package): |
---|
331 | n/a | if isinstance(package, str): |
---|
332 | n/a | package = package.split('.') |
---|
333 | n/a | elif not isinstance(package, (list, tuple)): |
---|
334 | n/a | raise TypeError( |
---|
335 | n/a | "'package' must be a string (dot-separated), list, or tuple") |
---|
336 | n/a | |
---|
337 | n/a | # Now put the module source file into the "build" area -- this is |
---|
338 | n/a | # easy, we just copy it somewhere under self.build_lib (the build |
---|
339 | n/a | # directory for Python source). |
---|
340 | n/a | outfile = self.get_module_outfile(self.build_lib, package, module) |
---|
341 | n/a | dir = os.path.dirname(outfile) |
---|
342 | n/a | self.mkpath(dir) |
---|
343 | n/a | return self.copy_file(module_file, outfile, preserve_mode=0) |
---|
344 | n/a | |
---|
345 | n/a | def build_modules(self): |
---|
346 | n/a | modules = self.find_modules() |
---|
347 | n/a | for (package, module, module_file) in modules: |
---|
348 | n/a | # Now "build" the module -- ie. copy the source file to |
---|
349 | n/a | # self.build_lib (the build directory for Python source). |
---|
350 | n/a | # (Actually, it gets copied to the directory for this package |
---|
351 | n/a | # under self.build_lib.) |
---|
352 | n/a | self.build_module(module, module_file, package) |
---|
353 | n/a | |
---|
354 | n/a | def build_packages(self): |
---|
355 | n/a | for package in self.packages: |
---|
356 | n/a | # Get list of (package, module, module_file) tuples based on |
---|
357 | n/a | # scanning the package directory. 'package' is only included |
---|
358 | n/a | # in the tuple so that 'find_modules()' and |
---|
359 | n/a | # 'find_package_tuples()' have a consistent interface; it's |
---|
360 | n/a | # ignored here (apart from a sanity check). Also, 'module' is |
---|
361 | n/a | # the *unqualified* module name (ie. no dots, no package -- we |
---|
362 | n/a | # already know its package!), and 'module_file' is the path to |
---|
363 | n/a | # the .py file, relative to the current directory |
---|
364 | n/a | # (ie. including 'package_dir'). |
---|
365 | n/a | package_dir = self.get_package_dir(package) |
---|
366 | n/a | modules = self.find_package_modules(package, package_dir) |
---|
367 | n/a | |
---|
368 | n/a | # Now loop over the modules we found, "building" each one (just |
---|
369 | n/a | # copy it to self.build_lib). |
---|
370 | n/a | for (package_, module, module_file) in modules: |
---|
371 | n/a | assert package == package_ |
---|
372 | n/a | self.build_module(module, module_file, package) |
---|
373 | n/a | |
---|
374 | n/a | def byte_compile(self, files): |
---|
375 | n/a | if sys.dont_write_bytecode: |
---|
376 | n/a | self.warn('byte-compiling is disabled, skipping.') |
---|
377 | n/a | return |
---|
378 | n/a | |
---|
379 | n/a | from distutils.util import byte_compile |
---|
380 | n/a | prefix = self.build_lib |
---|
381 | n/a | if prefix[-1] != os.sep: |
---|
382 | n/a | prefix = prefix + os.sep |
---|
383 | n/a | |
---|
384 | n/a | # XXX this code is essentially the same as the 'byte_compile() |
---|
385 | n/a | # method of the "install_lib" command, except for the determination |
---|
386 | n/a | # of the 'prefix' string. Hmmm. |
---|
387 | n/a | if self.compile: |
---|
388 | n/a | byte_compile(files, optimize=0, |
---|
389 | n/a | force=self.force, prefix=prefix, dry_run=self.dry_run) |
---|
390 | n/a | if self.optimize > 0: |
---|
391 | n/a | byte_compile(files, optimize=self.optimize, |
---|
392 | n/a | force=self.force, prefix=prefix, dry_run=self.dry_run) |
---|
393 | n/a | |
---|
394 | n/a | class build_py_2to3(build_py, Mixin2to3): |
---|
395 | n/a | def run(self): |
---|
396 | n/a | self.updated_files = [] |
---|
397 | n/a | |
---|
398 | n/a | # Base class code |
---|
399 | n/a | if self.py_modules: |
---|
400 | n/a | self.build_modules() |
---|
401 | n/a | if self.packages: |
---|
402 | n/a | self.build_packages() |
---|
403 | n/a | self.build_package_data() |
---|
404 | n/a | |
---|
405 | n/a | # 2to3 |
---|
406 | n/a | self.run_2to3(self.updated_files) |
---|
407 | n/a | |
---|
408 | n/a | # Remaining base class code |
---|
409 | n/a | self.byte_compile(self.get_outputs(include_bytecode=0)) |
---|
410 | n/a | |
---|
411 | n/a | def build_module(self, module, module_file, package): |
---|
412 | n/a | res = build_py.build_module(self, module, module_file, package) |
---|
413 | n/a | if res[1]: |
---|
414 | n/a | # file was copied |
---|
415 | n/a | self.updated_files.append(res[0]) |
---|
416 | n/a | return res |
---|