Skip to content

Commit 104b0ca

Browse files
committed
chore: use same convert_to_generic_platform_wheel.py script as cmake
This prepares for `universal2` wheels and allows to create `py2.py3` wheels
1 parent baa2d42 commit 104b0ca

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

scripts/convert_to_generic_platform_wheel.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _to_generic_pyver(pyver_tags):
4141
return ['py%s' % tag[2] if tag.startswith('cp') else tag for tag in pyver_tags]
4242

4343

44-
def _convert_to_generic_platform_wheel(wheel_ctx):
44+
def _convert_to_generic_platform_wheel(wheel_ctx, py2_py3, additional_platforms):
4545
"""Switch to generic python tags and remove ABI tags from a wheel
4646
4747
Convert implementation specific python tags to their generic equivalent and
@@ -51,6 +51,10 @@ def _convert_to_generic_platform_wheel(wheel_ctx):
5151
----------
5252
wheel_ctx : InWheelCtx
5353
An open wheel context
54+
py2_py3: Bool
55+
Whether the pyver tag shall be py2.py3 or just the one inferred from the wheel name
56+
additional_platforms : Optional[Iterable[str]]
57+
An optional iterable of additional platform to add to the wheel
5458
"""
5559

5660
abi_tags = ['none']
@@ -69,7 +73,14 @@ def _convert_to_generic_platform_wheel(wheel_ctx):
6973

7074
# Update wheel filename
7175
fparts = wf.parsed_filename.groupdict()
72-
original_platform_tags = fparts['plat'].split('.')
76+
platform_tags = fparts['plat'].split('.')
77+
logger.debug('Previous platform tags: %s', ', '.join(platform_tags))
78+
if additional_platforms:
79+
platform_tags = list(sorted(set(platform_tags + [p for p in additional_platforms])))
80+
fparts['plat'] = '.'.join(platform_tags)
81+
logger.debug('New platform tags ....: %s', ', '.join(platform_tags))
82+
else:
83+
logger.debug('No platform tags change needed.')
7384

7485
original_abi_tags = fparts['abi'].split('.')
7586
logger.debug('Previous ABI tags: %s', ', '.join(original_abi_tags))
@@ -82,6 +93,10 @@ def _convert_to_generic_platform_wheel(wheel_ctx):
8293
original_pyver_tags = fparts['pyver'].split('.')
8394
logger.debug('Previous pyver tags: %s', ', '.join(original_pyver_tags))
8495
pyver_tags = _to_generic_pyver(original_pyver_tags)
96+
if py2_py3:
97+
if len(set(["py2", "py3"]) & set(pyver_tags)) == 0:
98+
raise ValueError("pyver_tags does not contain py2 nor py3")
99+
pyver_tags = list(sorted(set(pyver_tags + ["py2", "py3"])))
85100
if pyver_tags != original_pyver_tags:
86101
logger.debug('New pyver tags ....: %s', ', '.join(pyver_tags))
87102
fparts['pyver'] = '.'.join(pyver_tags)
@@ -106,15 +121,14 @@ def _convert_to_generic_platform_wheel(wheel_ctx):
106121

107122
# Python version, C-API version combinations
108123
pyc_apis = []
109-
for tag in in_info_tags:
110-
py_ver = '.'.join(_to_generic_pyver(tag.split('-')[0].split('.')))
124+
for py_ver in pyver_tags:
111125
abi = 'none'
112126
pyc_apis.append('-'.join([py_ver, abi]))
113127
# unique Python version, C-API version combinations
114128
pyc_apis = unique_by_index(pyc_apis)
115129

116130
# Set tags for each Python version, C-API combination
117-
updated_tags = ['-'.join(tup) for tup in product(pyc_apis, original_platform_tags)]
131+
updated_tags = ['-'.join(tup) for tup in product(pyc_apis, platform_tags)]
118132

119133
if updated_tags != in_info_tags:
120134
del info['Tag']
@@ -128,7 +142,8 @@ def _convert_to_generic_platform_wheel(wheel_ctx):
128142
return out_wheel
129143

130144

131-
def convert_to_generic_platform_wheel(wheel_path, out_dir='./dist/', remove_original=False, verbose=0):
145+
def convert_to_generic_platform_wheel(wheel_path, out_dir='./dist/', remove_original=False, verbose=0,
146+
py2_py3=False, additional_platforms=None):
132147
logging.disable(logging.NOTSET)
133148
if verbose >= 1:
134149
logging.basicConfig(level=logging.DEBUG)
@@ -140,7 +155,7 @@ def convert_to_generic_platform_wheel(wheel_path, out_dir='./dist/', remove_orig
140155

141156
with InWheelCtx(wheel_path) as ctx:
142157
ctx.out_wheel = pjoin(out_dir, wheel_fname)
143-
ctx.out_wheel = _convert_to_generic_platform_wheel(ctx)
158+
ctx.out_wheel = _convert_to_generic_platform_wheel(ctx, py2_py3, additional_platforms)
144159

145160
if remove_original:
146161
logger.info('Removed original wheel %s' % wheel_path)
@@ -169,13 +184,23 @@ def main():
169184
dest='remove_original',
170185
action='store_true',
171186
help='Remove original wheel')
187+
p.add_argument("--py2-py3",
188+
dest='py2_py3',
189+
action='store_true',
190+
help='Remove original wheel')
191+
p.add_argument("-p",
192+
"--add-platform",
193+
dest='additional_platforms',
194+
action="append",
195+
help='Add a platform tag')
172196

173197
args = p.parse_args()
174198

175199
if not isfile(args.WHEEL_FILE):
176200
p.error('cannot access %s. No such file' % args.WHEEL_FILE)
177201

178-
convert_to_generic_platform_wheel(args.WHEEL_FILE, args.WHEEL_DIR, args.remove_original, args.verbose)
202+
convert_to_generic_platform_wheel(args.WHEEL_FILE, args.WHEEL_DIR, args.remove_original, args.verbose,
203+
args.py2_py3, args.additional_platforms)
179204

180205

181206
if __name__ == '__main__':

0 commit comments

Comments
 (0)