-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_line_counter.py
More file actions
352 lines (298 loc) · 9.79 KB
/
code_line_counter.py
File metadata and controls
352 lines (298 loc) · 9.79 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import argparse
from pathlib import Path
from typing import Dict, Set, List
import fnmatch
def is_code_line(line: str) -> bool:
"""
Check if a line contains actual code (not just whitespace or comments).
This is a simple heuristic - you might want to make it more sophisticated.
"""
stripped = line.strip()
if not stripped:
return False
# Skip single-line comments
if (
stripped.startswith("#")
or stripped.startswith("//")
or stripped.startswith("*")
):
return False
return True
def should_exclude_path(path: Path, excluded_patterns: List[str]) -> bool:
"""
Check if a path should be excluded based on patterns.
Args:
path: Path to check
excluded_patterns: List of patterns to exclude
Returns:
True if path should be excluded
"""
path_str = str(path)
path_parts = path.parts
for pattern in excluded_patterns:
# Check if any part of the path matches the pattern
for part in path_parts:
if fnmatch.fnmatch(part, pattern):
return True
# Also check the full path
if fnmatch.fnmatch(path_str, pattern):
return True
return False
def count_lines_in_file(file_path: Path) -> Dict[str, int]:
"""
Count different types of lines in a file.
Returns dict with total, code, blank, and comment lines.
"""
try:
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
lines = f.readlines()
except (IOError, OSError) as e:
print(f"Warning: Could not read {file_path}: {e}")
return {"total": 0, "code": 0, "blank": 0, "comments": 0}
total_lines = len(lines)
blank_lines = 0
comment_lines = 0
code_lines = 0
in_multiline_comment = False
for line in lines:
stripped = line.strip()
# Count blank lines
if not stripped:
blank_lines += 1
continue
# Handle multiline comments (/* ... */ for JS/TS, """ ... """ for Python)
if file_path.suffix == ".py":
if '"""' in stripped or "'''" in stripped:
in_multiline_comment = not in_multiline_comment
comment_lines += 1
continue
elif in_multiline_comment:
comment_lines += 1
continue
elif file_path.suffix in [".js", ".ts", ".tsx"]:
if "/*" in stripped:
in_multiline_comment = True
if in_multiline_comment:
comment_lines += 1
if "*/" in stripped:
in_multiline_comment = False
continue
# Check for single-line comments
if (
stripped.startswith("#")
or stripped.startswith("//")
or stripped.startswith("*")
):
comment_lines += 1
else:
code_lines += 1
return {
"total": total_lines,
"code": code_lines,
"blank": blank_lines,
"comments": comment_lines,
}
def count_lines_of_code(
directory: str, extensions: Set[str] = None, excluded_patterns: List[str] = None
) -> Dict[str, Dict[str, int]]:
"""
Recursively count lines of code in files with specified extensions.
Args:
directory: Starting directory path
extensions: Set of file extensions to include (default: .py, .js, .ts, .tsx)
excluded_patterns: List of directory/file patterns to exclude
Returns:
Dictionary with file counts and totals
"""
if extensions is None:
extensions = {".py", ".js", ".ts", ".tsx"}
if excluded_patterns is None:
excluded_patterns = [
"node_modules",
".git",
".svn",
".hg",
"__pycache__",
".pytest_cache",
".venv",
"venv",
"env",
".env",
"build",
"dist",
".next",
"coverage",
".nyc_output",
"vendor",
"target",
"out",
".idea",
".vscode",
"*.min.js",
"*.min.css",
]
directory_path = Path(directory)
if not directory_path.exists():
raise ValueError(f"Directory {directory} does not exist")
results = {
"files": {},
"totals": {"total": 0, "code": 0, "blank": 0, "comments": 0},
"by_extension": {},
"excluded_count": 0,
}
# Initialize extension counters
for ext in extensions:
results["by_extension"][ext] = {
"total": 0,
"code": 0,
"blank": 0,
"comments": 0,
"file_count": 0,
}
# Walk through directory recursively
for file_path in directory_path.rglob("*"):
if file_path.is_file() and file_path.suffix in extensions:
# Check if file should be excluded
if should_exclude_path(
file_path.relative_to(directory_path), excluded_patterns
):
results["excluded_count"] += 1
continue
file_stats = count_lines_in_file(file_path)
# Store individual file results
relative_path = file_path.relative_to(directory_path)
results["files"][str(relative_path)] = file_stats
# Update totals
for key in ["total", "code", "blank", "comments"]:
results["totals"][key] += file_stats[key]
results["by_extension"][file_path.suffix][key] += file_stats[key]
results["by_extension"][file_path.suffix]["file_count"] += 1
return results
def print_results(results: Dict[str, Dict[str, int]], show_files: bool = False):
"""Print formatted results."""
print("=" * 60)
print("LINE COUNT SUMMARY")
print("=" * 60)
# Print totals
totals = results["totals"]
print(f"Total Lines: {totals['total']:,}")
print(f"Code Lines: {totals['code']:,}")
print(f"Blank Lines: {totals['blank']:,}")
print(f"Comment Lines: {totals['comments']:,}")
if results.get("excluded_count", 0) > 0:
print(f"Excluded Files: {results['excluded_count']:,}")
print()
# Print by extension
print("BY FILE TYPE:")
print("-" * 40)
for ext, stats in results["by_extension"].items():
if stats["file_count"] > 0:
print(f"{ext} files ({stats['file_count']} files):")
print(f" Total: {stats['total']:,}")
print(f" Code: {stats['code']:,}")
print(f" Blank: {stats['blank']:,}")
print(f" Comments: {stats['comments']:,}")
print()
# Print individual files if requested
if show_files:
print("BY FILE:")
print("-" * 40)
for file_path, stats in results["files"].items():
print(f"{file_path}:")
print(
f" Total: {stats['total']:,}, Code: {stats['code']:,}, "
f"Blank: {stats['blank']:,}, Comments: {stats['comments']:,}"
)
def main():
parser = argparse.ArgumentParser(
description="Count lines of code in Python, JavaScript, and TypeScript files"
)
parser.add_argument("directory", help="Directory to scan recursively")
parser.add_argument(
"--extensions",
nargs="+",
default=[".py", ".js", ".ts", ".tsx"],
help="File extensions to include (default: .py .js .ts .tsx)",
)
parser.add_argument(
"--exclude",
nargs="+",
help="Additional patterns to exclude (beyond default exclusions)",
)
parser.add_argument(
"--no-default-excludes",
action="store_true",
help="Don't use default exclusion patterns",
)
parser.add_argument(
"--show-files", action="store_true", help="Show individual file statistics"
)
parser.add_argument(
"--list-excludes",
action="store_true",
help="List default exclusion patterns and exit",
)
args = parser.parse_args()
# Default exclusion patterns
default_excludes = [
"node_modules",
".git",
".svn",
".hg",
"__pycache__",
".pytest_cache",
".venv",
"venv",
"env",
".env",
"build",
"dist",
".next",
"coverage",
".nyc_output",
"vendor",
"target",
"out",
".idea",
".vscode",
"*.min.js",
"*.min.css",
]
if args.list_excludes:
print("Default exclusion patterns:")
for pattern in default_excludes:
print(f" {pattern}")
return 0
try:
# Convert extensions list to set, ensure they start with dot
extensions = set()
for ext in args.extensions:
if not ext.startswith("."):
ext = "." + ext
extensions.add(ext)
# Handle exclusion patterns
excluded_patterns = []
if not args.no_default_excludes:
excluded_patterns.extend(default_excludes)
if args.exclude:
excluded_patterns.extend(args.exclude)
print(f"Scanning directory: {args.directory}")
print(f"File extensions: {', '.join(sorted(extensions))}")
if excluded_patterns:
print(
f"Excluding patterns: {', '.join(excluded_patterns[:5])}"
+ (
f" (and {len(excluded_patterns)-5} more)"
if len(excluded_patterns) > 5
else ""
)
)
print()
results = count_lines_of_code(args.directory, extensions, excluded_patterns)
print_results(results, args.show_files)
except Exception as e:
print(f"Error: {e}")
return 1
return 0
if __name__ == "__main__":
exit(main())