|
29 | 29 | """ |
30 | 30 |
|
31 | 31 | import contextlib |
32 | | -import errno |
33 | 32 | import os |
34 | 33 | import shutil |
35 | 34 | import sys |
|
39 | 38 | import coverage.cmdline # type: ignore |
40 | 39 |
|
41 | 40 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 41 | +ROOT_DIR = os.path.dirname(SCRIPT_DIR) |
42 | 42 |
|
43 | 43 |
|
44 | 44 | def main(): |
45 | 45 | # We set EMSDK_PYTHON to point to this file, which is executable via #! line. |
46 | 46 | # Emscripten uses EMSDK_PYTHON to invoke all python subprocesses. By making this |
47 | 47 | # script run all python subprocesses, all of them will execute under the |
48 | | - # watchful eye of emcoverage.py, and resulting in their code coverage being |
| 48 | + # watchful eye of emcoverage.py, resulting in their code coverage being |
49 | 49 | # tracked. |
50 | 50 | os.environ['EMSDK_PYTHON'] = os.path.abspath(__file__) |
51 | 51 |
|
52 | 52 | store = os.path.join(SCRIPT_DIR, 'coverage') |
53 | 53 |
|
54 | | - if len(sys.argv) < 2 or sys.argv[1] == 'help': |
| 54 | + if len(sys.argv) < 2 or sys.argv[1] in {'help', '-h', '--help'}: |
55 | 55 | print(__doc__.replace('emcoverage.py', sys.argv[0]).strip()) |
56 | | - return |
| 56 | + return 0 |
57 | 57 |
|
58 | 58 | if sys.argv[1] == 'reset': |
59 | | - shutil.rmtree(store) |
60 | | - return |
61 | | - |
62 | | - if sys.argv[1] in {'html', 'report', 'xml'}: |
| 59 | + shutil.rmtree(store, ignore_errors=True) |
| 60 | + with contextlib.suppress(FileNotFoundError): |
| 61 | + os.remove('.coverage') |
| 62 | + for f in glob('.coverage.*'): |
| 63 | + with contextlib.suppress(FileNotFoundError): |
| 64 | + os.remove(f) |
| 65 | + return 0 |
| 66 | + |
| 67 | + if sys.argv[1] in {'html', 'report', 'xml', 'annotate', 'json', 'lcov'}: |
63 | 68 | old_argv = sys.argv |
64 | | - sys.argv = ['coverage', 'combine', *glob(os.path.join(store, '*'))] |
65 | | - with contextlib.suppress(SystemExit): |
66 | | - coverage.cmdline.main() |
67 | | - sys.argv = [*old_argv, '-i'] |
| 69 | + files = glob(os.path.join(store, '*')) |
| 70 | + if files: |
| 71 | + sys.argv = ['coverage', 'combine', *files] |
| 72 | + with contextlib.suppress(SystemExit): |
| 73 | + coverage.cmdline.main() |
| 74 | + extra_args = [] |
| 75 | + if '-i' not in old_argv and '--ignore-errors' not in old_argv: |
| 76 | + extra_args.append('-i') |
| 77 | + sys.argv = ['coverage', *old_argv[1:], *extra_args] |
68 | 78 | return coverage.cmdline.main() |
69 | 79 |
|
70 | | - if not os.path.exists(sys.argv[1]): |
71 | | - # If argv[1] is not a file path, instead try to interpret it as an emscripten command. |
72 | | - # This allows `emcoverage.py emcc` or `emcoverage.py embuilder` to work. |
73 | | - sys.argv[1] = os.path.join(os.path.dirname(sys.executable), '..', sys.argv[1] + '.py') |
| 80 | + # Strip python interpreter flags passed by launcher scripts (e.g. -E in emcc wrapper) |
| 81 | + while len(sys.argv) > 1 and sys.argv[1] in {'-E', '-B', '-O', '-s', '-S', '-u'}: |
| 82 | + sys.argv.pop(1) |
| 83 | + |
| 84 | + if len(sys.argv) > 1 and sys.argv[1] in {'python', 'python3'}: |
| 85 | + sys.argv.pop(1) |
| 86 | + |
| 87 | + if len(sys.argv) < 2: |
| 88 | + print('emcoverage.py: no command specified', file=sys.stderr) |
| 89 | + return 1 |
| 90 | + |
| 91 | + # If argv[1] is an emscripten command without .py extension, resolve it to root script. |
| 92 | + # This allows `emcoverage.py emcc` or `emcoverage.py embuilder` to work even though |
| 93 | + # executable shell wrappers exist in the repository root. |
| 94 | + if not sys.argv[1].endswith('.py'): |
| 95 | + candidate = os.path.join(ROOT_DIR, sys.argv[1] + '.py') |
| 96 | + if os.path.isfile(candidate): |
| 97 | + sys.argv[1] = candidate |
| 98 | + elif not os.path.exists(sys.argv[1]): |
| 99 | + sys.argv[1] = candidate |
74 | 100 |
|
75 | | - try: |
76 | | - os.mkdir(store) |
77 | | - except OSError as e: |
78 | | - if e.errno != errno.EEXIST: |
79 | | - raise |
| 101 | + os.makedirs(store, exist_ok=True) |
80 | 102 | os.environ['COVERAGE_FILE'] = os.path.join(store, str(uuid.uuid4())) |
81 | 103 | sys.argv[0:1] = ['coverage', 'run', '--parallel-mode', '--'] |
82 | 104 |
|
|
0 commit comments