Created
November 6, 2013 07:12
-
-
Save p4ul/7332160 to your computer and use it in GitHub Desktop.
Gets the average lines of code for each file type. Execute this script in the console under a directory that contains your code and it will output a table that lists lines of code for each file type. There is some basic skipping of things like minified files and images. Requires textable (https://pypi.python.org/pypi/texttable) install via `pip …
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/python | |
import texttable as tt | |
import fnmatch | |
import os | |
import operator | |
from collections import Counter | |
import mimetypes | |
def file_len(fname): | |
linecount = 0 | |
linelength = 0 | |
i = 0 | |
with open(fname) as f: | |
for i, l in enumerate(f): | |
# print len(l) | |
linelength += len(l) | |
pass | |
linecount = i + 1 | |
return linelength / linecount | |
matches = [] | |
for root, dirnames, filenames in os.walk('sites'): | |
# ignore .git and .svn | |
if root.find('.'): | |
pass | |
for filename in fnmatch.filter(filenames, '*.*'): | |
mimetype = str(mimetypes.guess_type(filename)[0]) | |
# ignore images | |
if mimetype.find('image') >= 0 : | |
continue | |
# ignore minified files | |
if filename.find('.min.') >= 0 : | |
continue | |
# print filename + " " + mimetype | |
fileName, fileExtension = os.path.splitext(filename) | |
line_length = file_len(os.path.join(os.getcwd(), root,filename)) | |
#ignore crazy long lines | |
if line_length > 120: | |
continue | |
matches.append((fileExtension,line_length)) | |
filecounts = {} | |
linecounts = {} | |
for lang, linelength in matches: | |
if lang in filecounts : | |
filecounts[lang] = filecounts[lang] + 1 | |
linecounts[lang] = linecounts[lang] + linelength | |
else : | |
filecounts[lang] = 1 | |
linecounts[lang] = linelength | |
tabledata = [] | |
for i in linecounts: | |
tabledata.append((i, filecounts[i], linecounts[i]/filecounts[i])) | |
# sort by avg line length descending | |
tabledata = sorted(tabledata, key=operator.itemgetter(2), reverse=True) | |
tab = tt.Texttable() | |
tab.add_rows(tabledata) | |
tab.set_cols_align(['r','r', 'r']) | |
tab.header(['extension', 'number of files', 'avg line length']) | |
print tab.draw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pip install texttable with "tt"