Update app.py
Browse files
app.py
CHANGED
@@ -30,46 +30,52 @@ async def stream_log(file_path):
|
|
30 |
import os
|
31 |
import subprocess
|
32 |
import urllib.request
|
33 |
-
|
34 |
|
35 |
-
def install_python_version(python_version: str):
|
36 |
-
|
37 |
-
|
|
|
38 |
|
39 |
# Check if Python is already installed
|
40 |
if os.path.isfile(python_bin):
|
41 |
return python_bin, pip_bin
|
42 |
|
43 |
-
# Download and install Python if not present
|
44 |
try:
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
69 |
raise RuntimeError(f"Error installing Python {python_version}: {str(e)}")
|
|
|
|
|
70 |
|
71 |
return python_bin, pip_bin
|
72 |
|
|
|
73 |
# @app.post("/download-dependencies")
|
74 |
# async def download_dependencies(requirements_file: UploadFile = File(...)):
|
75 |
# try:
|
|
|
30 |
import os
|
31 |
import subprocess
|
32 |
import urllib.request
|
33 |
+
import shutil
|
34 |
|
35 |
+
def install_python_version(python_version: str, use_deb: bool = False, install_dir: str = os.path.expanduser("~/local")):
|
36 |
+
base_url = "https://www.python.org/ftp/python"
|
37 |
+
python_bin = f"{install_dir}/bin/python{python_version}"
|
38 |
+
pip_bin = f"{install_dir}/bin/pip{python_version}"
|
39 |
|
40 |
# Check if Python is already installed
|
41 |
if os.path.isfile(python_bin):
|
42 |
return python_bin, pip_bin
|
43 |
|
|
|
44 |
try:
|
45 |
+
os.makedirs(install_dir, exist_ok=True)
|
46 |
+
if use_deb:
|
47 |
+
raise NotImplementedError("Debian package installation requires root permissions.")
|
48 |
+
else:
|
49 |
+
# For .tar.xz binary distribution
|
50 |
+
tar_url = f"{base_url}/{python_version}/Python-{python_version}-linux-x86_64.tar.xz"
|
51 |
+
tar_path = f"/tmp/Python-{python_version}.tar.xz"
|
52 |
+
|
53 |
+
# Download Python source tarball
|
54 |
+
urllib.request.urlretrieve(tar_url, tar_path)
|
55 |
+
|
56 |
+
# Extract tarball
|
57 |
+
subprocess.run(["tar", "-xf", tar_path, "-C", "/tmp"], check=True)
|
58 |
+
|
59 |
+
# Build and install Python to the specified directory
|
60 |
+
python_src_path = f"/tmp/Python-{python_version}"
|
61 |
+
subprocess.run([f"{python_src_path}/configure", f"--prefix={install_dir}"], cwd=python_src_path, check=True)
|
62 |
+
subprocess.run(["make", "-j"], cwd=python_src_path, check=True)
|
63 |
+
subprocess.run(["make", "install"], cwd=python_src_path, check=True)
|
64 |
+
|
65 |
+
# Clean up
|
66 |
+
os.remove(tar_path)
|
67 |
+
shutil.rmtree(python_src_path)
|
68 |
+
|
69 |
+
except urllib.error.HTTPError as e:
|
70 |
+
raise RuntimeError(f"HTTP Error: {str(e)}")
|
71 |
+
except subprocess.CalledProcessError as e:
|
72 |
raise RuntimeError(f"Error installing Python {python_version}: {str(e)}")
|
73 |
+
except Exception as e:
|
74 |
+
raise RuntimeError(f"Error: {str(e)}")
|
75 |
|
76 |
return python_bin, pip_bin
|
77 |
|
78 |
+
|
79 |
# @app.post("/download-dependencies")
|
80 |
# async def download_dependencies(requirements_file: UploadFile = File(...)):
|
81 |
# try:
|