Last active
December 10, 2015 00:18
-
-
Save fwenzel/4350046 to your computer and use it in GitHub Desktop.
A simple script to determine what portions of a web app are HTML/CSS/JS. Uses the tool ``cloc``.
This file contains hidden or 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 | |
"""This requires Python 2.7. Deal with it.""" | |
import shlex | |
import sys | |
import xml.etree.ElementTree as ET | |
from subprocess import check_output | |
# Language -> [extension, ...] mapping | |
FORCE_LANGS = { | |
'HTML': ('jade', 'ejs'), | |
'Javascript': ('json',) | |
} | |
err = lambda msg: sys.stderr.write('%s\n' % msg) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
err('Usage: `%s www`, where www is your web app directory.' % sys.argv[0]) | |
sys.exit(1) | |
force_langs = ' '.join('--force-lang="%s",%s' % (lang, e) | |
for lang in FORCE_LANGS for e in FORCE_LANGS[lang]) | |
out = check_output(shlex.split('cloc --quiet --xml %s %s' % ( | |
force_langs, sys.argv[1]))) | |
# Parse the results, extract HTML, CSS, JS. | |
tree = ET.fromstring(out.strip()) | |
langs = {} | |
total = 0 | |
for lang in ('HTML', 'CSS', 'Javascript'): # Javascript, sic. | |
node = tree.find(r".//language[@name='%s']" % lang) | |
if node is not None: | |
langs[lang] = int(node.attrib['code']) | |
total += int(node.attrib['code']) | |
# Calculate, display. | |
print "Note: Rounding errors are possible." | |
for lang in ('HTML', 'CSS', 'Javascript'): | |
count = langs.get(lang) or 0 | |
if lang == 'Javascript': lang = 'JavaScript' # Seriously. | |
print "%s: %d%%" % (lang, round(float(count) / total * 100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment