Created
July 7, 2015 21:55
-
-
Save Halicea/31d8e1ba9a2fa27474b5 to your computer and use it in GitHub Desktop.
Code Statistics for project
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
import os | |
import sys | |
import codecs | |
ignored_dirs = [ | |
'node_modules', 'bower_components', 'typings', | |
'bin', 'obj', 'Debug', 'Release', | |
'webview', 'html5', 'checkbox', 'jenkins', 'resources', | |
'Legacy', 'Old', 'PDFViewCtrlTools'] | |
supported_exts = [ | |
['.py', 'Python'], ['.php', 'PHP'], ['.pl', 'Perl'], | |
['.java', 'Java'], ['.m', 'Objective C'], ['.cs', 'C#'], | |
['.c', 'C'], ['.cpp', 'C++'], ['.asm', 'Assebmbler'], | |
['.js', 'Javascript', ['.ts', '.coffee']], ['.ts', 'Typescript'], | |
['.html', 'HTML'], ['.jade', 'Jade'], ['.coffee', 'Coffeescript'], | |
['.xml', 'Xml'], ['.json', 'Json'], ['.css', 'CSS'], ['.scss', 'SCSS']] | |
exts = supported_exts | |
def main(): | |
opts = set_arg_options() | |
files = collect_files(opts['directory']) | |
all_types = group_by_type(files) | |
file_counts = [len(x[1]) for x in all_types] | |
line_counts = [sum([u[2] for u in x[1]]) for x in all_types] | |
ext_names = [get_ext_name(x[0]) for x in all_types] | |
if '-v' in sys.argv: | |
for t in all_types: | |
print_file_list(t[1], get_ext_name(t[0])) | |
print_comparison(file_counts, ext_names, 'File Stats') | |
print_comparison(line_counts, ext_names, 'LineStats') | |
def set_arg_options(): | |
global exts | |
global ignored_dirs | |
directory = '/Users/halicea/projects/Mediawire' | |
for k in sys.argv[1:]: | |
if not k.startswith('-'): | |
directory = k | |
break | |
ignored_exts = get_arg_list('ie') | |
ignored_dirs = get_arg_list('i') | |
extensions = get_arg_list('ext') | |
print extensions | |
if extensions: | |
exts = [x for x in supported_exts if x[0] in extensions] | |
elif ignored_exts: | |
exts = [x for x in supported_exts if x[0] not in ignored_exts] | |
return { | |
'directory': directory, | |
'ignored_exts': ignored_exts, | |
'ignored_dirs': ignored_dirs, | |
'extensions': extensions | |
} | |
def get_arg_list(ext): | |
xext = '-'+ext | |
result = [] | |
if xext in sys.argv: | |
index = sys.argv.index(xext) + 1 | |
arg = sys.argv[index] | |
while arg and (not arg.startswith('-')): | |
result.append(arg) | |
index += 1 | |
if index >= len(sys.argv): | |
break | |
arg = sys.argv[index] | |
return result | |
def collect_files(directory): | |
source_files = [] | |
for root, dirs, files in os.walk(directory): | |
if not is_ignored(root): | |
for source_file in files: | |
f = os.path.join(root, source_file) | |
rel_path = f[len(directory)+1:] | |
if not is_ignored(f) and is_efull(f): | |
src_data = [rel_path, get_ext(f), 0] | |
try: | |
o = codecs.open(f, 'r', 'utf-8').readlines() | |
src_data[2] = len(o) | |
except: | |
print 'Cant open file:', rel_path | |
source_files.append(src_data) | |
return source_files | |
def group_by_type(files): | |
all_types = [] | |
for ext in exts: | |
twithext = [x for x in files if x[1] == ext[0]] | |
if len(twithext) > 0: | |
type_stat = [ext[0], twithext] | |
all_types.append(type_stat) | |
return all_types | |
def print_file_list(l, title): | |
print '\n'+'#'*30 | |
print title | |
print '#'*30 | |
current_base = None | |
total_lines = 0 | |
for tup in l: | |
f = tup[0] | |
lines = tup[2] | |
total_lines += lines | |
dir_name = os.path.dirname(f) | |
dept = len(dir_name.split(os.path.sep)) | |
if dir_name != current_base: | |
current_base = dir_name | |
if not dir_name: | |
print 'Root' | |
else: | |
print dir_name | |
print ' '*(dept+1)+f[len(dir_name)+(dir_name and 1 or 0):] | |
def print_comparison(counts, names, title): | |
total = sum(counts) | |
percents = [(float(x) / total) * 100 for x in counts] | |
print '\n'+'#'*30 | |
print title | |
print '#'*30 | |
for i in range(0, len(counts)): | |
print names[i]+'#:', counts[i] | |
print 'Total:', total | |
for i in range(0, len(counts)): | |
print names[i]+'#:', str(percents[i])[:5]+'%' | |
def is_efull(f): | |
found = False | |
for ext in exts: | |
if f.endswith(ext[0]): | |
found = True | |
if len(ext) > 2: | |
for pext in ext[2]: | |
if has_ext(pext, f): | |
found = False | |
break | |
break | |
return found | |
def get_ext_name(ext): | |
found_ext = [x[1] for x in exts if x[0] == ext] | |
if found_ext: | |
return found_ext[0] | |
def has_ext(ext, f): | |
ind = len(f) | |
if '.' in f: | |
ind = f.rindex('.') | |
name = f[:ind] | |
return os.path.exists(name+ext) | |
def get_ext(f): | |
if '.' in f: | |
ind = f.rindex('.') | |
return f[ind:] | |
else: | |
return '' | |
def is_ignored(p): | |
for m in ignored_dirs: | |
if m in p.split(os.path.sep): | |
return True | |
return False | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment