~crunch.io/ubuntu/precise/codespeak-lib/unstable

« back to all changes in this revision

Viewing changes to py/_cmdline/pycountloc.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-08-01 16:24:01 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100801162401-g37v49d1p148alpm
Tags: 1.3.3-1
* New upstream release.
* Bump Standards-Version to 3.9.1.
* Fix typo in py.test manpage.
* Prefer Breaks: over Conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
 
3
 
# hands on script to compute the non-empty Lines of Code 
4
 
# for tests and non-test code 
 
3
# hands on script to compute the non-empty Lines of Code
 
4
# for tests and non-test code
5
5
 
6
6
"""\
7
7
py.countloc [PATHS]
17
17
    parser = py.std.optparse.OptionParser(usage=__doc__)
18
18
    (options, args) = parser.parse_args()
19
19
    countloc(args)
20
 
   
 
20
 
21
21
def nodot(p):
22
22
    return p.check(dotfile=0)
23
23
 
24
 
class FileCounter(object):  
 
24
class FileCounter(object):
25
25
    def __init__(self):
26
26
        self.file2numlines = {}
27
27
        self.numlines = 0
28
28
        self.numfiles = 0
29
29
 
30
30
    def addrecursive(self, directory, fil="*.py", rec=nodot):
31
 
        for x in directory.visit(fil, rec): 
 
31
        for x in directory.visit(fil, rec):
32
32
            self.addfile(x)
33
33
 
34
34
    def addfile(self, fn, emptylines=False):
39
39
            for i in fn.readlines():
40
40
                if i.strip():
41
41
                    s += 1
42
 
        self.file2numlines[fn] = s 
 
42
        self.file2numlines[fn] = s
43
43
        self.numfiles += 1
44
44
        self.numlines += s
45
45
 
46
 
    def getnumlines(self, fil): 
 
46
    def getnumlines(self, fil):
47
47
        numlines = 0
48
48
        for path, value in self.file2numlines.items():
49
 
            if fil(path): 
 
49
            if fil(path):
50
50
                numlines += value
51
 
        return numlines 
 
51
        return numlines
52
52
 
53
 
    def getnumfiles(self, fil): 
 
53
    def getnumfiles(self, fil):
54
54
        numfiles = 0
55
55
        for path in self.file2numlines:
56
 
            if fil(path): 
 
56
            if fil(path):
57
57
                numfiles += 1
58
58
        return numfiles
59
59
 
61
61
    if locations is None:
62
62
        localtions = [py.path.local()]
63
63
    counter = FileCounter()
64
 
    for loc in locations: 
 
64
    for loc in locations:
65
65
        counter.addrecursive(loc, '*.py', rec=nodot)
66
66
 
67
67
    def istestfile(p):
68
68
        return p.check(fnmatch='test_*.py')
69
69
    isnottestfile = lambda x: not istestfile(x)
70
70
 
71
 
    numfiles = counter.getnumfiles(isnottestfile) 
72
 
    numlines = counter.getnumlines(isnottestfile) 
 
71
    numfiles = counter.getnumfiles(isnottestfile)
 
72
    numlines = counter.getnumlines(isnottestfile)
73
73
    numtestfiles = counter.getnumfiles(istestfile)
74
74
    numtestlines = counter.getnumlines(istestfile)
75
 
   
 
75
 
76
76
    return counter, numfiles, numlines, numtestfiles, numtestlines
77
77
 
78
78
def countloc(paths=None):
86
86
    items.sort(lambda x,y: cmp(x[1], y[1]))
87
87
    for x, y in items:
88
88
        print("%3d %30s" % (y,x))
89
 
    
 
89
 
90
90
    print("%30s %3d" %("number of testfiles", numtestfiles))
91
91
    print("%30s %3d" %("number of non-empty testlines", numtestlines))
92
92
    print("%30s %3d" %("number of files", numfiles))