Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/setuptools/command/build_py.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author | bcclaywell |
---|---|
date | Mon, 12 Oct 2015 17:43:33 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d67268158946 |
---|---|
1 from glob import glob | |
2 from distutils.util import convert_path | |
3 import distutils.command.build_py as orig | |
4 import os | |
5 import sys | |
6 import fnmatch | |
7 import textwrap | |
8 | |
9 try: | |
10 from setuptools.lib2to3_ex import Mixin2to3 | |
11 except ImportError: | |
12 class Mixin2to3: | |
13 def run_2to3(self, files, doctests=True): | |
14 "do nothing" | |
15 | |
16 | |
17 class build_py(orig.build_py, Mixin2to3): | |
18 """Enhanced 'build_py' command that includes data files with packages | |
19 | |
20 The data files are specified via a 'package_data' argument to 'setup()'. | |
21 See 'setuptools.dist.Distribution' for more details. | |
22 | |
23 Also, this version of the 'build_py' command allows you to specify both | |
24 'py_modules' and 'packages' in the same setup operation. | |
25 """ | |
26 | |
27 def finalize_options(self): | |
28 orig.build_py.finalize_options(self) | |
29 self.package_data = self.distribution.package_data | |
30 self.exclude_package_data = (self.distribution.exclude_package_data or | |
31 {}) | |
32 if 'data_files' in self.__dict__: | |
33 del self.__dict__['data_files'] | |
34 self.__updated_files = [] | |
35 self.__doctests_2to3 = [] | |
36 | |
37 def run(self): | |
38 """Build modules, packages, and copy data files to build directory""" | |
39 if not self.py_modules and not self.packages: | |
40 return | |
41 | |
42 if self.py_modules: | |
43 self.build_modules() | |
44 | |
45 if self.packages: | |
46 self.build_packages() | |
47 self.build_package_data() | |
48 | |
49 self.run_2to3(self.__updated_files, False) | |
50 self.run_2to3(self.__updated_files, True) | |
51 self.run_2to3(self.__doctests_2to3, True) | |
52 | |
53 # Only compile actual .py files, using our base class' idea of what our | |
54 # output files are. | |
55 self.byte_compile(orig.build_py.get_outputs(self, include_bytecode=0)) | |
56 | |
57 def __getattr__(self, attr): | |
58 if attr == 'data_files': # lazily compute data files | |
59 self.data_files = files = self._get_data_files() | |
60 return files | |
61 return orig.build_py.__getattr__(self, attr) | |
62 | |
63 def build_module(self, module, module_file, package): | |
64 outfile, copied = orig.build_py.build_module(self, module, module_file, | |
65 package) | |
66 if copied: | |
67 self.__updated_files.append(outfile) | |
68 return outfile, copied | |
69 | |
70 def _get_data_files(self): | |
71 """Generate list of '(package,src_dir,build_dir,filenames)' tuples""" | |
72 self.analyze_manifest() | |
73 data = [] | |
74 for package in self.packages or (): | |
75 # Locate package source directory | |
76 src_dir = self.get_package_dir(package) | |
77 | |
78 # Compute package build directory | |
79 build_dir = os.path.join(*([self.build_lib] + package.split('.'))) | |
80 | |
81 # Length of path to strip from found files | |
82 plen = len(src_dir) + 1 | |
83 | |
84 # Strip directory from globbed filenames | |
85 filenames = [ | |
86 file[plen:] for file in self.find_data_files(package, src_dir) | |
87 ] | |
88 data.append((package, src_dir, build_dir, filenames)) | |
89 return data | |
90 | |
91 def find_data_files(self, package, src_dir): | |
92 """Return filenames for package's data files in 'src_dir'""" | |
93 globs = (self.package_data.get('', []) | |
94 + self.package_data.get(package, [])) | |
95 files = self.manifest_files.get(package, [])[:] | |
96 for pattern in globs: | |
97 # Each pattern has to be converted to a platform-specific path | |
98 files.extend(glob(os.path.join(src_dir, convert_path(pattern)))) | |
99 return self.exclude_data_files(package, src_dir, files) | |
100 | |
101 def build_package_data(self): | |
102 """Copy data files into build directory""" | |
103 for package, src_dir, build_dir, filenames in self.data_files: | |
104 for filename in filenames: | |
105 target = os.path.join(build_dir, filename) | |
106 self.mkpath(os.path.dirname(target)) | |
107 srcfile = os.path.join(src_dir, filename) | |
108 outf, copied = self.copy_file(srcfile, target) | |
109 srcfile = os.path.abspath(srcfile) | |
110 if (copied and | |
111 srcfile in self.distribution.convert_2to3_doctests): | |
112 self.__doctests_2to3.append(outf) | |
113 | |
114 def analyze_manifest(self): | |
115 self.manifest_files = mf = {} | |
116 if not self.distribution.include_package_data: | |
117 return | |
118 src_dirs = {} | |
119 for package in self.packages or (): | |
120 # Locate package source directory | |
121 src_dirs[assert_relative(self.get_package_dir(package))] = package | |
122 | |
123 self.run_command('egg_info') | |
124 ei_cmd = self.get_finalized_command('egg_info') | |
125 for path in ei_cmd.filelist.files: | |
126 d, f = os.path.split(assert_relative(path)) | |
127 prev = None | |
128 oldf = f | |
129 while d and d != prev and d not in src_dirs: | |
130 prev = d | |
131 d, df = os.path.split(d) | |
132 f = os.path.join(df, f) | |
133 if d in src_dirs: | |
134 if path.endswith('.py') and f == oldf: | |
135 continue # it's a module, not data | |
136 mf.setdefault(src_dirs[d], []).append(path) | |
137 | |
138 def get_data_files(self): | |
139 pass # Lazily compute data files in _get_data_files() function. | |
140 | |
141 def check_package(self, package, package_dir): | |
142 """Check namespace packages' __init__ for declare_namespace""" | |
143 try: | |
144 return self.packages_checked[package] | |
145 except KeyError: | |
146 pass | |
147 | |
148 init_py = orig.build_py.check_package(self, package, package_dir) | |
149 self.packages_checked[package] = init_py | |
150 | |
151 if not init_py or not self.distribution.namespace_packages: | |
152 return init_py | |
153 | |
154 for pkg in self.distribution.namespace_packages: | |
155 if pkg == package or pkg.startswith(package + '.'): | |
156 break | |
157 else: | |
158 return init_py | |
159 | |
160 f = open(init_py, 'rbU') | |
161 if 'declare_namespace'.encode() not in f.read(): | |
162 from distutils.errors import DistutilsError | |
163 | |
164 raise DistutilsError( | |
165 "Namespace package problem: %s is a namespace package, but " | |
166 "its\n__init__.py does not call declare_namespace()! Please " | |
167 'fix it.\n(See the setuptools manual under ' | |
168 '"Namespace Packages" for details.)\n"' % (package,) | |
169 ) | |
170 f.close() | |
171 return init_py | |
172 | |
173 def initialize_options(self): | |
174 self.packages_checked = {} | |
175 orig.build_py.initialize_options(self) | |
176 | |
177 def get_package_dir(self, package): | |
178 res = orig.build_py.get_package_dir(self, package) | |
179 if self.distribution.src_root is not None: | |
180 return os.path.join(self.distribution.src_root, res) | |
181 return res | |
182 | |
183 def exclude_data_files(self, package, src_dir, files): | |
184 """Filter filenames for package's data files in 'src_dir'""" | |
185 globs = (self.exclude_package_data.get('', []) | |
186 + self.exclude_package_data.get(package, [])) | |
187 bad = [] | |
188 for pattern in globs: | |
189 bad.extend( | |
190 fnmatch.filter( | |
191 files, os.path.join(src_dir, convert_path(pattern)) | |
192 ) | |
193 ) | |
194 bad = dict.fromkeys(bad) | |
195 seen = {} | |
196 return [ | |
197 f for f in files if f not in bad | |
198 and f not in seen and seen.setdefault(f, 1) # ditch dupes | |
199 ] | |
200 | |
201 | |
202 def assert_relative(path): | |
203 if not os.path.isabs(path): | |
204 return path | |
205 from distutils.errors import DistutilsSetupError | |
206 | |
207 msg = textwrap.dedent(""" | |
208 Error: setup script specifies an absolute path: | |
209 | |
210 %s | |
211 | |
212 setup() arguments must *always* be /-separated paths relative to the | |
213 setup.py directory, *never* absolute paths. | |
214 """).lstrip() % path | |
215 raise DistutilsSetupError(msg) |