|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import base64 |
| 4 | +import hashlib |
| 5 | +import os.path |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import tempfile |
| 9 | +import zipapp |
| 10 | +import zipfile |
| 11 | + |
| 12 | +HERE = os.path.dirname(os.path.realpath(__file__)) |
| 13 | +IMG = 'make-pre-commit-zipapp' |
| 14 | + |
| 15 | + |
| 16 | +def _msg(s: str) -> None: |
| 17 | + print(f'\033[7m{s}\033[m') |
| 18 | + |
| 19 | + |
| 20 | +def _exit_if_retv(*cmd: str) -> None: |
| 21 | + if subprocess.call(cmd): |
| 22 | + raise SystemExit(1) |
| 23 | + |
| 24 | + |
| 25 | +def _check_no_shared_objects(wheeldir: str) -> None: |
| 26 | + for zip_filename in os.listdir(wheeldir): |
| 27 | + with zipfile.ZipFile(os.path.join(wheeldir, zip_filename)) as zipf: |
| 28 | + for filename in zipf.namelist(): |
| 29 | + if filename.endswith('.so') or '.so.' in filename: |
| 30 | + raise AssertionError(zip_filename, filename) |
| 31 | + |
| 32 | + |
| 33 | +def _write_cache_key(version: str, wheeldir: str, dest: str) -> None: |
| 34 | + cache_hash = hashlib.sha256(f'{version}\n'.encode()) |
| 35 | + for filename in sorted(os.listdir(wheeldir)): |
| 36 | + cache_hash.update(f'{filename}\n'.encode()) |
| 37 | + with open(os.path.join(HERE, 'fakepython'), 'rb') as f: |
| 38 | + cache_hash.update(f.read()) |
| 39 | + with open(os.path.join(dest, 'CACHE_KEY'), 'wb') as f: |
| 40 | + f.write(base64.urlsafe_b64encode(cache_hash.digest()).rstrip(b'=')) |
| 41 | + |
| 42 | + |
| 43 | +def main() -> int: |
| 44 | + parser = argparse.ArgumentParser() |
| 45 | + parser.add_argument('version') |
| 46 | + args = parser.parse_args() |
| 47 | + |
| 48 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 49 | + wheeldir = os.path.join(tmpdir, 'wheels') |
| 50 | + os.mkdir(wheeldir) |
| 51 | + |
| 52 | + _msg('building podman image...') |
| 53 | + _exit_if_retv('podman', 'build', '-q', '-t', IMG, HERE) |
| 54 | + |
| 55 | + _msg('populating wheels...') |
| 56 | + _exit_if_retv( |
| 57 | + 'podman', 'run', '--rm', '--volume', f'{wheeldir}:/wheels:rw', IMG, |
| 58 | + 'pip', 'wheel', f'pre_commit=={args.version}', |
| 59 | + '--wheel-dir', '/wheels', |
| 60 | + ) |
| 61 | + |
| 62 | + _msg('validating wheels...') |
| 63 | + _check_no_shared_objects(wheeldir) |
| 64 | + |
| 65 | + _msg('adding fakepython / __main__.py...') |
| 66 | + shutil.copy(os.path.join(HERE, 'fakepython'), tmpdir) |
| 67 | + mainfile = os.path.join(tmpdir, '__main__.py') |
| 68 | + shutil.copy(os.path.join(HERE, 'entry'), mainfile) |
| 69 | + |
| 70 | + _msg('copying file_lock.py...') |
| 71 | + file_lock_py = os.path.join(HERE, '../../pre_commit/file_lock.py') |
| 72 | + file_lock_py_dest = os.path.join(tmpdir, 'pre_commit/file_lock.py') |
| 73 | + os.makedirs(os.path.dirname(file_lock_py_dest)) |
| 74 | + shutil.copy(file_lock_py, file_lock_py_dest) |
| 75 | + |
| 76 | + _msg('writing CACHE_KEY...') |
| 77 | + _write_cache_key(args.version, wheeldir, tmpdir) |
| 78 | + |
| 79 | + filename = f'pre-commit-{args.version}.pyz' |
| 80 | + _msg(f'writing {filename}...') |
| 81 | + shebang = '/usr/bin/env python3' |
| 82 | + zipapp.create_archive(tmpdir, filename, interpreter=shebang) |
| 83 | + |
| 84 | + return 0 |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + exit(main()) |
0 commit comments