Merged revisions 2136-2200,2202-2290,2292-2301 via svnmerge from
[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'.
19 #
20 # We report the number of tests and sources, the total number of lines
21 # in each category, the number of non-blank lines, and the number of
22 # non-comment lines.  The last figure (non-comment) lines is the most
23 # interesting one for most purposes.
24 #
25
26 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
27
28 import os.path
29 import string
30
31 tests = []
32 sources = []
33
34 def is_test(x):
35     return x[:5] == 'test_' or x[-8:] == 'Tests.py'
36 def is_python(x):
37     return x[-3:] == '.py'
38
39 def t(arg, dirname, names):
40     names = filter(is_test, names)
41     arg.extend(map(lambda n, d=dirname: os.path.join(d, n), names))
42 os.path.walk('src', t, tests)
43
44 def p(arg, dirname, names):
45     names = filter(is_python, names)
46     arg.extend(map(lambda n, d=dirname: os.path.join(d, n), names))
47 os.path.walk('test', p, tests)
48
49 def s(arg, dirname, names):
50     names = filter(lambda n: is_python(n) and not is_test(n), names)
51     arg.extend(map(lambda n, d=dirname: os.path.join(d, n), names))
52 os.path.walk('src/engine', s, sources)
53 os.path.walk('src/script', s, sources)
54
55 def gather(files):
56     lines = []
57     for file in files:
58         lines.extend(open(file).readlines())
59     return lines
60
61 tlines = map(string.lstrip, gather(tests))
62 slines = map(string.lstrip, gather(sources))
63
64 nbtl = filter(lambda x: x != '', tlines)
65 nbsl = filter(lambda x: x != '', slines)
66
67 nctl = filter(lambda x: x[0] != '#', nbtl)
68 ncsl = filter(lambda x: x[0] != '#', nbsl)
69
70 def ratio(over, under):
71     return "%.2f" % (float(len(over)) / float(len(under)))
72
73 rfiles = ratio(tests, sources)
74 rlines = ratio(tlines, slines)
75 rnonblank = ratio(nbtl, nbsl)
76 rnoncomment = ratio(nctl, ncsl)
77
78 fmt = "%-8s  %12s  %12s  %12s  %12s"
79
80 print fmt % ('', 'files', 'lines', 'non-blank', 'non-comment')
81 print fmt % ('tests:', len(tests), len(tlines), len(nbtl), len(nctl))
82 print fmt % ('sources:', len(sources), len(slines), len(nbsl), len(ncsl))
83 print fmt % ('ratio:', rfiles, rlines, rnonblank, rnoncomment)