Spaces:
Runtime error
Runtime error
Create fix_imports_and_setup.py
Browse files- fix_imports_and_setup.py +69 -0
fix_imports_and_setup.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
|
| 5 |
+
COMPONENT_DIR = "components"
|
| 6 |
+
PY_FILE_REGEX = re.compile(r"from\s+([a-zA-Z0-9_\.]+)\s+import\s+([a-zA-Z0-9_]+)")
|
| 7 |
+
|
| 8 |
+
def find_py_files(base_path):
|
| 9 |
+
py_files = []
|
| 10 |
+
for root, dirs, files in os.walk(base_path):
|
| 11 |
+
for file in files:
|
| 12 |
+
if file.endswith(".py"):
|
| 13 |
+
py_files.append(os.path.join(root, file))
|
| 14 |
+
return py_files
|
| 15 |
+
|
| 16 |
+
def ensure_init_files():
|
| 17 |
+
"""Add __init__.py to every directory under components/"""
|
| 18 |
+
for root, dirs, files in os.walk(os.path.join(PROJECT_ROOT, COMPONENT_DIR)):
|
| 19 |
+
if '__init__.py' not in files:
|
| 20 |
+
init_path = os.path.join(root, '__init__.py')
|
| 21 |
+
open(init_path, 'w').close()
|
| 22 |
+
print(f"[INIT ADDED] {init_path}")
|
| 23 |
+
|
| 24 |
+
def resolve_import_module(symbol):
|
| 25 |
+
"""Try to locate the module file matching the symbol name"""
|
| 26 |
+
for root, dirs, files in os.walk(os.path.join(PROJECT_ROOT, COMPONENT_DIR)):
|
| 27 |
+
for file in files:
|
| 28 |
+
if file == f"{symbol}.py":
|
| 29 |
+
relative = os.path.relpath(root, PROJECT_ROOT)
|
| 30 |
+
return f"{relative.replace(os.sep, '.')}.{symbol}"
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
def fix_imports_in_file(file_path):
|
| 34 |
+
with open(file_path, 'r') as f:
|
| 35 |
+
lines = f.readlines()
|
| 36 |
+
|
| 37 |
+
changed = False
|
| 38 |
+
new_lines = []
|
| 39 |
+
for line in lines:
|
| 40 |
+
match = PY_FILE_REGEX.match(line.strip())
|
| 41 |
+
if match:
|
| 42 |
+
module_path, symbol = match.groups()
|
| 43 |
+
if module_path == symbol.lower():
|
| 44 |
+
resolved = resolve_import_module(symbol)
|
| 45 |
+
if resolved:
|
| 46 |
+
fixed_line = f"from {resolved} import {symbol}"
|
| 47 |
+
print(f"[FIX] {file_path} :: {line.strip()} -> {fixed_line}")
|
| 48 |
+
new_lines.append(fixed_line + '\n')
|
| 49 |
+
changed = True
|
| 50 |
+
continue
|
| 51 |
+
new_lines.append(line)
|
| 52 |
+
|
| 53 |
+
if changed:
|
| 54 |
+
with open(file_path, 'w') as f:
|
| 55 |
+
f.writelines(new_lines)
|
| 56 |
+
print(f"[UPDATED] {file_path}")
|
| 57 |
+
|
| 58 |
+
def run_full_fix():
|
| 59 |
+
print("== Starting Full Import Fix and Init Generator ==\n")
|
| 60 |
+
ensure_init_files()
|
| 61 |
+
py_files = find_py_files(PROJECT_ROOT)
|
| 62 |
+
for file in py_files:
|
| 63 |
+
if file.endswith("__init__.py") or COMPONENT_DIR not in file:
|
| 64 |
+
continue
|
| 65 |
+
fix_imports_in_file(file)
|
| 66 |
+
print("\n== Done ==")
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
run_full_fix()
|