File size: 1,567 Bytes
c33448f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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")