File: managed_virtual_env.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (28 lines) | stat: -rw-r--r-- 1,056 bytes parent folder | download | duplicates (2)
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
import os, logging, glob, subprocess, sys
from ci_tools.functions import cleanup_directory


class ManagedVirtualEnv:
    def __init__(self, path: str, name: str):
        self.path = os.path.join(path, name)

        # todo: do we want to accept refresh?
        if os.path.exists(self.path):
            cleanup_directory(self.path)

    def create(self):
        logging.info("Creating virtual environment [{}]".format(self.path))
        subprocess.check_call([sys.executable, "-m", "venv", "ENV_DIR", self.path])
        self.python_executable = self._find_python_executable()

    def clear_venv(self):
        subprocess.check_call([sys.executable, "-m", "venv", "--clear", "ENV_DIR", self.path])

    def _find_python_executable(self):
        paths = glob.glob(os.path.join(self.path, "*", "python")) + glob.glob(
            os.path.join(self.path, "*", "python.exe")
        )
        if not paths:
            logging.error(f"Failed to find path to python executable in virtual env:{self.path}")
            sys.exit(1)
        return paths[0]