-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_lines.py
More file actions
104 lines (80 loc) · 2.17 KB
/
Copy pathcount_lines.py
File metadata and controls
104 lines (80 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from pathlib import Path
import argparse
# File/folder names to skip
SKIP_DIRS = {
".git",
".idea",
".vscode",
".venv",
"venv",
"env",
"cmake-build-debug",
"cmake-build-release",
"build",
"__pycache__",
}
# Extensions to count.
# Add/remove anything here.
COUNT_EXTENSIONS = {
".c",
".h",
".nr",
".txt",
".md",
".cmake",
}
# Exact filenames to count even if they have no extension
COUNT_FILENAMES = {
"CMakeLists.txt",
"Makefile",
}
def should_skip(path: Path) -> bool:
return any(part in SKIP_DIRS for part in path.parts)
def should_count(path: Path) -> bool:
return (
path.suffix in COUNT_EXTENSIONS
or path.name in COUNT_FILENAMES
)
def count_lines(path: Path) -> int:
try:
with path.open("r", encoding="utf-8", errors="replace") as file:
return sum(1 for _ in file)
except Exception as error:
print(f"Could not read {path}: {error}")
return 0
def main():
parser = argparse.ArgumentParser(
description="Count lines of code/files in a Nearoh project directory."
)
parser.add_argument(
"directory",
nargs="?",
default=".",
help="Directory to scan. Defaults to current directory."
)
args = parser.parse_args()
root = Path(args.directory).resolve()
if not root.exists():
print(f"Directory does not exist: {root}")
return
if not root.is_dir():
print(f"Path is not a directory: {root}")
return
files = []
for path in root.rglob("*"):
if path.is_file() and not should_skip(path) and should_count(path):
line_count = count_lines(path)
files.append((path, line_count))
files.sort(key=lambda item: str(item[0]).lower())
total = 0
print(f"\nLine count for: {root}\n")
print("-" * 80)
for path, line_count in files:
relative_path = path.relative_to(root)
total += line_count
print(f"{str(relative_path):60} {line_count:>8}")
print("-" * 80)
print(f"{'TOTAL':60} {total:>8}")
print(f"{'FILES COUNTED':60} {len(files):>8}")
if __name__ == "__main__":
main()