pickle2json.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. """Convert pickle to JSON for coverage.py."""
  4. from coverage.backward import pickle
  5. from coverage.data import CoverageData
  6. def pickle_read_raw_data(cls_unused, file_obj):
  7. """Replacement for CoverageData._read_raw_data."""
  8. return pickle.load(file_obj)
  9. def pickle2json(infile, outfile):
  10. """Convert a coverage.py 3.x pickle data file to a 4.x JSON data file."""
  11. try:
  12. old_read_raw_data = CoverageData._read_raw_data
  13. CoverageData._read_raw_data = pickle_read_raw_data
  14. covdata = CoverageData()
  15. with open(infile, 'rb') as inf:
  16. covdata.read_fileobj(inf)
  17. covdata.write_file(outfile)
  18. finally:
  19. CoverageData._read_raw_data = old_read_raw_data
  20. if __name__ == "__main__":
  21. from optparse import OptionParser
  22. parser = OptionParser(usage="usage: %s [options]" % __file__)
  23. parser.description = "Convert .coverage files from pickle to JSON format"
  24. parser.add_option(
  25. "-i", "--input-file", action="store", default=".coverage",
  26. help="Name of input file. Default .coverage",
  27. )
  28. parser.add_option(
  29. "-o", "--output-file", action="store", default=".coverage",
  30. help="Name of output file. Default .coverage",
  31. )
  32. (options, args) = parser.parse_args()
  33. pickle2json(options.input_file, options.output_file)