Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from os import walk
- from os.path import basename, isdir, splitext
- from operator import itemgetter
- if len(sys.argv) < 2:
- print('Usage: %s <directory>'%basename(sys.argv[0]))
- sys.exit(1)
- dirname = sys.argv[1]
- if not isdir(dirname):
- print('\'%s\'\n is not a valid directory'%dirname)
- debugging = False
- if len(sys.argv) >= 3:
- if sys.argv[2][1:] == 'd':
- debugging = True
- valid_exts = ('cpp','c','h','hpp','sql','py','java','asm',
- 'bat','cmd','bas','vbs','vbe','inc','rb','php',
- 'asp','htm','html','ini','cfg','hs','bf','s',
- 'js','idl','pro')
- data = {}
- for ext in valid_exts:
- data[ext] = [0,0]
- other_data = {}
- total_nof_files = 0
- total_nof_lines = 0
- try:
- for root, dirs, files in walk(dirname):
- for filename in files:
- ext = splitext(filename)[1][1:]
- if not ext in valid_exts:
- other_data[ext] = other_data.get(ext,0)+1
- continue
- with open("%s\\%s"%(root,filename)) as file:
- try:
- nof_lines = sum([1 for line in file])
- except UnicodeDecodeError:
- if debugging:
- print('%\'s\' is not a valid text only file.'%filename)
- continue
- data[ext][0] = data[ext][0]+1
- data[ext][1] = data[ext][1]+nof_lines
- total_nof_files = total_nof_files+1
- total_nof_lines = total_nof_lines+nof_lines
- if debugging:
- print('\'%s\' have %d lines.'%(filename,nof_lines))
- except KeyboardInterrupt:
- if debugging:
- print('#interrupted.')
- finally:
- for ext, data in sorted(data.items(),key=itemgetter(1),reverse=True):
- [nof_files, nof_lines] = data
- if nof_lines:
- print('[%s]\n\t%d files - %.2f%%\n\t%.2f%% of code'
- '\n\t%.2f lines of code per file'
- %(ext.upper(),nof_files,nof_files/total_nof_files*100,
- nof_lines/total_nof_lines*100,nof_lines/nof_files))
- print('\nfiles: %d'%total_nof_files)
- print('lines of code: %d'%total_nof_lines)
- print('lines of code per file: %.2f'%(total_nof_lines/total_nof_files))
- print('\n[OTHER SIGNIFICANT EXTENSIONS]\n')
- for ext in sorted(other_data.items(),key=itemgetter(1),reverse=True)[:5]:
- print(ext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement