Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- def recurse_apply(current, report = []):
- parts = os.listdir(current)
- for part in parts:
- path = os.path.join(current, part)
- if os.path.isdir(path):
- recurse_apply(path, report)
- else:
- apply(path, report)
- return report
- def apply(path, report = []):
- if filter(path):
- # determine file type based on extension
- if path.endswith("xml"):
- (lines, comments, blanks) = apply_language(path, xml_line_type)
- print path + " - " + str((lines, comments, blanks))
- report.append(("xml", path, lines, comments, blanks))
- elif path.endswith("xsl"):
- (lines, comments, blanks) = apply_language(path, xml_line_type)
- print path + " - " + str((lines, comments, blanks))
- report.append(("xslt", path, lines, comments, blanks))
- elif path.endswith("java"):
- (lines, comments, blanks) = apply_language(path, java_line_type)
- print path + " - " + str((lines, comments, blanks))
- report.append(("java", path, lines, comments, blanks))
- elif path.endswith("pm") or path.endswith("pl"):
- (lines, comments, blanks) = apply_language(path, perl_line_type)
- print path + " - " + str((lines, comments, blanks))
- report.append(("perl", path, lines, comments, blanks))
- elif path.endswith("jsp"):
- (lines, comments, blanks) = apply_language(path, jsp_line_type)
- print path + " - " + str((lines, comments, blanks))
- report.append(("jsp", path, lines, comments, blanks))
- def apply_language(path, type_func):
- f = open(path, "r")
- struct = [type_func(line) for line in f]
- blanks = 0
- comments = 0
- lines = 0
- for v in struct:
- if v == 0:
- blanks += 1
- elif v == 1:
- lines += 1
- elif v == 2:
- comments += 1
- return (lines, comments, blanks)
- def xml_line_type(line):
- if line is None:
- return 0;
- stripped = line.strip()
- if stripped == '':
- #print stripped + " = blank line"
- return 0
- elif stripped.startswith("<!--") or stripped.startswith("-"):
- #print stripped + " = comment line"
- return 2
- else:
- #print stripped + " = code line"
- return 1
- def java_line_type(line):
- if line is None:
- return 0;
- stripped = line.strip()
- if stripped == '':
- #print stripped + " = blank line"
- return 0
- elif stripped.startswith("//") or stripped.startswith("*") or stripped.startswith("/*"):
- #print stripped + " = comment line"
- return 2
- else:
- #print stripped + " = code line"
- return 1
- def perl_line_type(line):
- if line is None:
- return 0;
- stripped = line.strip()
- if stripped == '':
- #print stripped + " = blank line"
- return 0
- elif stripped.startswith("#") or stripped.startswith("="):
- #print stripped + " = comment line"
- return 2
- else:
- #print stripped + " = code line"
- return 1
- def jsp_line_type(line):
- if line is None:
- return 0;
- stripped = line.strip()
- if stripped == '':
- #print stripped + " = blank line"
- return 0
- elif stripped.startswith("//") or stripped.startswith("*") or stripped.startswith("/*") or stripped.startswith("<!--"):
- #print stripped + " = comment line"
- return 2
- else:
- #print stripped + " = code line"
- return 1
- def filter(path):
- # we don't care about "svn" file paths
- if path.count(".svn") > 0:
- return False
- return True
- def output(type, report, filecount):
- (code, com, blank) = report
- print type + " (" + str(filecount) + " Files) :: Lines of Code: " + str(code) + "; Lines of Inline Comments: " + str(com) + "; Blank Lines: " + str(blank)
- productivity = recurse_apply(".")
- xsl_report = (0, 0, 0)
- xsl_count = 0
- xml_report = (0, 0, 0)
- xml_count = 0
- java_report = (0, 0, 0)
- java_count = 0
- perl_report = (0, 0, 0)
- perl_count = 0
- jsp_report = (0, 0, 0)
- jsp_count = 0
- for record in productivity:
- (type, path, code, comments, blanks) = record
- if type == "xml":
- (xcode, xcom, xb) = xml_report
- xml_report = (xcode + code, xcom + comments, xb + blanks)
- xml_count += 1
- elif type == "xslt":
- (xcode, xcom, xb) = xsl_report
- xsl_report = (xcode + code, xcom + comments, xb + blanks)
- xsl_count += 1
- elif type == "java":
- (jcode, jcom, jb) = java_report
- java_report = (jcode + code, jcom + comments, jb + blanks)
- java_count += 1
- elif type == "jsp":
- (jcode, jcom, jb) = jsp_report
- jsp_report = (jcode + code, jcom + comments, jb + blanks)
- jsp_count += 1
- elif type == "perl":
- (pcode, pcom, pb) = perl_report
- perl_report = (pcode + code, pcom + comments, pb + blanks)
- perl_count += 1
- output("XML", xml_report, xml_count)
- output("XSLT", xsl_report, xsl_count)
- output("JAVA", java_report, java_count)
- output("JSP", jsp_report, jsp_count)
- output("PERL", perl_report, perl_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement