#!/usr/bin/python3 import os import json path = '../' fileExtentions = [ 'ts', 'js', 'json', 'html', 'css', 'scss', 'sass', 'svg', 'sh', ] fileExtentionsNames = { 'ts': 'TypeScript', 'js': 'JavaScript', 'json': 'JavaScript Object', 'html': 'HTML', 'css': 'CSS', 'scss': 'SCSS', 'sass': 'SASS', 'svg': 'SVG', 'sh': 'ShellScript', } excludeFile = ['package-lock.json'] excludeDir = [ 'node_modules', 'dist', '.vscode', '.git', 'trash', ] files = [] total_lines = 0 # initialize states stats = {file: 0 for file in fileExtentions} def main(): global fileExtentions global excludeFile global excludeDir global files global total_lines global stats excludeDir = list( map(lambda dir: os.path.join(path, dir), excludeDir) ) # r=root, d=directories, f = files for r, d, f in os.walk(path): if(True in map(lambda dir: r.startswith(dir), excludeDir)): continue for file in f: fname = file.split('/')[-1] fext = fname.split('.')[-1] if fext in fileExtentions and fname not in excludeFile: file_lines = sum( 1 for line in open( os.path.join(r, file) ) ) stats[fext] += file_lines total_lines += file_lines if __name__ == '__main__': main() # create statistics stats = { fext: { 'language': fileExtentionsNames[fext], 'lines': stats[fext], "%": '%.2f%%' % (stats[fext] / total_lines * 100), } for fext in fileExtentions } # delete empty lines toDelete = [] for lang in stats: if(not stats[lang]['lines']): toDelete.append(lang) for deleteIt in toDelete: del stats[deleteIt] # show results print("Linhas:", total_lines) print( "Statistics:", json.dumps( stats, indent=2, sort_keys=True ) )