summaryrefslogtreecommitdiffstats
path: root/testing/zipapp/python
blob: 67910fca81dd2c5ab02132869ca415c48c497d5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
"""A shim executable to put dependencies on sys.path"""
from __future__ import annotations

import argparse
import os.path
import runpy
import sys

# 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')
SITE_DIRS = frozenset(('dist-packages', 'site-packages'))


def main() -> int:
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument('-m')
    args, rest = parser.parse_known_args()

    if args.m:
        # try and remove site-packages from sys.path so our packages win
        sys.path[:] = [
            p for p in sys.path
            if os.path.split(p)[1] not in SITE_DIRS
        ]
        for wheel in sorted(os.listdir(WHEELDIR)):
            sys.path.append(os.path.join(WHEELDIR, wheel))
        if args.m == 'pre_commit' or args.m.startswith('pre_commit.'):
            sys.executable = EXE
        sys.argv[1:] = rest
        runpy.run_module(args.m, run_name='__main__', alter_sys=True)
        return 0
    else:
        cmd = (sys.executable, *sys.argv[1:])
        if sys.platform == 'win32':  # https://bugs.python.org/issue19124
            import subprocess

            return subprocess.call(cmd)
        else:
            os.execvp(cmd[0], cmd)


if __name__ == '__main__':
    raise SystemExit(main())