comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/setuptools/wheel.py @ 68:5028fdace37b

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