Spaces:
Runtime error
Runtime error
Create patch_utils_imports.py
Browse files- patch_utils_imports.py +71 -0
patch_utils_imports.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
PATCH_FILE = os.path.abspath(__file__)
|
| 5 |
+
ROOT = os.path.dirname(PATCH_FILE)
|
| 6 |
+
|
| 7 |
+
APP_FILE = os.path.join(ROOT, "app.py")
|
| 8 |
+
TARGET_PREFIX = "omniscientframework.utils"
|
| 9 |
+
|
| 10 |
+
def patch_file(filepath):
|
| 11 |
+
if not os.path.exists(filepath):
|
| 12 |
+
print(f"[patch] File not found: {filepath}")
|
| 13 |
+
return False
|
| 14 |
+
|
| 15 |
+
with open(filepath, "r") as f:
|
| 16 |
+
original = f.read()
|
| 17 |
+
|
| 18 |
+
patched = original.replace(
|
| 19 |
+
"from utils.",
|
| 20 |
+
f"from {TARGET_PREFIX}."
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
if patched == original:
|
| 24 |
+
print("[patch] No changes needed in app.py.")
|
| 25 |
+
else:
|
| 26 |
+
with open(filepath, "w") as f:
|
| 27 |
+
f.write(patched)
|
| 28 |
+
print("[patch] Patched import paths in app.py.")
|
| 29 |
+
|
| 30 |
+
return True
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def ensure_init_files():
|
| 34 |
+
package_dirs = [
|
| 35 |
+
os.path.join(ROOT, "omniscientframework"),
|
| 36 |
+
os.path.join(ROOT, "omniscientframework", "utils")
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
for d in package_dirs:
|
| 40 |
+
init_file = os.path.join(d, "__init__.py")
|
| 41 |
+
if not os.path.exists(d):
|
| 42 |
+
print(f"[patch] Directory missing: {d}")
|
| 43 |
+
continue
|
| 44 |
+
if not os.path.exists(init_file):
|
| 45 |
+
with open(init_file, "w") as f:
|
| 46 |
+
f.write("") # empty init file
|
| 47 |
+
print(f"[patch] Created {init_file}")
|
| 48 |
+
else:
|
| 49 |
+
print(f"[patch] Init exists: {init_file}")
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def self_delete():
|
| 53 |
+
print("[patch] Cleaning up patch file...")
|
| 54 |
+
try:
|
| 55 |
+
os.remove(PATCH_FILE)
|
| 56 |
+
print("[patch] Patch file removed from container runtime.")
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"[patch] Failed to delete self: {e}")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def main():
|
| 62 |
+
print("[patch] Running import patcher...")
|
| 63 |
+
ok = patch_file(APP_FILE)
|
| 64 |
+
ensure_init_files()
|
| 65 |
+
if ok:
|
| 66 |
+
self_delete()
|
| 67 |
+
print("[patch] Done.")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
main()
|