import os import subprocess from io_utils import write_log_to_user_file def prepare_venv(execution_id, deps): python_executable = "python" venv_base = f"tmp/venvs/{execution_id}" pip_executable = os.path.join(venv_base, "bin", "pip") # Check pyver write_log_to_user_file(execution_id, "Checking Python version\n") p = subprocess.run([python_executable, "--version"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) write_log_to_user_file(execution_id, p.stdout.decode()) if p.returncode != 0: raise RuntimeError(f"{p.args} ended with {p.returncode}") # Create venv write_log_to_user_file(execution_id, "Creating virtual environment\n") p = subprocess.run([python_executable, "-m", "venv", venv_base, "--clear"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) write_log_to_user_file(execution_id, p.stdout.decode()) if p.returncode != 0: raise RuntimeError(f"{p.args} ended with {p.returncode}") # Output requirements.txt requirement_file = os.path.join(venv_base, "requirements.txt") with open(requirement_file, "w") as f: f.writelines(deps) # Install deps write_log_to_user_file(execution_id, "Installing dependencies\n") p = subprocess.run([pip_executable, "install", "-r", requirement_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) write_log_to_user_file(execution_id, p.stdout.decode()) if p.returncode != 0: raise RuntimeError(f"{p.args} ended with {p.returncode}") return os.path.join(venv_base, "bin", "giskard_scanner")