""" Script per preparare Python embeddable con le dipendenze di MediaTrack """ import subprocess import zipfile import shutil from pathlib import Path import sys # Configurazione PYTHON_EMBED_ZIP = Path("python-3.11.9-embed-amd64.zip") # Da scaricare OUTPUT_DIR = Path("templates/python_embeddable") OUTPUT_ZIP = Path("templates/python_embeddable.zip") # Dipendenze MediaTrack (da requirements.txt o manualmente) DEPENDENCIES = [ "Flask>=2.0", "pywebview>=3.0", "requests", "rapidfuzz", "werkzeug", ] def main(): print("=" * 70) print(" Preparazione Python Embeddable per MediaTrack") print("=" * 70) # 1. Verifica che lo zip esista if not PYTHON_EMBED_ZIP.exists(): print(f"\n❌ ERRORE: {PYTHON_EMBED_ZIP} non trovato!") print("\nScarica Python embeddable da:") print("https://www.python.org/downloads/windows/") print("\nCerca: 'Windows embeddable package (64-bit)'") print(f"Salva come: {PYTHON_EMBED_ZIP}") sys.exit(1) # 2. Estrai Python embeddable print(f"\n📦 Estrazione {PYTHON_EMBED_ZIP}...") if OUTPUT_DIR.exists(): shutil.rmtree(OUTPUT_DIR) OUTPUT_DIR.mkdir(parents=True, exist_ok=True) with zipfile.ZipFile(PYTHON_EMBED_ZIP, 'r') as zip_ref: zip_ref.extractall(OUTPUT_DIR) print("✅ Estrazione completata") # 3. Abilita pip (modifica python311._pth) print("\n🔧 Abilitazione pip...") pth_files = list(OUTPUT_DIR.glob("python*._pth")) if pth_files: pth_file = pth_files[0] content = pth_file.read_text() # Decommenta import site content = content.replace("#import site", "import site") content += "\nLib\\site-packages\n" pth_file.write_text(content) print(f"✅ Modificato {pth_file.name}") # 4. Scarica get-pip.py print("\n📥 Download get-pip.py...") import urllib.request get_pip_url = "https://bootstrap.pypa.io/get-pip.py" get_pip_path = OUTPUT_DIR / "get-pip.py" try: urllib.request.urlretrieve(get_pip_url, get_pip_path) print("✅ get-pip.py scaricato") except Exception as e: print(f"⚠️ Errore download: {e}") print("Scarica manualmente da: https://bootstrap.pypa.io/get-pip.py") print(f"E salva in: {get_pip_path}") sys.exit(1) # 5. Installa pip print("\n🔨 Installazione pip...") python_exe = OUTPUT_DIR / "python.exe" result = subprocess.run( [str(python_exe), "get-pip.py"], cwd=str(OUTPUT_DIR), capture_output=True, text=True ) if result.returncode == 0: print("✅ pip installato") else: print(f"⚠️ Warning: {result.stderr}") # 6. Installa dipendenze MediaTrack print("\n📦 Installazione dipendenze MediaTrack...") for dep in DEPENDENCIES: print(f" - Installazione {dep}...") result = subprocess.run( [str(python_exe), "-m", "pip", "install", dep], cwd=str(OUTPUT_DIR), capture_output=True, text=True ) if result.returncode == 0: print(f" ✅ {dep}") else: print(f" ⚠️ {dep}: {result.stderr[:100]}") # 7. Crea archivio zip finale print(f"\n📦 Creazione {OUTPUT_ZIP}...") if OUTPUT_ZIP.exists(): OUTPUT_ZIP.unlink() shutil.make_archive( str(OUTPUT_ZIP.with_suffix('')), 'zip', OUTPUT_DIR ) print(f"✅ Creato: {OUTPUT_ZIP}") print(f" Dimensione: {OUTPUT_ZIP.stat().st_size / 1024 / 1024:.1f} MB") # 8. Cleanup (opzionale) cleanup = input("\n🗑️ Rimuovere directory temporanea? (s/n): ") if cleanup.lower() == 's': shutil.rmtree(OUTPUT_DIR) print("✅ Directory temporanea rimossa") print("\n" + "=" * 70) print("🎉 Python embeddable pronto per il deploy!") print("=" * 70) print(f"\nFile creato: {OUTPUT_ZIP.absolute()}") print("\nProssimi passi:") print("1. Esegui: quick_deploy.bat") print("2. Python embeddable verrà deployato sul server") print("3. MediaTrack userà questo Python con tutte le sue dipendenze") if __name__ == "__main__": main()