File: genstatistic.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (142 lines) | stat: -rw-r--r-- 4,548 bytes parent folder | download | duplicates (8)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

import py
from py._cmdline import pycountloc as countloc
from py.xml import raw
from pypy import pypydir

pypydir = py.path.local(pypydir)

def isdocfile(p):
    return (p.ext in ('.txt', '.rst') or
            p.basename in ('README', 'NOTES', 'LICENSE'))

def istestfile(p):
    if not p.check(file=1, ext='.py'):
        return False
    pb = p.purebasename
    if pb.startswith('test_') or pb.endswith('_test'):
        return True
    if 'test' in [x.basename for x in p.parts()[-4:]]:
        return True

notistestfile = lambda x: not istestfile(x)

class relchecker:
    def __init__(self, rel):
        self.rel = rel

    def __call__(self, p):
        return p.relto(pypydir).startswith(self.rel)

def isfile(p):
    return p.check(file=1) and p.ext in ('.py', '.txt', '')

def recpypy(p):
    if p.basename[0] == '.':
        return False
    if p.basename in ('Pyrex',
                      '_cache',
                      'unicodedata',
                      'pypy-translation-snapshot'):
        return False
    return True

def getpypycounter():
    filecounter = countloc.FileCounter()
    root = py.path.local(pypydir)
    filecounter.addrecursive(root, isfile, rec=recpypy)
    return filecounter

class CounterModel:
    def __init__(self, pypycounter):
        self.counter = pypycounter
        self.totallines = pypycounter.numlines
        self.totalfiles = pypycounter.numfiles
        self.testlines = pypycounter.getnumlines(istestfile)
        self.testfiles = pypycounter.getnumfiles(istestfile)
        self.notestlines = pypycounter.getnumlines(notistestfile)
        self.notestfiles = pypycounter.getnumfiles(notistestfile)
        self.doclines = pypycounter.getnumlines(isdocfile)
        self.docfiles = pypycounter.getnumfiles(isdocfile)

#
# rendering
#
def row(*args):
    return html.tr([html.td(arg) for arg in args])

def percent(x, y):
    return "%.2f%%" % (x / (y/100.0))

def viewlocsummary(model):
    t = html.table(
        row("total number of lines", model.totallines, raw(" ")),
        row("number of testlines", model.testlines,
            percent(model.testlines, model.totallines)),
        row("number of non-testlines", model.notestlines,
            percent(model.notestlines, model.totallines)),

        row("total number of files", model.totalfiles, raw(" ")),
        row("number of testfiles", model.testfiles,
            percent(model.testfiles, model.totalfiles)),
        row("number of non-testfiles", model.notestfiles,
            percent(model.notestfiles, model.totalfiles)),
        )
    if model.docfiles:
        t.append(row("number of docfiles", model.docfiles,
            percent(model.docfiles, model.totalfiles)))
        t.append(row("number of doclines", model.doclines,
            percent(model.doclines, model.totallines)))
    return t

def viewloclist(model):
    t = html.table()
    d = model.counter.file2numlines
    paths = d.items()
    paths.sort(lambda x, y: -cmp(x[1], y[1]))  # sort by numlines
    for p, numlines in paths:
        if numlines < 3:
            continue
        t.append(row(p.relto(pypydir.dirpath()), numlines))
    return t

def viewsubdirs(model):
    t = html.table()
    for p in pypydir.listdir():
        if p.basename in '_cache .svn'.split():
            continue
        if p.check(dir=1):
            counter = countloc.FileCounter()
            counter.addrecursive(p, isfile, recpypy)
            model = CounterModel(counter)
            t.append(row(html.h2(p.relto(pypydir.dirpath()))))
            t.append(viewlocsummary(model))
            t.append(viewloclist(model))
    return t

if __name__ == '__main__':
    if len(py.std.sys.argv) >= 2:
        target = py.path.local(py.std.sys.argv[1])
    else:
        target = py.path.local('index.html')
    print "writing source statistics to", target
    pypycounter = getpypycounter()
    model = CounterModel(pypycounter)
    rev = py.path.svnwc(pypydir).info().rev
    html = py.xml.html
    doc = html.html(
        html.head(
            html.title("PyPy Statistics %d" % rev),
        ),
        html.body(
            html.h2("rev %d PyPy Summary of Files and Lines" % rev),
            viewlocsummary(model),
            html.h2("Details on first-level subdirectories"),
            viewsubdirs(model),
            html.h3("PyPy Full List Files and Lines"),
            viewloclist(model),
            html.p("files with less than 3 lines ignored")
        )
    )
    content = doc.unicode(indent=2).encode('utf8')
    target.write(content)