Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. import sys
  2. from os import walk
  3. from os.path import basename, isdir, splitext
  4. from operator import itemgetter
  5.  
  6. if len(sys.argv) < 2:
  7.     print('Usage: %s <directory>'%basename(sys.argv[0]))
  8.     sys.exit(1)
  9.  
  10. dirname = sys.argv[1]
  11. if not isdir(dirname):
  12.     print('\'%s\'\n is not a valid directory'%dirname)
  13.  
  14. debugging = False
  15. if len(sys.argv) >= 3:
  16.     if sys.argv[2][1:] == 'd':
  17.         debugging = True
  18.        
  19. valid_exts = ('cpp','c','h','hpp','sql','py','java','asm',
  20.               'bat','cmd','bas','vbs','vbe','inc','rb','php',
  21.               'asp','htm','html','ini','cfg','hs','bf','s',
  22.               'js','idl','pro')
  23. data = {}
  24. for ext in valid_exts:
  25.     data[ext] = [0,0]
  26. other_data = {}
  27. total_nof_files = 0
  28. total_nof_lines = 0
  29. try:
  30.     for root, dirs, files in walk(dirname):
  31.         for filename in files:
  32.             ext = splitext(filename)[1][1:]
  33.             if not ext in valid_exts:
  34.                 other_data[ext] = other_data.get(ext,0)+1
  35.                 continue
  36.             with open("%s\\%s"%(root,filename)) as file:
  37.                 try:
  38.                     nof_lines = sum([1 for line in file])
  39.                 except UnicodeDecodeError:
  40.                     if debugging:
  41.                         print('%\'s\' is not a valid text only file.'%filename)
  42.                     continue
  43.                 data[ext][0] = data[ext][0]+1
  44.                 data[ext][1] = data[ext][1]+nof_lines
  45.                 total_nof_files = total_nof_files+1
  46.                 total_nof_lines = total_nof_lines+nof_lines
  47.                 if debugging:
  48.                     print('\'%s\' have %d lines.'%(filename,nof_lines))
  49. except KeyboardInterrupt:
  50.     if debugging:
  51.         print('#interrupted.')
  52. finally:
  53.     for ext, data in sorted(data.items(),key=itemgetter(1),reverse=True):
  54.         [nof_files, nof_lines] = data
  55.         if nof_lines:
  56.             print('[%s]\n\t%d files - %.2f%%\n\t%.2f%% of code'
  57.                   '\n\t%.2f lines of code per file'
  58.                   %(ext.upper(),nof_files,nof_files/total_nof_files*100,
  59.                     nof_lines/total_nof_lines*100,nof_lines/nof_files))
  60.    
  61.     print('\nfiles: %d'%total_nof_files)
  62.     print('lines of code: %d'%total_nof_lines)
  63.     print('lines of code per file: %.2f'%(total_nof_lines/total_nof_files))
  64.    
  65.     print('\n[OTHER SIGNIFICANT EXTENSIONS]\n')
  66.     for ext in sorted(other_data.items(),key=itemgetter(1),reverse=True)[:5]:
  67.         print(ext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement