Optimize out N*M suffix matching in Builder.py.
[scons.git] / bin / linecount
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Count statistics about SCons test and source files.  This must be run
6 # against a fully-populated tree (for example, one that's been freshly
7 # checked out).
8 #
9 # A test file is anything under the src/ directory that begins with
10 # 'test_' or ends in 'Tests.py', or anything under the test/ directory
11 # that ends in '.py'.  Note that runtest.py script does *not*, by default,
12 # consider the files that begin with 'test_' to be tests, because they're
13 # tests of SCons packaging and installation, not functional tests of
14 # SCons code.
15 #
16 # A source file is anything under the src/engine/ or src/script/
17 # directories that ends in '.py' but does NOT begin with 'test_'
18 # or end in 'Tests.py'.  (We should probably ignore the stuff in
19 # src/engine/SCons/Optik, since it doesn't originate with SCons, but
20 # what the hell.)
21 #
22 # We report the number of tests and sources, the total number of lines
23 # in each category, the number of non-blank lines, and the number of
24 # non-comment lines.  The last figure (non-comment) lines is the most
25 # interesting one for most purposes.
26 #
27
28 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
29
30 import os.path
31 import string
32
33 tests = []
34 sources = []
35
36 def is_test(x):
37     return x[:5] == 'test_' or x[-8:] == 'Tests.py'
38 def is_python(x):
39     return x[-3:] == '.py'
40
41 def t(arg, dirname, names):
42     names = filter(is_test, names)
43     arg.extend(map(lambda n, d=dirname: os.path.join(d, n), names))
44 os.path.walk('src', t, tests)
45
46 def p(arg, dirname, names):
47     names = filter(is_python, names)
48     arg.extend(map(lambda n, d=dirname: os.path.join(d, n), names))
49 os.path.walk('test', p, tests)
50
51 def s(arg, dirname, names):
52     names = filter(lambda n: is_python(n) and not is_test(n), names)
53     arg.extend(map(lambda n, d=dirname: os.path.join(d, n), names))
54 os.path.walk('src/engine', s, sources)
55 os.path.walk('src/script', s, sources)
56
57 def gather(files):
58     lines = []
59     for file in files:
60         lines.extend(open(file).readlines())
61     return lines
62
63 tlines = map(string.lstrip, gather(tests))
64 slines = map(string.lstrip, gather(sources))
65
66 nbtl = filter(lambda x: x != '', tlines)
67 nbsl = filter(lambda x: x != '', slines)
68
69 nctl = filter(lambda x: x[0] != '#', nbtl)
70 ncsl = filter(lambda x: x[0] != '#', nbsl)
71
72 def ratio(over, under):
73     return "%.2f" % (float(len(over)) / float(len(under)))
74
75 rfiles = ratio(tests, sources)
76 rlines = ratio(tlines, slines)
77 rnonblank = ratio(nbtl, nbsl)
78 rnoncomment = ratio(nctl, ncsl)
79
80 fmt = "%-8s  %12s  %12s  %12s  %12s"
81
82 print fmt % ('', 'files', 'lines', 'non-blank', 'non-comment')
83 print fmt % ('tests:', len(tests), len(tlines), len(nbtl), len(nctl))
84 print fmt % ('sources:', len(sources), len(slines), len(nbsl), len(ncsl))
85 print fmt % ('ratio:', rfiles, rlines, rnonblank, rnoncomment)