Last active
June 10, 2017 04:52
-
-
Save upvalue/81122e892c321970d854f895491cd258 to your computer and use it in GitHub Desktop.
count comments as a percentage of LOC with cloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# cloccomment.py - using cloc, return percentage of code that is comments | |
# usage: cloccomment <arguments to be passed to cloc> | |
import sys | |
import subprocess | |
args = sys.argv[1:] | |
results = subprocess.check_output(['cloc'] + args).decode('utf-8') | |
print(results) | |
results = results.split('\n') | |
i = 0 | |
while i < len(results): | |
line = results[i] | |
if line.startswith('Language'): | |
i += 2 | |
break | |
i += 1 | |
while i < len(results): | |
line = results[i] | |
i += 1 | |
if line.startswith('-'): | |
continue | |
if line.strip() == '': | |
break | |
vals = line.split() | |
# print(vals) | |
lang = ' '.join(vals[:-4]) | |
comments = int(vals[-2]) | |
loc = int(vals[-1]) | |
pct = (float(comments) / (loc + comments)) * 100 | |
fmt = (lang, comments, comments + loc, round(pct, 2)) | |
print("%s %s out of %s were comments (%s%%)" % fmt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment