You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dump_coveralls.py 1.6KB

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