Debian Codegraph @

pypy/tool/genstatistic.py

raw symbols SCIP
1
2 import py
3 from py._cmdline import pycountloc as countloc
4 from py.xml import raw
5 from pypy import pypydir
6
7 pypydir = py.path.local(pypydir)
8
9 def isdocfile(p):
10 return (p.ext in ('.txt', '.rst') or
11 p.basename in ('README', 'NOTES', 'LICENSE'))
12
13 def istestfile(p):
14 if not p.check(file=1, ext='.py'):
15 return False
16 pb = p.purebasename
17 if pb.startswith('test_') or pb.endswith('_test'):
18 return True
19 if 'test' in [x.basename for x in p.parts()[-4:]]:
20 return True
21
22 notistestfile = lambda x: not istestfile(x)
23
24 class relchecker:
25 def __init__(self, rel):
26 self.rel = rel
27
28 def __call__(self, p):
29 return p.relto(pypydir).startswith(self.rel)
30
31 def isfile(p):
32 return p.check(file=1) and p.ext in ('.py', '.txt', '')
33
34 def recpypy(p):
35 if p.basename[0] == '.':
36 return False
37 if p.basename in ('Pyrex',
38 '_cache',
39 'unicodedata',
40 'pypy-translation-snapshot'):
41 return False
42 return True
43
44 def getpypycounter():
45 filecounter = countloc.FileCounter()
46 root = py.path.local(pypydir)
47 filecounter.addrecursive(root, isfile, rec=recpypy)
48 return filecounter
49
50 class CounterModel:
51 def __init__(self, pypycounter):
52 self.counter = pypycounter
53 self.totallines = pypycounter.numlines
54 self.totalfiles = pypycounter.numfiles
55 self.testlines = pypycounter.getnumlines(istestfile)
56 self.testfiles = pypycounter.getnumfiles(istestfile)
57 self.notestlines = pypycounter.getnumlines(notistestfile)
58 self.notestfiles = pypycounter.getnumfiles(notistestfile)
59 self.doclines = pypycounter.getnumlines(isdocfile)
60 self.docfiles = pypycounter.getnumfiles(isdocfile)
61
62 #
63 # rendering
64 #
65 def row(*args):
66 return html.tr([html.td(arg) for arg in args])
67
68 def percent(x, y):
69 return "%.2f%%" % (x / (y/100.0))
70
71 def viewlocsummary(model):
72 t = html.table(
73 row("total number of lines", model.totallines, raw(" ")),
74 row("number of testlines", model.testlines,
75 percent(model.testlines, model.totallines)),
76 row("number of non-testlines", model.notestlines,
77 percent(model.notestlines, model.totallines)),
78
79 row("total number of files", model.totalfiles, raw(" ")),
80 row("number of testfiles", model.testfiles,
81 percent(model.testfiles, model.totalfiles)),
82 row("number of non-testfiles", model.notestfiles,
83 percent(model.notestfiles, model.totalfiles)),
84 )
85 if model.docfiles:
86 t.append(row("number of docfiles", model.docfiles,
87 percent(model.docfiles, model.totalfiles)))
88 t.append(row("number of doclines", model.doclines,
89 percent(model.doclines, model.totallines)))
90 return t
91
92 def viewloclist(model):
93 t = html.table()
94 d = model.counter.file2numlines
95 paths = d.items()
96 paths.sort(lambda x, y: -cmp(x[1], y[1])) # sort by numlines
97 for p, numlines in paths:
98 if numlines < 3:
99 continue
100 t.append(row(p.relto(pypydir.dirpath()), numlines))
101 return t
102
103 def viewsubdirs(model):
104 t = html.table()
105 for p in pypydir.listdir():
106 if p.basename in '_cache .svn'.split():
107 continue
108 if p.check(dir=1):
109 counter = countloc.FileCounter()
110 counter.addrecursive(p, isfile, recpypy)
111 model = CounterModel(counter)
112 t.append(row(html.h2(p.relto(pypydir.dirpath()))))
113 t.append(viewlocsummary(model))
114 t.append(viewloclist(model))
115 return t
116
117 if __name__ == '__main__':
118 if len(py.std.sys.argv) >= 2:
119 target = py.path.local(py.std.sys.argv[1])
120 else:
121 target = py.path.local('index.html')
122 print "writing source statistics to", target
123 pypycounter = getpypycounter()
124 model = CounterModel(pypycounter)
125 rev = py.path.svnwc(pypydir).info().rev
126 html = py.xml.html
127 doc = html.html(
128 html.head(
129 html.title("PyPy Statistics %d" % rev),
130 ),
131 html.body(
132 html.h2("rev %d PyPy Summary of Files and Lines" % rev),
133 viewlocsummary(model),
134 html.h2("Details on first-level subdirectories"),
135 viewsubdirs(model),
136 html.h3("PyPy Full List Files and Lines"),
137 viewloclist(model),
138 html.p("files with less than 3 lines ignored")
139 )
140 )
141 content = doc.unicode(indent=2).encode('utf8')
142 target.write(content)