Azeez98 commited on
Commit
d1d6946
·
verified ·
1 Parent(s): 5d0e7d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -29
app.py CHANGED
@@ -30,46 +30,52 @@ async def stream_log(file_path):
30
  import os
31
  import subprocess
32
  import urllib.request
33
- from pathlib import Path
34
 
35
- def install_python_version(python_version: str):
36
- python_bin = f"/usr/local/bin/python{python_version}"
37
- pip_bin = f"/usr/local/bin/pip{python_version}"
 
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
- url = f"https://www.python.org/ftp/python/{python_version}/Python-{python_version}.tgz"
46
- tar_path = f"/tmp/Python-{python_version}.tgz"
47
-
48
- # Download Python source tarball
49
- urllib.request.urlretrieve(url, tar_path)
50
-
51
- # Extract tarball
52
- subprocess.run(["tar", "-xzf", tar_path, "-C", "/tmp"], check=True)
53
-
54
- # Build and install Python
55
- python_src_path = f"/tmp/Python-{python_version}"
56
- subprocess.run([f"{python_src_path}/configure"], cwd=python_src_path, check=True)
57
- subprocess.run(["make", "-j"], cwd=python_src_path, check=True)
58
- subprocess.run(["make", "altinstall", f"EXTRA_CFLAGS='-I/usr/local/include'"], cwd=python_src_path, check=True)
59
-
60
- # Clean up
61
- os.remove(tar_path)
62
- shutil.rmtree(python_src_path)
63
-
64
- # Update alternatives (if necessary)
65
- subprocess.run(["update-alternatives", "--install", "/usr/bin/python3", "python3", python_bin, "1"], check=True)
66
- subprocess.run(["update-alternatives", "--install", "/usr/bin/pip", "pip", pip_bin, "1"], check=True)
67
-
68
- except Exception as e:
 
 
 
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: