|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import shutil |
| 4 | + |
| 5 | +# --- Step 1: Ask user for new builder name --- |
| 6 | +while True: |
| 7 | + new_name = input("Enter new builder HTML name (e.g., xybuilder.html): ").strip() |
| 8 | + if new_name: |
| 9 | + break |
| 10 | + print("ERROR: Name cannot be empty!") |
| 11 | + |
| 12 | +if not new_name.endswith(".html"): |
| 13 | + new_name += ".html" |
| 14 | + |
| 15 | +# --- Step 2: Ask for virtualenv path --- |
| 16 | +while True: |
| 17 | + venv_path = input("Enter path to your Python virtualenv: ").strip() |
| 18 | + if os.path.exists(venv_path): |
| 19 | + break |
| 20 | + print("ERROR: Path does not exist!") |
| 21 | + |
| 22 | +# --- Step 3: Determine site-packages dynamically --- |
| 23 | +lib_path = os.path.join(venv_path, "lib") |
| 24 | +python_dirs = [d for d in os.listdir(lib_path) if d.startswith("python")] |
| 25 | +if not python_dirs: |
| 26 | + print("ERROR: Could not find pythonX.Y folder in lib/") |
| 27 | + exit(1) |
| 28 | +site_packages_path = os.path.join(lib_path, python_dirs[0], "site-packages") |
| 29 | +if not os.path.exists(site_packages_path): |
| 30 | + print("ERROR: site-packages folder not found.") |
| 31 | + exit(1) |
| 32 | + |
| 33 | +# --- Step 4: Paths --- |
| 34 | +builder_py = os.path.join(site_packages_path, "jam/admin/builder.py") |
| 35 | +wsgi_py = os.path.join(site_packages_path, "jam/wsgi.py") |
| 36 | +html_old = os.path.join(site_packages_path, "jam/html/builder.html") |
| 37 | +html_new = os.path.join(site_packages_path, "jam/html", new_name) |
| 38 | + |
| 39 | +# --- Step 5: Backup files --- |
| 40 | +for file_path in [builder_py, wsgi_py, html_old]: |
| 41 | + if os.path.exists(file_path): |
| 42 | + backup_path = file_path + ".bak" |
| 43 | + if not os.path.exists(backup_path): |
| 44 | + shutil.copy(file_path, backup_path) |
| 45 | + print(f"Backup created: {backup_path}") |
| 46 | + else: |
| 47 | + print(f"WARNING: File not found, skipping backup: {file_path}") |
| 48 | + |
| 49 | +# --- Step 6: Patch builder.py --- |
| 50 | +if os.path.exists(builder_py): |
| 51 | + with open(builder_py, "r", encoding="utf-8") as f: |
| 52 | + content = f.read() |
| 53 | + content = content.replace("'builder.html'", f"'{new_name}'") |
| 54 | + with open(builder_py, "w", encoding="utf-8") as f: |
| 55 | + f.write(content) |
| 56 | + print(f"Patched: {builder_py}") |
| 57 | + |
| 58 | +# --- Step 7: Patch wsgi.py --- |
| 59 | +if os.path.exists(wsgi_py): |
| 60 | + with open(wsgi_py, "r", encoding="utf-8") as f: |
| 61 | + content = f.read() |
| 62 | + # Replace /builder.html → /new_name |
| 63 | + content = content.replace("/builder.html", f"/{new_name}") |
| 64 | + # Replace 'builder.html' → 'new_name' (handles list and os.path.exists) |
| 65 | + content = content.replace("'builder.html'", f"'{new_name}'") |
| 66 | + with open(wsgi_py, "w", encoding="utf-8") as f: |
| 67 | + f.write(content) |
| 68 | + print(f"Patched: {wsgi_py}") |
| 69 | + |
| 70 | +# --- Step 8: Move HTML file --- |
| 71 | +if os.path.exists(html_old): |
| 72 | + shutil.move(html_old, html_new) |
| 73 | + print(f"HTML file moved: {html_old} → {html_new}") |
| 74 | +else: |
| 75 | + print(f"WARNING: HTML file not found: {html_old}") |
| 76 | + |
| 77 | +print("\n✅ Done! Builder is now secured with new name:", new_name) |
| 78 | +print("Backups of original files are kept with .bak extension.") |
0 commit comments