-
Notifications
You must be signed in to change notification settings - Fork 245
/
chk-invalid-headers.py
executable file
·122 lines (108 loc) · 3.82 KB
/
chk-invalid-headers.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
import gzip
import re
from json import dump
from os import environ
from os.path import exists, join
from _py2with3compatibility import run_cmd
def hasInclude(inc, src, cache):
if src not in cache:
cache[src] = {}
for e in ["CMSSW_BASE", "CMSSW_RELEASE_BASE", "CMSSW_FULL_RELEASE_BASE"]:
if (e not in environ) or (not environ[e]):
continue
src_file = join(environ[e], "src", src)
if not exists(src_file):
continue
exp = re.compile(r'^\s*#\s*include\s*([<"])([^<"]+)([<"])\s*$')
with open(src_file) as ref:
lnum = 0
for line in ref.readlines():
lnum += 1
m = exp.match(line)
if m:
cache[src][m.group(2)] = lnum
break
return inc in cache[src]
def readDeps(cache, depFile):
with gzip.open(depFile, "rt") as ref:
for line in ref.readlines():
data = line.strip().split(" ", 1)
if len(data) < 2:
continue
cache[data[0]] = data[1].strip()
return
def main():
includes = {}
uses = {}
usedby = {}
readDeps(uses, join(environ["CMSSW_RELEASE_BASE"], "etc", "dependencies", "uses.out.gz"))
readDeps(usedby, join(environ["CMSSW_RELEASE_BASE"], "etc", "dependencies", "usedby.out.gz"))
errs = {}
checked = {}
for inc in usedby:
items = [x for x in inc.split("/") if x]
if items[2] == "interface":
continue
for src in [s for s in usedby[inc].split(" ") if s]:
sitems = [x for x in src.split("/") if x]
if (items[0] == sitems[0]) and (items[1] == sitems[1]):
continue
if (items[2] == sitems[2]) and (items[2] == "test"):
continue
if hasInclude(inc, src, includes):
if src not in errs:
errs[src] = {}
errs[src][inc] = includes[src][inc]
if src in uses:
for isrc in [s for s in uses[src].strip().split(" ") if s]:
xchk = "%s:%s" % (src, inc)
if xchk in checked:
continue
checked[xchk] = 1
if not hasInclude(inc, isrc, includes):
continue
if isrc not in errs:
errs[isrc] = {}
errs[isrc][inc] = includes[isrc][inc]
# Free memory
del checked
del includes
del uses
del usedby
pkg_errs = {}
for e in errs:
pkg = "/".join(e.split("/")[:2])
if pkg not in pkg_errs:
pkg_errs[pkg] = {}
pkg_errs[pkg][e] = errs[e]
print("ERRORS:", pkg_errs)
outdir = "invalid-includes"
run_cmd("rm -f %s; mkdir %s" % (outdir, outdir))
all_count = {}
for p in sorted(pkg_errs):
all_count[p] = len(pkg_errs[p])
pdir = join(outdir, p)
run_cmd("mkdir -p %s" % pdir)
with open(join(pdir, "index.html"), "w") as ref:
ref.write("<html><head></head><body>\n")
for e in sorted(pkg_errs[p]):
ref.write("<h3>%s:</h3>\n" % e)
for inc in sorted(errs[e].keys()):
url = "https://github.com/cms-sw/cmssw/blob/%s/%s#L%s" % (
environ["CMSSW_VERSION"],
e,
errs[e][inc],
)
ref.write('<l1><a href="%s">%s</a></l1></br>\n' % (url, inc))
ref.write("</ul></br>\n")
ref.write("</body></html>\n")
dump(
all_count,
open(outdir + "/summary.json", "w"),
indent=2,
sort_keys=True,
separators=(",", ": "),
)
if __name__ == "__main__":
main()