File: diff_coverage.py

package info (click to toggle)
zim 0.76.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,952 kB
  • sloc: python: 68,612; xml: 1,270; javascript: 512; sh: 101; makefile: 47
file content (95 lines) | stat: -rwxr-xr-x 2,091 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python3

# Tool that reads diff from pipe and filters out sections that are
# covered by testing
#
# Assumes code is new, after diff is applied

import coverage



class Diff(object):

	def __init__(self, lines=None):
		self.parts = []
		if lines:
			for part in self._readparts(lines):
				self.parts.append(part)

	def to_text(self):
		text = []
		for part in self.parts:
			text += ["==== ", part.file, "\n"]
			text += [part.head]
			text += part.lines
			text += ['\n']
		return ''.join(text)

	@staticmethod
	def _readparts(lines):
		lines = iter(lines)
		file = None
		for line in lines:
			if line.startswith('+++ '):
				file = line[4:].split()[0]
				if file.startswith('b/'):
					file = file[2:] # git has a/ b/ prefixes for old and new file
			elif line.startswith('@@'):
				assert file is not None
				head = line
				part = []
				for line in lines:
					if not line[0] in (' ', '+', '-'):
						break
					else:
						part.append(line)
				yield DiffPart(file, head, part)
			else:
				pass # any other line with info, not part of a diff block


class DiffPart(object):

	def __init__(self, file, head, lines):
		self.file = file
		self.head = head
		self.lines = lines

	def range(self):
		old, new = self.head.strip().split()[:2]
		start, size = list(map(int, new[1:].split(',')))
		return start, start + size



def coverage_filter_diff(diff):
	cov = coverage.coverage()
	cov.load()

	new = Diff()
	for part in diff.parts:
		if  part.file.startswith('zim') \
		and part.file.endswith('.py'):
			if not part_covered(part, cov):
				new.parts.append(part)
			else:
				print("Covered %s %s" % (part.file, part.head.strip()))
		else:
			print("Skip %s" % part.file)
	return new


def part_covered(part, cov):
	f, l, missing, r = cov.analysis(part.file)
	start, end = part.range()
	return not any(l >= start and l <= end for l in missing)


if __name__ == '__main__':
	import sys
	lines = sys.stdin.readlines()
	diff = Diff(lines)
	print("TODO: check timestamps diff vs timestamp './coverage'")
	diff = coverage_filter_diff(diff)
	sys.stdout.write(diff.to_text())