File: source_file_stats.py

package info (click to toggle)
trilinos 16.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 994,012 kB
  • sloc: cpp: 3,764,859; ansic: 425,011; fortran: 160,684; python: 101,476; xml: 74,329; sh: 37,044; makefile: 22,641; perl: 7,525; f90: 6,424; csh: 5,285; objc: 2,620; lex: 1,646; lisp: 810; yacc: 603; javascript: 552; awk: 364; ml: 281; php: 145
file content (81 lines) | stat: -rwxr-xr-x 2,664 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python

"""
Python module/program that counts source code stats for a series of directories
for files of different types.
"""

# ToDo: I want to add the following statistics as well:
# 0) Count the number of semi-colons
# 1) Count the number of tokens in all of the source code lines
# 2) Count the number of total non-space chars in all of the source code lines
# 3) Count the number of total non-space chars in all of the documentation lines
# 4) Separate out the copyright header comment lines from the other comment
#    lines and print the line counts for these separately

import sys
import os
import re

reBeginCStyleCommentLine = re.compile(r"^\s*/\*.*")
reEndCStyleCommentLine = re.compile(r".*\*/.*")
reCppStyleCommentLine = re.compile(r"^\s*//.*")
reBlankLine = re.compile(r"^\s*$")

def countLines(fileName):
  #
  file = open(fileName)
  #
  numBlankLines = 0
  numCopyrightHeaderLines = 0
  numCommentLines = 0
  numCodeLines = 0
  numTotalLines = 0
  numSrcTokens = 0
  numSrcChars = 0
  numDocChars = 0
  #
  activeCommentBlock = 0
  #
  for line in file:
    line = line.rstrip()
    #print "\nline = \"" + line + "\"\n"
    numTotalLines += 1
    if not activeCommentBlock:
      #print "\nComment block is not active!\n";
      if re.match(reBeginCStyleCommentLine,line):
        #print "\nMatches the beginning of a C-style comment line!\n";
        numCommentLines += 1
        if not re.match(reEndCStyleCommentLine,line):
          #print "\nDoes not match the end of a C-stlye comment line!\n";
          activeCommentBlock = 1
        #else: print "\nMatches the end of a C-stlye comment line!\n";
      elif re.match(reCppStyleCommentLine,line):
        #print "\nMatches a C++-style comment line!\n";
        numCommentLines += 1
      elif re.match(reBlankLine,line):
        #print "\nMatches a blank line!\n";
        numBlankLines += 1
      else:
        #print "\nMust be a code line!\n";
        numCodeLines += 1
      # end if
    else:
      #print "\nComment block is active!\n";
      numCommentLines += 1
      if re.match(reEndCStyleCommentLine,line):
        #print "\nMatches end of C-sytle comment line!\n";
        activeCommentBlock = 0
    # end if
  # end for
  return (numBlankLines,numCommentLines,numCodeLines,numTotalLines)

if __name__ == '__main__':
  (numBlankLines,numCommentLines,numCodeLines,numTotalLines) \
    = countLines(sys.argv[1])
  print "File: ", os.path.basename(sys.argv[1])
  print "Number of code lines     =", numCodeLines
  print "Number of comment lines  =", numCommentLines
  print "Number of blank lines    =", numBlankLines
  print "numTotalLines (", numTotalLines, ")"