SCons / bin / linecount

#!/usr/bin/env python
#
# __COPYRIGHT__
#
# 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 begins with
# 'test_' or ends in 'Tests.py', or anything under the test/ directory
# that ends in '.py'.  Note that runtest.py script does *not*, by default,
# consider the files that begin with 'test_' to be tests, because they're
# tests of SCons packaging and installation, not functional tests of
# SCons code.
#
# A source file is anything under the src/engine/ or src/script/
# directories that ends in '.py' but does NOT begin with 'test_'
# or end in 'Tests.py'.
#
# 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.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import os.path
import string

tests = []
sources = []

def is_test(x):
    return x[:5] == 'test_' or x[-8:] == 'Tests.py'
def is_python(x):
    return x[-3:] == '.py'

def t(arg, dirname, names):
    names = filter(is_test, 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(is_python, 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: is_python(n) and not is_test(n), 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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.