Created
April 1, 2021 07:35
-
-
Save rondreas/b357101130f62af6173593272db1e90c to your computer and use it in GitHub Desktop.
Tool to get a quick overview of number of files and lines of text
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 re | |
import argparse | |
def main(): | |
parser = argparse.ArgumentParser( | |
description="Tool to get a quick overview of number of files and lines of text" | |
) | |
parser.add_argument( | |
"-p", | |
"--path", | |
help="Path to root directory", | |
type=str, | |
required=True | |
) | |
parser.add_argument( | |
"--pattern", | |
help="Pattern to exclude example *.pyc will ignore all .pyc files\ | |
match is made only on filename so doesn't handle directory filter.", | |
type=str, | |
) | |
pargs = parser.parse_args() | |
# say we want to exclude all .pyc and .md files, we should run --pattern *.pyc;*.md | |
if pargs.pattern: | |
pattern = pargs.pattern.replace("*", ".+") # * wildcard is .+ in regex, | |
patterns = pattern.split(';') | |
pattern = '|'.join(["^{}$".format(x) for x in patterns]) | |
exclude = re.compile("({})".format(pattern)) | |
files_total = 0 | |
lines_total = 0 | |
print("="*30) | |
for root, dirs, files in os.walk(pargs.path): | |
for file in files: | |
if pargs.pattern: | |
if re.match(exclude, file): | |
continue | |
path = os.path.join(root, file) | |
with open(path, 'r') as f: | |
line_count = sum(1 for line in f) | |
print("{:<25}{}".format(file, line_count)) | |
files_total += 1 | |
lines_total += line_count | |
print("="*30) | |
print("Total Files: {}".format(files_total)) | |
print("Total Lines: {}".format(lines_total)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment