/
upload_coverage.py
81 lines (67 loc) · 2.34 KB
/
upload_coverage.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Helper script to combine coverage (as codecov seems to have problems...).
Written in python to be cross-platform
"""
import argparse
from os import chdir, listdir, environ
from pathlib import Path
import platform
from pprint import pprint
import shlex
from subprocess import run, PIPE
import sys
THIS_FILE = Path(__file__)
GIT_MAIN_DIR = THIS_FILE.parent.parent.resolve()
COVERAGE_DIR = GIT_MAIN_DIR.joinpath('.coverage_dir')
PYVERSION = '{}.{}'.format(sys.version_info[0], sys.version_info[1])
def msg(*args):
print('ERR:', *args, file=sys.stderr)
def pmsg(*args):
pprint(*args, stream=sys.stderr)
def run_with_python(args, **kwargs):
if platform.system() == 'Windows':
exe = ['py', '-' + PYVERSION, '-m']
else:
exe = []
cmd = exe + args
msg("Running:", *cmd)
res = run(cmd, check=True, stdout=PIPE, stderr=PIPE, **kwargs)
msg("STDOUT:")
sys.stdout.buffer.write(res.stdout)
msg("STDERR:")
sys.stderr.buffer.write(res.stdout)
return res
def send_coverage(*, workdir, coverage_files, codecov_token):
chdir(workdir)
run_with_python(['coverage', 'combine'] + coverage_files)
msg(f"Combined coverage, listing {GIT_MAIN_DIR}")
pmsg(sorted(listdir(GIT_MAIN_DIR)))
run_with_python(['coverage', 'xml', '--ignore-errors'])
msg(f"Created coverage xml, listing {GIT_MAIN_DIR}")
pmsg(sorted(listdir(GIT_MAIN_DIR)))
codecov_args = ["--required"]
if codecov_token is not None:
codecov_args.extend(['-t', codecov_token])
codecov_args.extend(['--file', 'coverage.xml'])
run_with_python(['codecov'] + codecov_args)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--codecov-token", default=None)
args = parser.parse_args()
msg(f"Working in {GIT_MAIN_DIR}, listing coverage dir {COVERAGE_DIR}")
pmsg(sorted(listdir(COVERAGE_DIR)))
coverage_files = [str(f) for f in COVERAGE_DIR.glob('coverage-*')]
if coverage_files:
send_coverage(
workdir=GIT_MAIN_DIR,
coverage_files=coverage_files,
codecov_token=args.codecov_token,
)
else:
msg("No coverage files found")
if environ.get("H5PY_ENFORCE_COVERAGE") is not None:
raise RuntimeError(
"Coverage required, no coverage found, failing..."
)
if __name__ == '__main__':
main()