annotate Lib/packaging/command/install_lib.py @ 73323:dad02a080bbc

Improve byte-compilation in packaging to be independent of -O or -B. The code I fixed to comply with PEP 3147 still had one bug: When run under python -O, some paths for pyc files would be pyo, because I called imp.cache_from_source without explicit debug_override argument in some places, and under -O that would return .pyo (this is well explained in the imp docs). Now all code (util.byte_compile, build_py, install_lib) can create .pyo files according to options given by users, without interference from the calling Python’s own optimize mode. On a related topic, I also removed the code that prevented byte compilation under python -B. The rationale is that packaging gives control over the creation of pyc files to the user with its own explicit option, and the behavior should not be changed if the calling Python happens to run with -B for whatever reason. I will argue that this is a bug fix and ask to be allowed to backport this change to distutils. Finally, I moved one nugget of information about the --compile and --optimize options from the source into the doc. It clears up a misunderstanding that I (and maybe other people) had.
author Éric Araujo <merwok@netwok.org>
date Thu, 03 Nov 2011 05:08:28 +0100
parents 1cc4d822123b
children c10946a17420
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
1 """Install all modules (extensions and pure Python)."""
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
2
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
3 import os
73003
1cc4d822123b More fixes for PEP 3147 compliance in packaging (#11254)
Éric Araujo <merwok@netwok.org>
parents: 72401
diff changeset
4 import imp
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
5 import logging
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
6
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
7 from packaging import logger
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
8 from packaging.command.cmd import Command
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
9 from packaging.errors import PackagingOptionError
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
10
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
11
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
12 # Extension for Python source files.
73323
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
13 # XXX dead code? most of the codebase checks for literal '.py'
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
14 if hasattr(os, 'extsep'):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
15 PYTHON_SOURCE_EXTENSION = os.extsep + "py"
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
16 else:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
17 PYTHON_SOURCE_EXTENSION = ".py"
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
18
73323
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
19
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
20 class install_lib(Command):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
21
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
22 description = "install all modules (extensions and pure Python)"
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
23
73323
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
24 # The options for controlling byte compilations are two independent sets:
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
25 # 'compile' is strictly boolean, and only decides whether to
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
26 # generate .pyc files. 'optimize' is three-way (0, 1, or 2), and
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
27 # decides both whether to generate .pyo files and what level of
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
28 # optimization to use.
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
29
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
30 user_options = [
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
31 ('install-dir=', 'd', "directory to install to"),
73323
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
32 ('build-dir=', 'b', "build directory (where to install from)"),
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
33 ('force', 'f', "force installation (overwrite existing files)"),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
34 ('compile', 'c', "compile .py to .pyc [default]"),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
35 ('no-compile', None, "don't compile .py files"),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
36 ('optimize=', 'O',
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
37 "also compile with optimization: -O1 for \"python -O\", "
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
38 "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
39 ('skip-build', None, "skip the build steps"),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
40 ]
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
41
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
42 boolean_options = ['force', 'compile', 'skip-build']
73323
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
43
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
44 negative_opt = {'no-compile': 'compile'}
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
45
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
46 def initialize_options(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
47 # let the 'install_dist' command dictate our installation directory
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
48 self.install_dir = None
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
49 self.build_dir = None
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
50 self.force = False
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
51 self.compile = None
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
52 self.optimize = None
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
53 self.skip_build = None
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
54
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
55 def finalize_options(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
56 # Get all the information we need to install pure Python modules
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
57 # from the umbrella 'install_dist' command -- build (source) directory,
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
58 # install (target) directory, and whether to compile .py files.
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
59 self.set_undefined_options('install_dist',
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
60 ('build_lib', 'build_dir'),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
61 ('install_lib', 'install_dir'),
73323
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
62 'force', 'compile', 'optimize',
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
63 'skip_build')
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
64
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
65 if self.compile is None:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
66 self.compile = True
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
67 if self.optimize is None:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
68 self.optimize = 0
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
69
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
70 if not isinstance(self.optimize, int):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
71 try:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
72 self.optimize = int(self.optimize)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
73 if self.optimize not in (0, 1, 2):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
74 raise AssertionError
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
75 except (ValueError, AssertionError):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
76 raise PackagingOptionError("optimize must be 0, 1, or 2")
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
77
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
78 def run(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
79 # Make sure we have built everything we need first
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
80 self.build()
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
81
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
82 # Install everything: simply dump the entire contents of the build
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
83 # directory to the installation directory (that's the beauty of
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
84 # having a build directory!)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
85 outfiles = self.install()
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
86
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
87 # (Optionally) compile .py to .pyc
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
88 if outfiles is not None and self.distribution.has_pure_modules():
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
89 self.byte_compile(outfiles)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
90
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
91 # -- Top-level worker functions ------------------------------------
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
92 # (called from 'run()')
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
93
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
94 def build(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
95 if not self.skip_build:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
96 if self.distribution.has_pure_modules():
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
97 self.run_command('build_py')
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
98 if self.distribution.has_ext_modules():
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
99 self.run_command('build_ext')
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
100
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
101 def install(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
102 if os.path.isdir(self.build_dir):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
103 outfiles = self.copy_tree(self.build_dir, self.install_dir)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
104 else:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
105 logger.warning(
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
106 '%s: %r does not exist -- no Python modules to install',
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
107 self.get_command_name(), self.build_dir)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
108 return
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
109 return outfiles
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
110
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
111 def byte_compile(self, files):
72178
28e4cd8fd864 Clean up packaging.util: add __all__, remove some unused functions.
Éric Araujo <merwok@netwok.org>
parents: 70194
diff changeset
112 from packaging.util import byte_compile # FIXME use compileall
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
113
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
114 # Get the "--root" directory supplied to the "install_dist" command,
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
115 # and use it as a prefix to strip off the purported filename
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
116 # encoded in bytecode files. This is far from complete, but it
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
117 # should at least generate usable bytecode in RPM distributions.
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
118 install_root = self.get_finalized_command('install_dist').root
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
119
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
120 # Temporary kludge until we remove the verbose arguments and use
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
121 # logging everywhere
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
122 verbose = logger.getEffectiveLevel() >= logging.DEBUG
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
123
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
124 if self.compile:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
125 byte_compile(files, optimize=0,
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
126 force=self.force, prefix=install_root,
73323
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
127 verbose=verbose, dry_run=self.dry_run)
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
128 if self.optimize > 0:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
129 byte_compile(files, optimize=self.optimize,
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
130 force=self.force, prefix=install_root,
73323
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
131 verbose=verbose, dry_run=self.dry_run)
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
132
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
133 # -- Utility methods -----------------------------------------------
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
134
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
135 def _mutate_outputs(self, has_any, build_cmd, cmd_option, output_dir):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
136 if not has_any:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
137 return []
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
138
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
139 build_cmd = self.get_finalized_command(build_cmd)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
140 build_files = build_cmd.get_outputs()
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
141 build_dir = getattr(build_cmd, cmd_option)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
142
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
143 prefix_len = len(build_dir) + len(os.sep)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
144 outputs = []
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
145 for file in build_files:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
146 outputs.append(os.path.join(output_dir, file[prefix_len:]))
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
147
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
148 return outputs
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
149
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
150 def _bytecode_filenames(self, py_filenames):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
151 bytecode_files = []
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
152 for py_file in py_filenames:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
153 # Since build_py handles package data installation, the
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
154 # list of outputs can contain more than just .py files.
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
155 # Make sure we only report bytecode for the .py files.
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
156 ext = os.path.splitext(os.path.normcase(py_file))[1]
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
157 if ext != PYTHON_SOURCE_EXTENSION:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
158 continue
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
159 if self.compile:
73323
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
160 bytecode_files.append(imp.cache_from_source(
dad02a080bbc Improve byte-compilation in packaging to be independent of -O or -B.
Éric Araujo <merwok@netwok.org>
parents: 73003
diff changeset
161 py_file, debug_override=True))
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
162 if self.optimize > 0:
73003
1cc4d822123b More fixes for PEP 3147 compliance in packaging (#11254)
Éric Araujo <merwok@netwok.org>
parents: 72401
diff changeset
163 bytecode_files.append(imp.cache_from_source(
1cc4d822123b More fixes for PEP 3147 compliance in packaging (#11254)
Éric Araujo <merwok@netwok.org>
parents: 72401
diff changeset
164 py_file, debug_override=False))
70194
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
165
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
166 return bytecode_files
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
167
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
168 # -- External interface --------------------------------------------
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
169 # (called by outsiders)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
170
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
171 def get_outputs(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
172 """Return the list of files that would be installed if this command
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
173 were actually run. Not affected by the "dry-run" flag or whether
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
174 modules have actually been built yet.
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
175 """
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
176 pure_outputs = \
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
177 self._mutate_outputs(self.distribution.has_pure_modules(),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
178 'build_py', 'build_lib',
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
179 self.install_dir)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
180 if self.compile:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
181 bytecode_outputs = self._bytecode_filenames(pure_outputs)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
182 else:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
183 bytecode_outputs = []
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
184
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
185 ext_outputs = \
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
186 self._mutate_outputs(self.distribution.has_ext_modules(),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
187 'build_ext', 'build_lib',
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
188 self.install_dir)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
189
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
190 return pure_outputs + bytecode_outputs + ext_outputs
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
191
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
192 def get_inputs(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
193 """Get the list of files that are input to this command, ie. the
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
194 files that get installed as they are named in the build tree.
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
195 The files in this list correspond one-to-one to the output
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
196 filenames returned by 'get_outputs()'.
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
197 """
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
198 inputs = []
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
199
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
200 if self.distribution.has_pure_modules():
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
201 build_py = self.get_finalized_command('build_py')
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
202 inputs.extend(build_py.get_outputs())
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
203
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
204 if self.distribution.has_ext_modules():
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
205 build_ext = self.get_finalized_command('build_ext')
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
206 inputs.extend(build_ext.get_outputs())
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
207
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
208 return inputs