summaryrefslogtreecommitdiffstats
path: root/bin/linecount
blob: e344e2a2a8661c801255ee92022d284690122853 (plain)
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
#!/usr/bin/env python
#
# Count statistics about SCons test and source files.  This must be run
# against a fully-populated tree (for example, one that's been freshly
# checked out).
#
# A test file is anything under the src/ directory that ends in
# 'Tests.py', or anything under the test/ directory that ends in '.py'.
#
# A source file is anything under the src/engine/ or src/script/
# directories that ends in '.py' but does NOT end in 'Tests.py'.  (We
# should probably ignore the stuff in src/engine/SCons/Optik, since it
# doesn't originate with SCons, but what the hell.)
#
# We report the number of tests and sources, the total number of lines
# in each category, the number of non-blank lines, and the number of
# non-comment lines.  The last figure (non-comment) lines is the most
# interesting one for most purposes.
#

import os.path
import string

tests = []
sources = []

def t(arg, dirname, names):
    names = filter(lambda n: n[-8:] == 'Tests.py', names)
    arg.extend(map(lambda n, d=dirname: os.path.join(d, n), names))
os.path.walk('src', t, tests)

def p(arg, dirname, names):
    names = filter(lambda n: n[-3:] == '.py', names)
    arg.extend(map(lambda n, d=dirname: os.path.join(d, n), names))
os.path.walk('test', p, tests)

def s(arg, dirname, names):
    names = filter(lambda n: n[-3:] == '.py' and n[-8:] != 'Tests.py', names)
    arg.extend(map(lambda n, d=dirname: os.path.join(d, n), names))
os.path.walk('src/engine', s, sources)
os.path.walk('src/script', s, sources)

def gather(files):
    lines = []
    for file in files:
        lines.extend(open(file).readlines())
    return lines

tlines = map(string.lstrip, gather(tests))
slines = map(string.lstrip, gather(sources))

nbtl = filter(lambda x: x != '', tlines)
nbsl = filter(lambda x: x != '', slines)

nctl = filter(lambda x: x[0] != '#', nbtl)
ncsl = filter(lambda x: x[0] != '#', nbsl)

def ratio(over, under):
    return "%.2f" % (float(len(over)) / float(len(under)))

rfiles = ratio(tests, sources)
rlines = ratio(tlines, slines)
rnonblank = ratio(nbtl, nbsl)
rnoncomment = ratio(nctl, ncsl)

fmt = "%-8s  %12s  %12s  %12s  %12s"

print fmt % ('', 'files', 'lines', 'non-blank', 'non-comment')
print fmt % ('tests:', len(tests), len(tlines), len(nbtl), len(nctl))
print fmt % ('sources:', len(sources), len(slines), len(nbsl), len(ncsl))
print fmt % ('ratio:', rfiles, rlines, rnonblank, rnoncomment)