-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfind_imports.py
55 lines (45 loc) · 1.64 KB
/
find_imports.py
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
import os
import ast
import importlib.util
def get_python_files(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.py'):
yield os.path.join(root, file)
def get_imports(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
try:
tree = ast.parse(file.read())
except SyntaxError:
print(f"Syntax error in {file_path}")
return []
imports = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
imports.add(alias.name)
elif isinstance(node, ast.ImportFrom):
module = node.module if node.module else ''
for alias in node.names:
imports.add(f"{module}.{alias.name}")
return imports
def is_local_module(module_name, base_path):
parts = module_name.split('.')
current_path = base_path
for part in parts:
current_path = os.path.join(current_path, part)
if os.path.isfile(current_path + '.py') or os.path.isdir(current_path):
continue
return False
return True
def main():
base_path = os.path.dirname(os.path.abspath(__file__)) # Assumes this script is in the project root
all_imports = set()
for file_path in get_python_files(base_path):
all_imports.update(get_imports(file_path))
local_imports = {imp for imp in all_imports if is_local_module(imp, base_path)}
print("Potential hidden imports:")
for imp in sorted(local_imports):
print(f"--hidden-import={imp}")
if __name__ == "__main__":
main()