Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import zipfile
|
| 3 |
+
import importlib.util
|
| 4 |
+
import os
|
| 5 |
+
import shutil
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import subprocess
|
| 8 |
+
|
| 9 |
+
REMOTE_CODE_URL = os.environ.get("api_url")
|
| 10 |
+
|
| 11 |
+
def download_zip(remote_url, local_zipfile):
|
| 12 |
+
response = requests.get(remote_url, stream=True)
|
| 13 |
+
if response.status_code == 200:
|
| 14 |
+
with open(local_zipfile, "wb") as f:
|
| 15 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 16 |
+
if chunk:
|
| 17 |
+
f.write(chunk)
|
| 18 |
+
else:
|
| 19 |
+
raise Exception(f"Failed to download ZIP file, status code: {response.status_code}")
|
| 20 |
+
|
| 21 |
+
def unzip_file(local_zipfile, extract_dir):
|
| 22 |
+
if not os.path.exists(extract_dir):
|
| 23 |
+
os.makedirs(extract_dir, exist_ok=True)
|
| 24 |
+
with zipfile.ZipFile(local_zipfile, "r") as zip_ref:
|
| 25 |
+
zip_ref.extractall(extract_dir)
|
| 26 |
+
|
| 27 |
+
def find_py_file(extract_dir):
|
| 28 |
+
py_files = []
|
| 29 |
+
for root, dirs, files in os.walk(extract_dir):
|
| 30 |
+
for file in files:
|
| 31 |
+
if file.lower().endswith(".py"):
|
| 32 |
+
py_files.append(os.path.join(root, file))
|
| 33 |
+
if len(py_files) == 0:
|
| 34 |
+
raise FileNotFoundError("No .py file found in the extracted directory.")
|
| 35 |
+
elif len(py_files) > 1:
|
| 36 |
+
raise RuntimeError("Multiple .py files found in the ZIP package.")
|
| 37 |
+
else:
|
| 38 |
+
return py_files[0]
|
| 39 |
+
|
| 40 |
+
def execute_py_file(py_file_path):
|
| 41 |
+
spec = importlib.util.spec_from_file_location("dynamic_module", py_file_path)
|
| 42 |
+
dynamic_module = importlib.util.module_from_spec(spec)
|
| 43 |
+
spec.loader.exec_module(dynamic_module)
|
| 44 |
+
if hasattr(dynamic_module, "build_app"):
|
| 45 |
+
app = dynamic_module.build_app()
|
| 46 |
+
app.launch()
|
| 47 |
+
else:
|
| 48 |
+
subprocess.run(["python", py_file_path], check=True)
|
| 49 |
+
|
| 50 |
+
def main():
|
| 51 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 52 |
+
local_zipfile = f"downloaded_app_{timestamp}.zip"
|
| 53 |
+
extract_dir = f"extracted_code_{timestamp}"
|
| 54 |
+
try:
|
| 55 |
+
download_zip(REMOTE_CODE_URL, local_zipfile)
|
| 56 |
+
unzip_file(local_zipfile, extract_dir)
|
| 57 |
+
py_file_path = find_py_file(extract_dir)
|
| 58 |
+
execute_py_file(py_file_path)
|
| 59 |
+
finally:
|
| 60 |
+
if os.path.exists(local_zipfile):
|
| 61 |
+
os.remove(local_zipfile)
|
| 62 |
+
if os.path.exists(extract_dir):
|
| 63 |
+
shutil.rmtree(extract_dir)
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
main()
|