Skip to content

Instantly share code, notes, and snippets.

@fwenzel
Last active December 10, 2015 00:18
Show Gist options
  • Save fwenzel/4350046 to your computer and use it in GitHub Desktop.
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``.
#!/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."
print
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