root/tags/release-0.6.2/scripts/count_code.py

Revision 1, 1.0 kB (checked in by mjoc, 2 years ago)

Initial import.

Line 
1 #!/usr/bin/env python
2
3 # Count OpenDict Code Lines
4 # Copyright (c) Martynas Jocius <mjoc@delfi.lt>
5 # Licensed under the GNU GPL.
6
7 import sys
8 import glob
9
10 def count(file):
11
12     fd = open(file)
13
14     all = 0
15     code = 0
16
17     for line in fd.readlines():
18         all += 1
19         if line.strip() == "":
20             continue
21         if line.find("#") > -1:
22             if line[0] == "#":
23                 continue
24             elif line[0:line.index("#")].strip() == "":
25                 continue
26         code += 1
27
28     print "%s:" % file
29     print "   Number of lines: ", all
30     print "   Number of code lines:", code
31
32     return (all, code)
33
34 if __name__ == "__main__":
35     if len(sys.argv) == 1:
36         print "Usage: %s <file> [file1] [file2] ..." % sys.argv[0]
37         sys.exit(1)
38
39     lines = 0
40     code = 0
41    
42     for arg in sys.argv[1:]:
43         for file in glob.glob(arg):
44             new = count(file)
45             lines += new[0]
46             code += new[1]
47
48     print "\nTotal number of lines: %d" % lines
49     print "Total number of code lines: %d" % code
Note: See TracBrowser for help on using the browser.