|
2 | 2 | import argparse |
3 | 3 | import base64 |
4 | 4 | import hashlib |
| 5 | +import importlib.resources |
| 6 | +import io |
5 | 7 | import os.path |
6 | 8 | import shutil |
7 | 9 | import subprocess |
@@ -30,11 +32,25 @@ def _check_no_shared_objects(wheeldir: str) -> None: |
30 | 32 | raise AssertionError(zip_filename, filename) |
31 | 33 |
|
32 | 34 |
|
| 35 | +def _add_shim(dest: str) -> None: |
| 36 | + shim = os.path.join(HERE, 'python') |
| 37 | + shutil.copy(shim, dest) |
| 38 | + |
| 39 | + bio = io.BytesIO() |
| 40 | + with zipfile.ZipFile(bio, 'w') as zipf: |
| 41 | + zipf.write(shim, arcname='__main__.py') |
| 42 | + |
| 43 | + with open(os.path.join(dest, 'python.exe'), 'wb') as f: |
| 44 | + f.write(importlib.resources.read_binary('distlib', 't32.exe')) |
| 45 | + f.write(b'#!py.exe -3\n') |
| 46 | + f.write(bio.getvalue()) |
| 47 | + |
| 48 | + |
33 | 49 | def _write_cache_key(version: str, wheeldir: str, dest: str) -> None: |
34 | 50 | cache_hash = hashlib.sha256(f'{version}\n'.encode()) |
35 | 51 | for filename in sorted(os.listdir(wheeldir)): |
36 | 52 | cache_hash.update(f'{filename}\n'.encode()) |
37 | | - with open(os.path.join(HERE, 'fakepython'), 'rb') as f: |
| 53 | + with open(os.path.join(HERE, 'python'), 'rb') as f: |
38 | 54 | cache_hash.update(f.read()) |
39 | 55 | with open(os.path.join(dest, 'CACHE_KEY'), 'wb') as f: |
40 | 56 | f.write(base64.urlsafe_b64encode(cache_hash.digest()).rstrip(b'=')) |
@@ -62,11 +78,13 @@ def main() -> int: |
62 | 78 | _msg('validating wheels...') |
63 | 79 | _check_no_shared_objects(wheeldir) |
64 | 80 |
|
65 | | - _msg('adding fakepython / __main__.py...') |
66 | | - shutil.copy(os.path.join(HERE, 'fakepython'), tmpdir) |
| 81 | + _msg('adding __main__.py...') |
67 | 82 | mainfile = os.path.join(tmpdir, '__main__.py') |
68 | 83 | shutil.copy(os.path.join(HERE, 'entry'), mainfile) |
69 | 84 |
|
| 85 | + _msg('adding shim...') |
| 86 | + _add_shim(tmpdir) |
| 87 | + |
70 | 88 | _msg('copying file_lock.py...') |
71 | 89 | file_lock_py = os.path.join(HERE, '../../pre_commit/file_lock.py') |
72 | 90 | file_lock_py_dest = os.path.join(tmpdir, 'pre_commit/file_lock.py') |
|
0 commit comments