-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeekCodeLine.py
More file actions
88 lines (81 loc) · 2.85 KB
/
Copy pathSeekCodeLine.py
File metadata and controls
88 lines (81 loc) · 2.85 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
import os
def count_code_lines(file_path, file_ext):
"""Counts the number of non-empty, non-comment lines in a given file."""
count = 0
try:
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
except UnicodeDecodeError:
try:
with open(file_path, 'r', encoding='gbk') as file:
lines = file.readlines()
except UnicodeDecodeError:
print(f"Could not decode file {file_path}. Skipping...")
return 0
if file_ext in ['.cpp', '.h']:
in_block_comment = False
for line in lines:
line = line.strip()
if not line:
continue
if '/*' in line:
in_block_comment = True
if '*/' in line:
in_block_comment = False
continue
if in_block_comment:
continue
if not line.startswith('//'):
count += 1
elif file_ext == '.cs':
in_block_comment = False
for line in lines:
line = line.strip()
if not line:
continue
if '/*' in line:
in_block_comment = True
if '*/' in line:
in_block_comment = False
continue
if in_block_comment:
continue
if not line.startswith('//'):
count += 1
elif file_ext == '.py':
for line in lines:
line = line.strip()
if not line:
continue
if line.startswith('#'):
continue
count += 1
elif file_ext == '.xaml':
for line in lines:
line = line.strip()
if not line:
continue
if line.startswith('<!'):
continue
count += 1
return count
def scan_directory(directory):
"""Scans the directory and counts the effective lines of code, skipping certain folders."""
code_exts = ['.cpp', '.h', '.cs', '.py', '.xaml']
total_lines = {ext: 0 for ext in code_exts}
for root, dirs, files in os.walk(directory):
# Modify the dirs in-place to skip certain directories
dirs[:] = [d for d in dirs if not (d.startswith('.') or d in ['bin', 'obj'])]
for file in files:
file_ext = os.path.splitext(file)[1]
if file_ext in code_exts:
file_path = os.path.join(root, file)
num_lines = count_code_lines(file_path, file_ext)
total_lines[file_ext] += num_lines
return total_lines
if __name__ == "__main__":
directory = input("Enter the directory path to scan: ")
lines_count = scan_directory(directory)
print("Effective lines of code:")
for ext, count in lines_count.items():
print(f"{ext} files: {count} lines")