annotate Lib/packaging/command/install_data.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 1a31c3be88b1
children
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 platform-independent data files."""
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 # Contributed by Bastian Kleineidam
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
4
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
5 import os
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
6 from shutil import Error
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
7 from sysconfig import get_paths, format_value
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
8 from packaging import logger
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
9 from packaging.util import convert_path
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
10 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
11
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
12
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
13 class install_data(Command):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
14
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
15 description = "install platform-independent data files"
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
16
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
17 user_options = [
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
18 ('install-dir=', 'd',
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
19 "base directory for installing data files "
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
20 "(default: installation base dir)"),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
21 ('root=', None,
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
22 "install everything relative to this alternate root directory"),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
23 ('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
24 ]
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
25
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
26 boolean_options = ['force']
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
27
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
28 def initialize_options(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
29 self.install_dir = None
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
30 self.outfiles = []
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
31 self.data_files_out = []
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
32 self.root = None
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
33 self.force = False
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
34 self.data_files = self.distribution.data_files
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
35 self.warn_dir = True
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
36
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
37 def finalize_options(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
38 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
39 ('install_data', 'install_dir'),
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
40 'root', 'force')
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 def run(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
43 self.mkpath(self.install_dir)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
44 for _file in self.data_files.items():
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
45 destination = convert_path(self.expand_categories(_file[1]))
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
46 dir_dest = os.path.abspath(os.path.dirname(destination))
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
47
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
48 self.mkpath(dir_dest)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
49 try:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
50 out = self.copy_file(_file[0], dir_dest)[0]
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
51 except Error as e:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
52 logger.warning('%s: %s', self.get_command_name(), e)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
53 out = destination
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 self.outfiles.append(out)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
56 self.data_files_out.append((_file[0], destination))
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
57
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
58 def expand_categories(self, path_with_categories):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
59 local_vars = get_paths()
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
60 local_vars['distribution.name'] = self.distribution.metadata['Name']
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
61 expanded_path = format_value(path_with_categories, local_vars)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
62 expanded_path = format_value(expanded_path, local_vars)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
63 if '{' in expanded_path and '}' in expanded_path:
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
64 logger.warning(
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
65 '%s: unable to expand %s, some categories may be missing',
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
66 self.get_command_name(), path_with_categories)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
67 return expanded_path
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
68
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
69 def get_source_files(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
70 return list(self.data_files)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
71
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
72 def get_inputs(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
73 return list(self.data_files)
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
74
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
75 def get_outputs(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
76 return self.outfiles
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 get_resources_out(self):
1a31c3be88b1 initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff changeset
79 return self.data_files_out