Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/setuptools/wheel.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 """Wheels support.""" | |
2 | |
3 from distutils.util import get_platform | |
4 import email | |
5 import itertools | |
6 import os | |
7 import posixpath | |
8 import re | |
9 import zipfile | |
10 | |
11 import pkg_resources | |
12 import setuptools | |
13 from pkg_resources import parse_version | |
14 from setuptools.extern.packaging.utils import canonicalize_name | |
15 from setuptools.extern.six import PY3 | |
16 from setuptools import pep425tags | |
17 from setuptools.command.egg_info import write_requirements | |
18 | |
19 | |
20 __metaclass__ = type | |
21 | |
22 | |
23 WHEEL_NAME = re.compile( | |
24 r"""^(?P<project_name>.+?)-(?P<version>\d.*?) | |
25 ((-(?P<build>\d.*?))?-(?P<py_version>.+?)-(?P<abi>.+?)-(?P<platform>.+?) | |
26 )\.whl$""", | |
27 re.VERBOSE).match | |
28 | |
29 NAMESPACE_PACKAGE_INIT = '''\ | |
30 try: | |
31 __import__('pkg_resources').declare_namespace(__name__) | |
32 except ImportError: | |
33 __path__ = __import__('pkgutil').extend_path(__path__, __name__) | |
34 ''' | |
35 | |
36 | |
37 def unpack(src_dir, dst_dir): | |
38 '''Move everything under `src_dir` to `dst_dir`, and delete the former.''' | |
39 for dirpath, dirnames, filenames in os.walk(src_dir): | |
40 subdir = os.path.relpath(dirpath, src_dir) | |
41 for f in filenames: | |
42 src = os.path.join(dirpath, f) | |
43 dst = os.path.join(dst_dir, subdir, f) | |
44 os.renames(src, dst) | |
45 for n, d in reversed(list(enumerate(dirnames))): | |
46 src = os.path.join(dirpath, d) | |
47 dst = os.path.join(dst_dir, subdir, d) | |
48 if not os.path.exists(dst): | |
49 # Directory does not exist in destination, | |
50 # rename it and prune it from os.walk list. | |
51 os.renames(src, dst) | |
52 del dirnames[n] | |
53 # Cleanup. | |
54 for dirpath, dirnames, filenames in os.walk(src_dir, topdown=True): | |
55 assert not filenames | |
56 os.rmdir(dirpath) | |
57 | |
58 | |
59 class Wheel: | |
60 | |
61 def __init__(self, filename): | |
62 match = WHEEL_NAME(os.path.basename(filename)) | |
63 if match is None: | |
64 raise ValueError('invalid wheel name: %r' % filename) | |
65 self.filename = filename | |
66 for k, v in match.groupdict().items(): | |
67 setattr(self, k, v) | |
68 | |
69 def tags(self): | |
70 '''List tags (py_version, abi, platform) supported by this wheel.''' | |
71 return itertools.product( | |
72 self.py_version.split('.'), | |
73 self.abi.split('.'), | |
74 self.platform.split('.'), | |
75 ) | |
76 | |
77 def is_compatible(self): | |
78 '''Is the wheel is compatible with the current platform?''' | |
79 supported_tags = pep425tags.get_supported() | |
80 return next((True for t in self.tags() if t in supported_tags), False) | |
81 | |
82 def egg_name(self): | |
83 return pkg_resources.Distribution( | |
84 project_name=self.project_name, version=self.version, | |
85 platform=(None if self.platform == 'any' else get_platform()), | |
86 ).egg_name() + '.egg' | |
87 | |
88 def get_dist_info(self, zf): | |
89 # find the correct name of the .dist-info dir in the wheel file | |
90 for member in zf.namelist(): | |
91 dirname = posixpath.dirname(member) | |
92 if (dirname.endswith('.dist-info') and | |
93 canonicalize_name(dirname).startswith( | |
94 canonicalize_name(self.project_name))): | |
95 return dirname | |
96 raise ValueError("unsupported wheel format. .dist-info not found") | |
97 | |
98 def install_as_egg(self, destination_eggdir): | |
99 '''Install wheel as an egg directory.''' | |
100 with zipfile.ZipFile(self.filename) as zf: | |
101 self._install_as_egg(destination_eggdir, zf) | |
102 | |
103 def _install_as_egg(self, destination_eggdir, zf): | |
104 dist_basename = '%s-%s' % (self.project_name, self.version) | |
105 dist_info = self.get_dist_info(zf) | |
106 dist_data = '%s.data' % dist_basename | |
107 egg_info = os.path.join(destination_eggdir, 'EGG-INFO') | |
108 | |
109 self._convert_metadata(zf, destination_eggdir, dist_info, egg_info) | |
110 self._move_data_entries(destination_eggdir, dist_data) | |
111 self._fix_namespace_packages(egg_info, destination_eggdir) | |
112 | |
113 @staticmethod | |
114 def _convert_metadata(zf, destination_eggdir, dist_info, egg_info): | |
115 def get_metadata(name): | |
116 with zf.open(posixpath.join(dist_info, name)) as fp: | |
117 value = fp.read().decode('utf-8') if PY3 else fp.read() | |
118 return email.parser.Parser().parsestr(value) | |
119 | |
120 wheel_metadata = get_metadata('WHEEL') | |
121 # Check wheel format version is supported. | |
122 wheel_version = parse_version(wheel_metadata.get('Wheel-Version')) | |
123 wheel_v1 = ( | |
124 parse_version('1.0') <= wheel_version < parse_version('2.0dev0') | |
125 ) | |
126 if not wheel_v1: | |
127 raise ValueError( | |
128 'unsupported wheel format version: %s' % wheel_version) | |
129 # Extract to target directory. | |
130 os.mkdir(destination_eggdir) | |
131 zf.extractall(destination_eggdir) | |
132 # Convert metadata. | |
133 dist_info = os.path.join(destination_eggdir, dist_info) | |
134 dist = pkg_resources.Distribution.from_location( | |
135 destination_eggdir, dist_info, | |
136 metadata=pkg_resources.PathMetadata(destination_eggdir, dist_info), | |
137 ) | |
138 | |
139 # Note: Evaluate and strip markers now, | |
140 # as it's difficult to convert back from the syntax: | |
141 # foobar; "linux" in sys_platform and extra == 'test' | |
142 def raw_req(req): | |
143 req.marker = None | |
144 return str(req) | |
145 install_requires = list(sorted(map(raw_req, dist.requires()))) | |
146 extras_require = { | |
147 extra: sorted( | |
148 req | |
149 for req in map(raw_req, dist.requires((extra,))) | |
150 if req not in install_requires | |
151 ) | |
152 for extra in dist.extras | |
153 } | |
154 os.rename(dist_info, egg_info) | |
155 os.rename( | |
156 os.path.join(egg_info, 'METADATA'), | |
157 os.path.join(egg_info, 'PKG-INFO'), | |
158 ) | |
159 setup_dist = setuptools.Distribution( | |
160 attrs=dict( | |
161 install_requires=install_requires, | |
162 extras_require=extras_require, | |
163 ), | |
164 ) | |
165 write_requirements( | |
166 setup_dist.get_command_obj('egg_info'), | |
167 None, | |
168 os.path.join(egg_info, 'requires.txt'), | |
169 ) | |
170 | |
171 @staticmethod | |
172 def _move_data_entries(destination_eggdir, dist_data): | |
173 """Move data entries to their correct location.""" | |
174 dist_data = os.path.join(destination_eggdir, dist_data) | |
175 dist_data_scripts = os.path.join(dist_data, 'scripts') | |
176 if os.path.exists(dist_data_scripts): | |
177 egg_info_scripts = os.path.join( | |
178 destination_eggdir, 'EGG-INFO', 'scripts') | |
179 os.mkdir(egg_info_scripts) | |
180 for entry in os.listdir(dist_data_scripts): | |
181 # Remove bytecode, as it's not properly handled | |
182 # during easy_install scripts install phase. | |
183 if entry.endswith('.pyc'): | |
184 os.unlink(os.path.join(dist_data_scripts, entry)) | |
185 else: | |
186 os.rename( | |
187 os.path.join(dist_data_scripts, entry), | |
188 os.path.join(egg_info_scripts, entry), | |
189 ) | |
190 os.rmdir(dist_data_scripts) | |
191 for subdir in filter(os.path.exists, ( | |
192 os.path.join(dist_data, d) | |
193 for d in ('data', 'headers', 'purelib', 'platlib') | |
194 )): | |
195 unpack(subdir, destination_eggdir) | |
196 if os.path.exists(dist_data): | |
197 os.rmdir(dist_data) | |
198 | |
199 @staticmethod | |
200 def _fix_namespace_packages(egg_info, destination_eggdir): | |
201 namespace_packages = os.path.join( | |
202 egg_info, 'namespace_packages.txt') | |
203 if os.path.exists(namespace_packages): | |
204 with open(namespace_packages) as fp: | |
205 namespace_packages = fp.read().split() | |
206 for mod in namespace_packages: | |
207 mod_dir = os.path.join(destination_eggdir, *mod.split('.')) | |
208 mod_init = os.path.join(mod_dir, '__init__.py') | |
209 if os.path.exists(mod_dir) and not os.path.exists(mod_init): | |
210 with open(mod_init, 'w') as fp: | |
211 fp.write(NAMESPACE_PACKAGE_INIT) |