"Fossies" - the Fresh Open Source Software Archive

Member "zim-0.71.1/tools/diff_coverage.py" (14 Mar 2019, 2091 Bytes) of package /linux/privat/zim-0.71.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "diff_coverage.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.69.1_vs_0.70.

    1 #!/usr/bin/python3
    2 
    3 # Tool that reads diff from pipe and filters out sections that are
    4 # covered by testing
    5 #
    6 # Assumes code is new, after diff is applied
    7 
    8 import coverage
    9 
   10 
   11 
   12 class Diff(object):
   13 
   14     def __init__(self, lines=None):
   15         self.parts = []
   16         if lines:
   17             for part in self._readparts(lines):
   18                 self.parts.append(part)
   19 
   20     def to_text(self):
   21         text = []
   22         for part in self.parts:
   23             text += ["==== ", part.file, "\n"]
   24             text += [part.head]
   25             text += part.lines
   26             text += ['\n']
   27         return ''.join(text)
   28 
   29     @staticmethod
   30     def _readparts(lines):
   31         lines = iter(lines)
   32         file = None
   33         for line in lines:
   34             if line.startswith('+++ '):
   35                 file = line[4:].split()[0]
   36                 if file.startswith('b/'):
   37                     file = file[2:] # git has a/ b/ prefixes for old and new file
   38             elif line.startswith('@@'):
   39                 assert file is not None
   40                 head = line
   41                 part = []
   42                 for line in lines:
   43                     if not line[0] in (' ', '+', '-'):
   44                         break
   45                     else:
   46                         part.append(line)
   47                 yield DiffPart(file, head, part)
   48             else:
   49                 pass # any other line with info, not part of a diff block
   50 
   51 
   52 class DiffPart(object):
   53 
   54     def __init__(self, file, head, lines):
   55         self.file = file
   56         self.head = head
   57         self.lines = lines
   58 
   59     def range(self):
   60         old, new = self.head.strip().split()[:2]
   61         start, size = list(map(int, new[1:].split(',')))
   62         return start, start + size
   63 
   64 
   65 
   66 def coverage_filter_diff(diff):
   67     cov = coverage.coverage()
   68     cov.load()
   69 
   70     new = Diff()
   71     for part in diff.parts:
   72         if  part.file.startswith('zim') \
   73         and part.file.endswith('.py'):
   74             if not part_covered(part, cov):
   75                 new.parts.append(part)
   76             else:
   77                 print("Covered %s %s" % (part.file, part.head.strip()))
   78         else:
   79             print("Skip %s" % part.file)
   80     return new
   81 
   82 
   83 def part_covered(part, cov):
   84     f, l, missing, r = cov.analysis(part.file)
   85     start, end = part.range()
   86     return not any(l >= start and l <= end for l in missing)
   87 
   88 
   89 if __name__ == '__main__':
   90     import sys
   91     lines = sys.stdin.readlines()
   92     diff = Diff(lines)
   93     print("TODO: check timestamps diff vs timestamp './coverage'")
   94     diff = coverage_filter_diff(diff)
   95     sys.stdout.write(diff.to_text())