| 150 | return filename.lstrip(".") |
| 151 | |
| 152 | class CoverageResults: |
| 153 | def __init__(self, counts=None, calledfuncs=None, infile=None, |
| 154 | callers=None, outfile=None): |
| 155 | self.counts = counts |
| 156 | if self.counts is None: |
| 157 | self.counts = {} |
| 158 | self.counter = self.counts.copy() # map (filename, lineno) to count |
| 159 | self.calledfuncs = calledfuncs |
| 160 | if self.calledfuncs is None: |
| 161 | self.calledfuncs = {} |
| 162 | self.calledfuncs = self.calledfuncs.copy() |
| 163 | self.callers = callers |
| 164 | if self.callers is None: |
| 165 | self.callers = {} |
| 166 | self.callers = self.callers.copy() |
| 167 | self.infile = infile |
| 168 | self.outfile = outfile |
| 169 | if self.infile: |
| 170 | # Try to merge existing counts file. |
| 171 | try: |
| 172 | with open(self.infile, 'rb') as f: |
| 173 | counts, calledfuncs, callers = pickle.load(f) |
| 174 | self.update(self.__class__(counts, calledfuncs, callers=callers)) |
| 175 | except (OSError, EOFError, ValueError) as err: |
| 176 | print(("Skipping counts file %r: %s" |
| 177 | % (self.infile, err)), file=sys.stderr) |
| 178 | |
| 179 | def is_ignored_filename(self, filename): |
| 180 | """Return True if the filename does not refer to a file |
| 181 | we want to have reported. |
| 182 | """ |
| 183 | return filename.startswith('<') and filename.endswith('>') |
| 184 | |
| 185 | def update(self, other): |
| 186 | """Merge in the data from another CoverageResults""" |
| 187 | counts = self.counts |
| 188 | calledfuncs = self.calledfuncs |
| 189 | callers = self.callers |
| 190 | other_counts = other.counts |
| 191 | other_calledfuncs = other.calledfuncs |
| 192 | other_callers = other.callers |
| 193 | |
| 194 | for key in other_counts: |
| 195 | counts[key] = counts.get(key, 0) + other_counts[key] |
| 196 | |
| 197 | for key in other_calledfuncs: |
| 198 | calledfuncs[key] = 1 |
| 199 | |
| 200 | for key in other_callers: |
| 201 | callers[key] = 1 |
| 202 | |
| 203 | def write_results(self, show_missing=True, summary=False, coverdir=None, *, |
| 204 | ignore_missing_files=False): |
| 205 | """ |
| 206 | Write the coverage results. |
| 207 | |
| 208 | :param show_missing: Show lines that had no hits. |
| 209 | :param summary: Include coverage summary per module. |