| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Small tool to dump JSON payload for coveralls.io API |
| 4 |
|
| 5 |
import json |
| 6 |
from operator import itemgetter |
| 7 |
import os |
| 8 |
import sys |
| 9 |
|
| 10 |
|
| 11 |
def warn(*args, **kwargs): |
| 12 |
print(*args, file=sys.stderr, **kwargs) |
| 13 |
|
| 14 |
|
| 15 |
def dump_file(json_file): |
| 16 |
"""Dumps coveralls.io API payload stored in json_file |
| 17 |
Returns: 0 if successful, 1 otherwise |
| 18 |
""" |
| 19 |
try: |
| 20 |
with open(json_file, encoding='utf8') as f: |
| 21 |
data = json.load(f) |
| 22 |
except OSError as err: |
| 23 |
warn(err) |
| 24 |
return os.EX_DATAERR |
| 25 |
except json.decoder.JSONDecodeError: |
| 26 |
warn("{}: json parsing error".format(json_file)) |
| 27 |
return 1 |
| 28 |
|
| 29 |
if 'source_files' not in data: |
| 30 |
warn("{}: no source_files, not a coveralls.io payload?".format(json_file)) |
| 31 |
return 1 |
| 32 |
|
| 33 |
print("{} ({} source files)".format(json_file, len(data['source_files']))) |
| 34 |
|
| 35 |
for src_file in sorted(data['source_files'], key=itemgetter('name')): |
| 36 |
covered_lines = not_skipped_lines = 0 |
| 37 |
for cnt in src_file['coverage']: |
| 38 |
if cnt is None: |
| 39 |
continue |
| 40 |
not_skipped_lines += 1 |
| 41 |
if cnt > 0: |
| 42 |
covered_lines += 1 |
| 43 |
if not_skipped_lines > 0: |
| 44 |
coverage = "{:.0%}".format(covered_lines / not_skipped_lines) |
| 45 |
else: |
| 46 |
coverage = 'N/A' |
| 47 |
|
| 48 |
print("\t{:>3} {}".format(coverage, src_file['name'])) |
| 49 |
|
| 50 |
return 0 |
| 51 |
|
| 52 |
|
| 53 |
def main(): |
| 54 |
if (len(sys.argv) < 2): |
| 55 |
warn("usage: {} file.json ...".format(sys.argv[0])) |
| 56 |
return os.EX_USAGE |
| 57 |
|
| 58 |
exit_status = 0 |
| 59 |
for f in sys.argv[1:]: |
| 60 |
exit_status += dump_file(f) |
| 61 |
|
| 62 |
return exit_status |
| 63 |
|
| 64 |
|
| 65 |
if __name__ == '__main__': |
| 66 |
sys.exit(main()) |