Last active
November 20, 2021 19:40
-
-
Save davincif/eb294a1e196225cfda263f2a36acb6af to your computer and use it in GitHub Desktop.
A project line counter in python
This file contains hidden or 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/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 | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment