SHARE
TWEET

Productivity Analyser

a guest Mar 22nd, 2010 419 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2.  
  3. def recurse_apply(current, report = []):
  4.         parts = os.listdir(current)
  5.         for part in parts:
  6.                 path = os.path.join(current, part)
  7.                 if os.path.isdir(path):
  8.                         recurse_apply(path, report)
  9.                 else:
  10.                         apply(path, report)
  11.         return report
  12.  
  13. def apply(path, report = []):
  14.         if filter(path):
  15.                 # determine file type based on extension
  16.                 if path.endswith("xml"):
  17.                         (lines, comments, blanks) = apply_language(path, xml_line_type)
  18.                         print path + " - " + str((lines, comments, blanks))
  19.                         report.append(("xml", path, lines, comments, blanks))
  20.                 elif path.endswith("xsl"):
  21.                         (lines, comments, blanks) = apply_language(path, xml_line_type)
  22.                         print path + " - " + str((lines, comments, blanks))
  23.                         report.append(("xslt", path, lines, comments, blanks))
  24.                 elif path.endswith("java"):
  25.                         (lines, comments, blanks) = apply_language(path, java_line_type)
  26.                         print path + " - " + str((lines, comments, blanks))
  27.                         report.append(("java", path, lines, comments, blanks))
  28.                 elif path.endswith("pm") or path.endswith("pl"):
  29.                         (lines, comments, blanks) = apply_language(path, perl_line_type)
  30.                         print path + " - " + str((lines, comments, blanks))
  31.                         report.append(("perl", path, lines, comments, blanks))
  32.                 elif path.endswith("jsp"):
  33.                         (lines, comments, blanks) = apply_language(path, jsp_line_type)
  34.                         print path + " - " + str((lines, comments, blanks))
  35.                         report.append(("jsp", path, lines, comments, blanks))
  36.  
  37. def apply_language(path, type_func):
  38.         f = open(path, "r")
  39.         struct = [type_func(line) for line in f]
  40.         blanks = 0
  41.         comments = 0
  42.         lines = 0
  43.         for v in struct:
  44.                 if v == 0:
  45.                         blanks += 1
  46.                 elif v == 1:
  47.                         lines += 1
  48.                 elif v == 2:
  49.                         comments += 1
  50.         return (lines, comments, blanks)
  51.  
  52. def xml_line_type(line):
  53.         if line is None:
  54.                 return 0;
  55.         stripped = line.strip()
  56.         if stripped == '':
  57.                 #print stripped + " = blank line"
  58.                 return 0
  59.         elif stripped.startswith("<!--") or stripped.startswith("-"):
  60.                 #print stripped + " = comment line"
  61.                 return 2
  62.         else:
  63.                 #print stripped + " = code line"
  64.                 return 1       
  65.  
  66. def java_line_type(line):
  67.         if line is None:
  68.                 return 0;
  69.         stripped = line.strip()
  70.         if stripped == '':
  71.                 #print stripped + " = blank line"
  72.                 return 0
  73.         elif stripped.startswith("//") or stripped.startswith("*") or stripped.startswith("/*"):
  74.                 #print stripped + " = comment line"
  75.                 return 2
  76.         else:
  77.                 #print stripped + " = code line"
  78.                 return 1
  79.  
  80. def perl_line_type(line):
  81.         if line is None:
  82.                 return 0;
  83.         stripped = line.strip()
  84.         if stripped == '':
  85.                 #print stripped + " = blank line"
  86.                 return 0
  87.         elif stripped.startswith("#") or stripped.startswith("="):
  88.                 #print stripped + " = comment line"
  89.                 return 2
  90.         else:
  91.                 #print stripped + " = code line"
  92.                 return 1
  93.  
  94. def jsp_line_type(line):
  95.         if line is None:
  96.                 return 0;
  97.         stripped = line.strip()
  98.         if stripped == '':
  99.                 #print stripped + " = blank line"
  100.                 return 0
  101.         elif stripped.startswith("//") or stripped.startswith("*") or stripped.startswith("/*") or stripped.startswith("<!--"):
  102.                 #print stripped + " = comment line"
  103.                 return 2
  104.         else:
  105.                 #print stripped + " = code line"
  106.                 return 1
  107.  
  108. def filter(path):
  109.         # we don't care about "svn" file paths
  110.         if path.count(".svn") > 0:
  111.                 return False
  112.         return True
  113.  
  114. def output(type, report, filecount):
  115.         (code, com, blank) = report
  116.         print type + " (" + str(filecount) + " Files) :: Lines of Code: " + str(code) + "; Lines of Inline Comments: " + str(com) + "; Blank Lines: " + str(blank)
  117.  
  118. productivity = recurse_apply(".")
  119.  
  120. xsl_report = (0, 0, 0)
  121. xsl_count = 0
  122. xml_report = (0, 0, 0)
  123. xml_count = 0
  124. java_report = (0, 0, 0)
  125. java_count = 0
  126. perl_report = (0, 0, 0)
  127. perl_count = 0
  128. jsp_report = (0, 0, 0)
  129. jsp_count = 0
  130.  
  131. for record in productivity:
  132.         (type, path, code, comments, blanks) = record
  133.         if type == "xml":
  134.                 (xcode, xcom, xb) = xml_report
  135.                 xml_report = (xcode + code, xcom + comments, xb + blanks)
  136.                 xml_count += 1
  137.         elif type == "xslt":
  138.                 (xcode, xcom, xb) = xsl_report
  139.                 xsl_report = (xcode + code, xcom + comments, xb + blanks)
  140.                 xsl_count += 1
  141.         elif type == "java":
  142.                 (jcode, jcom, jb) = java_report
  143.                 java_report = (jcode + code, jcom + comments, jb + blanks)
  144.                 java_count += 1
  145.         elif type == "jsp":
  146.                 (jcode, jcom, jb) = jsp_report
  147.                 jsp_report = (jcode + code, jcom + comments, jb + blanks)
  148.                 jsp_count += 1
  149.         elif type == "perl":
  150.                 (pcode, pcom, pb) = perl_report
  151.                 perl_report = (pcode + code, pcom + comments, pb + blanks)
  152.                 perl_count += 1
  153.  
  154. output("XML", xml_report, xml_count)
  155. output("XSLT", xsl_report, xsl_count)
  156. output("JAVA", java_report, java_count)
  157. output("JSP", jsp_report, jsp_count)
  158. output("PERL", perl_report, perl_count)
RAW Paste Data
Top