mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 00:04:42 +04:00
make an exe stub for windows
This commit is contained in:
parent
bc198b89ca
commit
fbd529204b
4 changed files with 35 additions and 8 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
covdefaults
|
covdefaults
|
||||||
coverage
|
coverage
|
||||||
|
distlib
|
||||||
pytest
|
pytest
|
||||||
pytest-env
|
pytest-env
|
||||||
re-assert
|
re-assert
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,8 @@ def _ensure_cache(zipf: zipfile.ZipFile, cache_key: str) -> str:
|
||||||
try:
|
try:
|
||||||
zipf.extractall(tmpdir)
|
zipf.extractall(tmpdir)
|
||||||
# zip doesn't maintain permissions
|
# zip doesn't maintain permissions
|
||||||
_make_executable(os.path.join(tmpdir, 'fakepython'))
|
_make_executable(os.path.join(tmpdir, 'python'))
|
||||||
|
_make_executable(os.path.join(tmpdir, 'python.exe'))
|
||||||
os.rename(tmpdir, cache_dest)
|
os.rename(tmpdir, cache_dest)
|
||||||
except BaseException:
|
except BaseException:
|
||||||
shutil.rmtree(tmpdir)
|
shutil.rmtree(tmpdir)
|
||||||
|
|
@ -49,8 +50,12 @@ def main() -> int:
|
||||||
|
|
||||||
cache_dest = _ensure_cache(zipf, cache_key)
|
cache_dest = _ensure_cache(zipf, cache_key)
|
||||||
|
|
||||||
fakepython = os.path.join(cache_dest, 'fakepython')
|
if sys.platform != 'win32':
|
||||||
cmd = (sys.executable, fakepython, '-mpre_commit', *sys.argv[1:])
|
exe = os.path.join(cache_dest, 'python')
|
||||||
|
else:
|
||||||
|
exe = os.path.join(cache_dest, 'python.exe')
|
||||||
|
|
||||||
|
cmd = (exe, '-mpre_commit', *sys.argv[1:])
|
||||||
if sys.platform == 'win32': # https://bugs.python.org/issue19124
|
if sys.platform == 'win32': # https://bugs.python.org/issue19124
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
import argparse
|
import argparse
|
||||||
import base64
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import importlib.resources
|
||||||
|
import io
|
||||||
import os.path
|
import os.path
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
@ -30,11 +32,25 @@ def _check_no_shared_objects(wheeldir: str) -> None:
|
||||||
raise AssertionError(zip_filename, filename)
|
raise AssertionError(zip_filename, filename)
|
||||||
|
|
||||||
|
|
||||||
|
def _add_shim(dest: str) -> None:
|
||||||
|
shim = os.path.join(HERE, 'python')
|
||||||
|
shutil.copy(shim, dest)
|
||||||
|
|
||||||
|
bio = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(bio, 'w') as zipf:
|
||||||
|
zipf.write(shim, arcname='__main__.py')
|
||||||
|
|
||||||
|
with open(os.path.join(dest, 'python.exe'), 'wb') as f:
|
||||||
|
f.write(importlib.resources.read_binary('distlib', 't32.exe'))
|
||||||
|
f.write(b'#!py.exe -3\n')
|
||||||
|
f.write(bio.getvalue())
|
||||||
|
|
||||||
|
|
||||||
def _write_cache_key(version: str, wheeldir: str, dest: str) -> None:
|
def _write_cache_key(version: str, wheeldir: str, dest: str) -> None:
|
||||||
cache_hash = hashlib.sha256(f'{version}\n'.encode())
|
cache_hash = hashlib.sha256(f'{version}\n'.encode())
|
||||||
for filename in sorted(os.listdir(wheeldir)):
|
for filename in sorted(os.listdir(wheeldir)):
|
||||||
cache_hash.update(f'{filename}\n'.encode())
|
cache_hash.update(f'{filename}\n'.encode())
|
||||||
with open(os.path.join(HERE, 'fakepython'), 'rb') as f:
|
with open(os.path.join(HERE, 'python'), 'rb') as f:
|
||||||
cache_hash.update(f.read())
|
cache_hash.update(f.read())
|
||||||
with open(os.path.join(dest, 'CACHE_KEY'), 'wb') as f:
|
with open(os.path.join(dest, 'CACHE_KEY'), 'wb') as f:
|
||||||
f.write(base64.urlsafe_b64encode(cache_hash.digest()).rstrip(b'='))
|
f.write(base64.urlsafe_b64encode(cache_hash.digest()).rstrip(b'='))
|
||||||
|
|
@ -62,11 +78,13 @@ def main() -> int:
|
||||||
_msg('validating wheels...')
|
_msg('validating wheels...')
|
||||||
_check_no_shared_objects(wheeldir)
|
_check_no_shared_objects(wheeldir)
|
||||||
|
|
||||||
_msg('adding fakepython / __main__.py...')
|
_msg('adding __main__.py...')
|
||||||
shutil.copy(os.path.join(HERE, 'fakepython'), tmpdir)
|
|
||||||
mainfile = os.path.join(tmpdir, '__main__.py')
|
mainfile = os.path.join(tmpdir, '__main__.py')
|
||||||
shutil.copy(os.path.join(HERE, 'entry'), mainfile)
|
shutil.copy(os.path.join(HERE, 'entry'), mainfile)
|
||||||
|
|
||||||
|
_msg('adding shim...')
|
||||||
|
_add_shim(tmpdir)
|
||||||
|
|
||||||
_msg('copying file_lock.py...')
|
_msg('copying file_lock.py...')
|
||||||
file_lock_py = os.path.join(HERE, '../../pre_commit/file_lock.py')
|
file_lock_py = os.path.join(HERE, '../../pre_commit/file_lock.py')
|
||||||
file_lock_py_dest = os.path.join(tmpdir, 'pre_commit/file_lock.py')
|
file_lock_py_dest = os.path.join(tmpdir, 'pre_commit/file_lock.py')
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,10 @@ import os.path
|
||||||
import runpy
|
import runpy
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
HERE = os.path.dirname(os.path.realpath(__file__))
|
# an exe-zipapp will have a __file__ of shim.exe/__main__.py
|
||||||
|
EXE = __file__ if os.path.isfile(__file__) else os.path.dirname(__file__)
|
||||||
|
EXE = os.path.realpath(EXE)
|
||||||
|
HERE = os.path.dirname(EXE)
|
||||||
WHEELDIR = os.path.join(HERE, 'wheels')
|
WHEELDIR = os.path.join(HERE, 'wheels')
|
||||||
SITE_DIRS = frozenset(('dist-packages', 'site-packages'))
|
SITE_DIRS = frozenset(('dist-packages', 'site-packages'))
|
||||||
|
|
||||||
|
|
@ -24,7 +27,7 @@ def main() -> int:
|
||||||
for wheel in sorted(os.listdir(WHEELDIR)):
|
for wheel in sorted(os.listdir(WHEELDIR)):
|
||||||
sys.path.append(os.path.join(WHEELDIR, wheel))
|
sys.path.append(os.path.join(WHEELDIR, wheel))
|
||||||
if args.m == 'pre_commit' or args.m.startswith('pre_commit.'):
|
if args.m == 'pre_commit' or args.m.startswith('pre_commit.'):
|
||||||
sys.executable = os.path.abspath(__file__)
|
sys.executable = EXE
|
||||||
sys.argv[1:] = rest
|
sys.argv[1:] = rest
|
||||||
runpy.run_module(args.m, run_name='__main__', alter_sys=True)
|
runpy.run_module(args.m, run_name='__main__', alter_sys=True)
|
||||||
return 0
|
return 0
|
||||||
Loading…
Add table
Add a link
Reference in a new issue