|
| 1 | + |
| 2 | +import argparse |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +from itertools import product |
| 8 | +from os.path import (abspath, basename, dirname, isfile, join as pjoin, splitext) |
| 9 | + |
| 10 | +from auditwheel.tools import unique_by_index |
| 11 | +from auditwheel.wheeltools import _dist_info_dir, InWheelCtx |
| 12 | +from wheel.pkginfo import read_pkg_info, write_pkg_info |
| 13 | +from wheel.install import WHEEL_INFO_RE |
| 14 | + |
| 15 | +logger = logging.getLogger(splitext(basename(__file__))[0]) |
| 16 | + |
| 17 | + |
| 18 | +def _to_generic_pyver(pyver_tags): |
| 19 | + """Convert from CPython implementation to generic python version tags |
| 20 | +
|
| 21 | + Convert each CPython version tag to the equivalent generic tag. |
| 22 | +
|
| 23 | + For example:: |
| 24 | +
|
| 25 | + cp35 -> py3 |
| 26 | + cp27 -> py2 |
| 27 | +
|
| 28 | + See https://www.python.org/dev/peps/pep-0425/#python-tag |
| 29 | + """ |
| 30 | + return ['py%s' % tag[2] if tag.startswith('cp') else tag for tag in pyver_tags] |
| 31 | + |
| 32 | + |
| 33 | +def _convert_to_generic_platform_wheel(wheel_ctx): |
| 34 | + """Switch to generic python tags and remove ABI tags from a wheel |
| 35 | +
|
| 36 | + Convert implementation specific python tags to their generic equivalent and |
| 37 | + remove all ABI tags from wheel_ctx's filename and ``WHEEL`` file. |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | + wheel_ctx : InWheelCtx |
| 42 | + An open wheel context |
| 43 | + """ |
| 44 | + |
| 45 | + abi_tags = ['none'] |
| 46 | + |
| 47 | + info_fname = pjoin(_dist_info_dir(wheel_ctx.path), 'WHEEL') |
| 48 | + info = read_pkg_info(info_fname) |
| 49 | + |
| 50 | + # Check what tags we have |
| 51 | + if wheel_ctx.out_wheel is not None: |
| 52 | + out_dir = dirname(wheel_ctx.out_wheel) |
| 53 | + wheel_fname = basename(wheel_ctx.out_wheel) |
| 54 | + else: |
| 55 | + out_dir = '.' |
| 56 | + wheel_fname = basename(wheel_ctx.in_wheel) |
| 57 | + |
| 58 | + # Update wheel filename |
| 59 | + parsed_fname = WHEEL_INFO_RE(wheel_fname) |
| 60 | + fparts = parsed_fname.groupdict() |
| 61 | + original_platform_tags = fparts['plat'].split('.') |
| 62 | + |
| 63 | + original_abi_tags = fparts['abi'].split('.') |
| 64 | + logger.debug('Previous ABI tags: %s', ', '.join(original_abi_tags)) |
| 65 | + if abi_tags != original_abi_tags: |
| 66 | + logger.debug('New ABI tags ....: %s', ', '.join(abi_tags)) |
| 67 | + fparts['abi'] = '.'.join(abi_tags) |
| 68 | + else: |
| 69 | + logger.debug('No ABI tags change needed.') |
| 70 | + |
| 71 | + original_pyver_tags = fparts['pyver'].split('.') |
| 72 | + logger.debug('Previous pyver tags: %s', ', '.join(original_pyver_tags)) |
| 73 | + pyver_tags = _to_generic_pyver(original_pyver_tags) |
| 74 | + if pyver_tags != original_pyver_tags: |
| 75 | + logger.debug('New pyver tags ....: %s', ', '.join(pyver_tags)) |
| 76 | + fparts['pyver'] = '.'.join(pyver_tags) |
| 77 | + else: |
| 78 | + logger.debug('No pyver change needed.') |
| 79 | + |
| 80 | + _, ext = splitext(wheel_fname) |
| 81 | + fparts['ext'] = ext |
| 82 | + out_wheel_fname = "{namever}-{pyver}-{abi}-{plat}{ext}".format(**fparts) |
| 83 | + |
| 84 | + logger.info('Previous filename: %s', wheel_fname) |
| 85 | + if out_wheel_fname != wheel_fname: |
| 86 | + logger.info('New filename ....: %s', out_wheel_fname) |
| 87 | + else: |
| 88 | + logger.info('No filename change needed.') |
| 89 | + |
| 90 | + out_wheel = pjoin(out_dir, out_wheel_fname) |
| 91 | + |
| 92 | + # Update wheel tags |
| 93 | + in_info_tags = [tag for name, tag in info.items() if name == 'Tag'] |
| 94 | + logger.info('Previous WHEEL info tags: %s', ', '.join(in_info_tags)) |
| 95 | + |
| 96 | + # Python version, C-API version combinations |
| 97 | + pyc_apis = [] |
| 98 | + for tag in in_info_tags: |
| 99 | + py_ver = '.'.join(_to_generic_pyver(tag.split('-')[0].split('.'))) |
| 100 | + abi = 'none' |
| 101 | + pyc_apis.append('-'.join([py_ver, abi])) |
| 102 | + # unique Python version, C-API version combinations |
| 103 | + pyc_apis = unique_by_index(pyc_apis) |
| 104 | + |
| 105 | + # Set tags for each Python version, C-API combination |
| 106 | + updated_tags = ['-'.join(tup) for tup in product(pyc_apis, original_platform_tags)] |
| 107 | + |
| 108 | + if updated_tags != in_info_tags: |
| 109 | + del info['Tag'] |
| 110 | + for tag in updated_tags: |
| 111 | + info.add_header('Tag', tag) |
| 112 | + |
| 113 | + logger.info('New WHEEL info tags ....: %s', ', '.join(info.get_all('Tag'))) |
| 114 | + write_pkg_info(info_fname, info) |
| 115 | + else: |
| 116 | + logger.info('No WHEEL info change needed.') |
| 117 | + return out_wheel |
| 118 | + |
| 119 | + |
| 120 | +def convert_to_generic_platform_wheel(wheel_path, out_dir='./dist/', remove_original=False, verbose=0): |
| 121 | + logging.disable(logging.NOTSET) |
| 122 | + if verbose >= 1: |
| 123 | + logging.basicConfig(level=logging.DEBUG) |
| 124 | + else: |
| 125 | + logging.basicConfig(level=logging.INFO) |
| 126 | + |
| 127 | + wheel_fname = basename(wheel_path) |
| 128 | + out_dir = abspath(out_dir) |
| 129 | + |
| 130 | + with InWheelCtx(wheel_path) as ctx: |
| 131 | + ctx.out_wheel = pjoin(out_dir, wheel_fname) |
| 132 | + ctx.out_wheel = _convert_to_generic_platform_wheel(ctx) |
| 133 | + |
| 134 | + if remove_original: |
| 135 | + logger.info('Removed original wheel %s' % wheel_path) |
| 136 | + os.remove(wheel_path) |
| 137 | + |
| 138 | + |
| 139 | +def main(): |
| 140 | + p = argparse.ArgumentParser(description='Convert wheel to be independent of python implementation and ABI') |
| 141 | + p.set_defaults(prog=basename(sys.argv[0])) |
| 142 | + p.add_argument("-v", |
| 143 | + "--verbose", |
| 144 | + action='count', |
| 145 | + dest='verbose', |
| 146 | + default=0, |
| 147 | + help='Give more output. Option is additive') |
| 148 | + |
| 149 | + p.add_argument('WHEEL_FILE', help='Path to wheel file.') |
| 150 | + p.add_argument('-w', |
| 151 | + '--wheel-dir', |
| 152 | + dest='WHEEL_DIR', |
| 153 | + type=abspath, |
| 154 | + help='Directory to store updated wheels (default: "dist/")', |
| 155 | + default='dist/') |
| 156 | + p.add_argument("-r", |
| 157 | + "--remove-original", |
| 158 | + dest='remove_original', |
| 159 | + action='store_true', |
| 160 | + help='Remove original wheel') |
| 161 | + |
| 162 | + args = p.parse_args() |
| 163 | + |
| 164 | + if not isfile(args.WHEEL_FILE): |
| 165 | + p.error('cannot access %s. No such file' % args.WHEEL_FILE) |
| 166 | + |
| 167 | + convert_to_generic_platform_wheel(args.WHEEL_FILE, args.WHEEL_DIR, args.remove_original, args.verbose) |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + main() |
0 commit comments