OLD | NEW |
1 """Coverage data for Coverage.""" | 1 """Coverage data for Coverage.""" |
2 | 2 |
3 import os | 3 import os |
4 | 4 |
5 from coverage.backward import iitems, pickle, sorted # pylint: disable=W0622 | 5 from coverage.backward import iitems, pickle, sorted # pylint: disable=W0622 |
6 from coverage.files import PathAliases | 6 from coverage.files import PathAliases |
7 from coverage.misc import file_be_gone | 7 from coverage.misc import file_be_gone |
8 | 8 |
9 | 9 |
10 class CoverageData(object): | 10 class CoverageData(object): |
11 """Manages collected coverage data, including file storage. | 11 """Manages collected coverage data, including file storage. |
12 | 12 |
13 The data file format is a pickled dict, with these keys: | 13 The data file format is a pickled dict, with these keys: |
14 | 14 |
15 * collector: a string identifying the collecting software | 15 * collector: a string identifying the collecting software |
16 | 16 |
17 * lines: a dict mapping filenames to sorted lists of line numbers | 17 * lines: a dict mapping filenames to sorted lists of line numbers |
18 executed: | 18 executed: |
19 { 'file1': [17,23,45], 'file2': [1,2,3], ... } | 19 { 'file1': [17,23,45], 'file2': [1,2,3], ... } |
20 | 20 |
21 * arcs: a dict mapping filenames to sorted lists of line number pairs: | 21 * arcs: a dict mapping filenames to sorted lists of line number pairs: |
22 { 'file1': [(17,23), (17,25), (25,26)], ... } | 22 { 'file1': [(17,23), (17,25), (25,26)], ... } |
23 | 23 |
24 """ | 24 """ |
25 | 25 |
26 def __init__(self, basename=None, collector=None): | 26 def __init__(self, basename=None, collector=None, debug=None): |
27 """Create a CoverageData. | 27 """Create a CoverageData. |
28 | 28 |
29 `basename` is the name of the file to use for storing data. | 29 `basename` is the name of the file to use for storing data. |
30 | 30 |
31 `collector` is a string describing the coverage measurement software. | 31 `collector` is a string describing the coverage measurement software. |
32 | 32 |
| 33 `debug` is a `DebugControl` object for writing debug messages. |
| 34 |
33 """ | 35 """ |
34 self.collector = collector or 'unknown' | 36 self.collector = collector or 'unknown' |
| 37 self.debug = debug |
35 | 38 |
36 self.use_file = True | 39 self.use_file = True |
37 | 40 |
38 # Construct the filename that will be used for data file storage, if we | 41 # Construct the filename that will be used for data file storage, if we |
39 # ever do any file storage. | 42 # ever do any file storage. |
40 self.filename = basename or ".coverage" | 43 self.filename = basename or ".coverage" |
41 self.filename = os.path.abspath(self.filename) | 44 self.filename = os.path.abspath(self.filename) |
42 | 45 |
43 # A map from canonical Python source file name to a dictionary in | 46 # A map from canonical Python source file name to a dictionary in |
44 # which there's an entry for each line number that has been | 47 # which there's an entry for each line number that has been |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 data = {} | 117 data = {} |
115 | 118 |
116 data['lines'] = self.line_data() | 119 data['lines'] = self.line_data() |
117 arcs = self.arc_data() | 120 arcs = self.arc_data() |
118 if arcs: | 121 if arcs: |
119 data['arcs'] = arcs | 122 data['arcs'] = arcs |
120 | 123 |
121 if self.collector: | 124 if self.collector: |
122 data['collector'] = self.collector | 125 data['collector'] = self.collector |
123 | 126 |
| 127 if self.debug and self.debug.should('dataio'): |
| 128 self.debug.write("Writing data to %r" % (filename,)) |
| 129 |
124 # Write the pickle to the file. | 130 # Write the pickle to the file. |
125 fdata = open(filename, 'wb') | 131 fdata = open(filename, 'wb') |
126 try: | 132 try: |
127 pickle.dump(data, fdata, 2) | 133 pickle.dump(data, fdata, 2) |
128 finally: | 134 finally: |
129 fdata.close() | 135 fdata.close() |
130 | 136 |
131 def read_file(self, filename): | 137 def read_file(self, filename): |
132 """Read the coverage data from `filename`.""" | 138 """Read the coverage data from `filename`.""" |
133 self.lines, self.arcs = self._read_file(filename) | 139 self.lines, self.arcs = self._read_file(filename) |
134 | 140 |
135 def raw_data(self, filename): | 141 def raw_data(self, filename): |
136 """Return the raw pickled data from `filename`.""" | 142 """Return the raw pickled data from `filename`.""" |
| 143 if self.debug and self.debug.should('dataio'): |
| 144 self.debug.write("Reading data from %r" % (filename,)) |
137 fdata = open(filename, 'rb') | 145 fdata = open(filename, 'rb') |
138 try: | 146 try: |
139 data = pickle.load(fdata) | 147 data = pickle.load(fdata) |
140 finally: | 148 finally: |
141 fdata.close() | 149 fdata.close() |
142 return data | 150 return data |
143 | 151 |
144 def _read_file(self, filename): | 152 def _read_file(self, filename): |
145 """Return the stored coverage data from the given file. | 153 """Return the stored coverage data from the given file. |
146 | 154 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 | 269 |
262 if __name__ == '__main__': | 270 if __name__ == '__main__': |
263 # Ad-hoc: show the raw data in a data file. | 271 # Ad-hoc: show the raw data in a data file. |
264 import pprint, sys | 272 import pprint, sys |
265 covdata = CoverageData() | 273 covdata = CoverageData() |
266 if sys.argv[1:]: | 274 if sys.argv[1:]: |
267 fname = sys.argv[1] | 275 fname = sys.argv[1] |
268 else: | 276 else: |
269 fname = covdata.filename | 277 fname = covdata.filename |
270 pprint.pprint(covdata.raw_data(fname)) | 278 pprint.pprint(covdata.raw_data(fname)) |
OLD | NEW |