Skip to content

Instantly share code, notes, and snippets.

@upvalue
Last active June 10, 2017 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save upvalue/81122e892c321970d854f895491cd258 to your computer and use it in GitHub Desktop.
Save upvalue/81122e892c321970d854f895491cd258 to your computer and use it in GitHub Desktop.
count comments as a percentage of LOC with cloc
#!/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