-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-duplicates.py
More file actions
228 lines (179 loc) · 7.39 KB
/
find-duplicates.py
File metadata and controls
228 lines (179 loc) · 7.39 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
import ast
import os
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path
@dataclass
class FileInfo:
absolute_path: str
relative_path: str
name: str
is_file: bool
def is_path_excluded(absolute_path, excluded_folders):
"""Check if path contains any excluded folder as a complete directory name."""
path_parts = Path(absolute_path).parts
return any(folder in path_parts for folder in excluded_folders)
def find_classes_in_file(file_path):
"""Extract all class names from a Python file using AST."""
classes = []
try:
with open(file_path, "r", encoding="utf-8") as f:
tree = ast.parse(f.read(), filename=file_path)
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef):
classes.append(node.name)
except (SyntaxError, UnicodeDecodeError) as e:
print(f"Error parsing {file_path}: {e}")
return classes
def find_functions_in_file(file_path):
"""Extract all function names from a Python file using AST, excluding dunder methods."""
functions = []
try:
with open(file_path, "r", encoding="utf-8") as f:
tree = ast.parse(f.read(), filename=file_path)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
# Skip dunder methods (e.g., __init__, __str__, __repr__)
if not (node.name.startswith("__") and node.name.endswith("__")):
functions.append(node.name)
except (SyntaxError, UnicodeDecodeError) as e:
print(f"Error parsing {file_path}: {e}")
return functions
def count_class_occurrences(all_items):
"""Count occurrences of each class name across all Python files."""
class_occurrences = defaultdict(list) # class_name -> [file_path, ...]
for item in all_items:
if item.is_file and item.name.endswith(".py"):
classes = find_classes_in_file(item.absolute_path)
for class_name in classes:
class_occurrences[class_name].append(item)
return class_occurrences
def count_function_occurrences(all_items):
"""Count occurrences of each function name across all Python files."""
function_occurrences = defaultdict(list) # function_name -> [file_path, ...]
for item in all_items:
if item.is_file and item.name.endswith(".py"):
functions = find_functions_in_file(item.absolute_path)
for function_name in functions:
function_occurrences[function_name].append(item)
return function_occurrences
def search_files_and_folders(basedir) -> list:
all_items = [] # store FileInfo objects
excluded_folders = {
".cache",
".git",
".github",
".meta",
".mypy_cache",
".pytest_cache",
".ruff_cache",
".tox",
".venv",
".vscode",
"logs",
"fixtures",
"site",
"stubs",
}
excluded_file_extensions = {"pyc"}
for dirpath, dirnames, filenames in os.walk(basedir):
# handle files
for f in filenames: # filename is f
# get absolute path to file
relative_path = os.path.join(dirpath, f)
absolute_path = os.path.abspath(relative_path)
file_extension = Path(absolute_path).suffix[1:]
if is_path_excluded(absolute_path, excluded_folders):
continue
if file_extension in excluded_file_extensions:
continue
all_items.append(
FileInfo(
absolute_path=absolute_path, relative_path=relative_path, name=f, is_file=True
)
)
# handle folders
for d in dirnames: # foldername is d
if d == "__pycache__":
continue
relative_path = os.path.join(dirpath, d)
absolute_path = os.path.abspath(relative_path)
if is_path_excluded(absolute_path, excluded_folders):
continue
all_items.append(
FileInfo(
absolute_path=absolute_path, relative_path=relative_path, name=d, is_file=False
)
)
return all_items
def main():
cwd = "."
print("Scanning subfolders..")
all_items = search_files_and_folders(cwd)
assert len(all_items) > 0, f"Did not find any files or folders"
# Use defaultdict to store lists of FileInfo objects
folder_occurrences = defaultdict(list)
file_occurrences = defaultdict(list)
for fi in all_items:
if fi.is_file:
file_occurrences[fi.name].append(fi)
else: # isFolder
folder_occurrences[fi.name].append(fi)
print("\n--- File Occurrences (Sorted by count, descending) ---")
# Sort file occurrences by the number of items in the list (their count)
# The key for sorting is the length of the list associated with each name
sorted_file_occurrences = sorted(
file_occurrences.items(),
key=lambda item: len(item[1]), # item[1] is the list of FileInfo objects
reverse=True,
)
for name, files in sorted_file_occurrences:
count = len(files)
if count == 1:
continue
print(f"Name: '{name}' - Occurrences: {count}")
print("\n--- Folder Occurrences (Sorted by count, descending) ---")
# Sort folder occurrences similarly
sorted_folder_occurrences = sorted(
folder_occurrences.items(), key=lambda item: len(item[1]), reverse=True
)
for name, folders in sorted_folder_occurrences:
count = len(folders)
if count == 1:
continue
print(f"Name: '{name}' - Occurrences: {count}")
# Count class occurrences
print("\n--- Class Occurrences (Sorted by count, descending) ---")
class_occurrences = count_class_occurrences(all_items)
# Sort class occurrences by count
sorted_class_occurrences = sorted(
class_occurrences.items(), key=lambda x: len(x[1]), reverse=True
)
for class_name, file_infos in sorted_class_occurrences:
count = len(file_infos)
if count > 1: # Only show classes that appear more than once
print(f"Class '{class_name}' - Occurrences: {count}")
# Optionally show first few file paths
for i, file_info in enumerate(file_infos[:3]): # Show first 3 paths
print(f" - {file_info.relative_path}")
if count > 3:
print(f" ... and {count - 3} more")
# Count function occurrences
print("\n--- Function Occurrences (Sorted by count, descending) ---")
function_occurrences = count_function_occurrences(all_items)
# Sort function occurrences by count
sorted_function_occurrences = sorted(
function_occurrences.items(), key=lambda x: len(x[1]), reverse=True
)
min_occurrences_to_show = 2 # put this value higher to be less strict
for function_name, file_infos in sorted_function_occurrences:
count = len(file_infos)
if count >= min_occurrences_to_show: # Configurable threshold
print(f"Function '{function_name}()' - Occurrences: {count}")
# Show first few file paths
for i, file_info in enumerate(file_infos[:3]): # Show first 3 paths
print(f" - {file_info.relative_path}")
if count > 3:
print(f" ... and {count - 3} more")
if __name__ == "__main__":
main()