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