|
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 |
|
4 """Convert pickle to JSON for coverage.py.""" |
|
5 |
|
6 from coverage.backward import pickle |
|
7 from coverage.data import CoverageData |
|
8 |
|
9 |
|
10 def pickle_read_raw_data(cls_unused, file_obj): |
|
11 """Replacement for CoverageData._read_raw_data.""" |
|
12 return pickle.load(file_obj) |
|
13 |
|
14 |
|
15 def pickle2json(infile, outfile): |
|
16 """Convert a coverage.py 3.x pickle data file to a 4.x JSON data file.""" |
|
17 try: |
|
18 old_read_raw_data = CoverageData._read_raw_data |
|
19 CoverageData._read_raw_data = pickle_read_raw_data |
|
20 |
|
21 covdata = CoverageData() |
|
22 |
|
23 with open(infile, 'rb') as inf: |
|
24 covdata.read_fileobj(inf) |
|
25 |
|
26 covdata.write_file(outfile) |
|
27 finally: |
|
28 CoverageData._read_raw_data = old_read_raw_data |
|
29 |
|
30 |
|
31 if __name__ == "__main__": |
|
32 from optparse import OptionParser |
|
33 |
|
34 parser = OptionParser(usage="usage: %s [options]" % __file__) |
|
35 parser.description = "Convert .coverage files from pickle to JSON format" |
|
36 parser.add_option( |
|
37 "-i", "--input-file", action="store", default=".coverage", |
|
38 help="Name of input file. Default .coverage", |
|
39 ) |
|
40 parser.add_option( |
|
41 "-o", "--output-file", action="store", default=".coverage", |
|
42 help="Name of output file. Default .coverage", |
|
43 ) |
|
44 |
|
45 (options, args) = parser.parse_args() |
|
46 |
|
47 pickle2json(options.input_file, options.output_file) |