| 1 |
"""Virtual environment management.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import collections.abc as c |
| 6 |
import json |
| 7 |
import os |
| 8 |
import pathlib |
| 9 |
import sys |
| 10 |
import typing as t |
| 11 |
|
| 12 |
from .config import ( |
| 13 |
EnvironmentConfig, |
| 14 |
) |
| 15 |
|
| 16 |
from .util import ( |
| 17 |
find_python, |
| 18 |
SubprocessError, |
| 19 |
ANSIBLE_TEST_TARGET_TOOLS_ROOT, |
| 20 |
display, |
| 21 |
remove_tree, |
| 22 |
ApplicationError, |
| 23 |
str_to_version, |
| 24 |
raw_command, |
| 25 |
) |
| 26 |
|
| 27 |
from .util_common import ( |
| 28 |
run_command, |
| 29 |
ResultType, |
| 30 |
) |
| 31 |
|
| 32 |
from .host_configs import ( |
| 33 |
VirtualPythonConfig, |
| 34 |
PythonConfig, |
| 35 |
) |
| 36 |
|
| 37 |
from .python_requirements import ( |
| 38 |
collect_bootstrap, |
| 39 |
run_pip, |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
def get_virtual_python( |
| 44 |
args: EnvironmentConfig, |
| 45 |
python: VirtualPythonConfig, |
| 46 |
) -> VirtualPythonConfig: |
| 47 |
"""Create a virtual environment for the given Python and return the path to its root.""" |
| 48 |
if python.system_site_packages: |
| 49 |
suffix = '-ssp' |
| 50 |
else: |
| 51 |
suffix = '' |
| 52 |
|
| 53 |
virtual_environment_path = os.path.join(ResultType.TMP.path, 'delegation', f'python{python.version}{suffix}') |
| 54 |
virtual_environment_marker = os.path.join(virtual_environment_path, 'marker.txt') |
| 55 |
|
| 56 |
virtual_environment_python = VirtualPythonConfig( |
| 57 |
version=python.version, |
| 58 |
path=os.path.join(virtual_environment_path, 'bin', 'python'), |
| 59 |
system_site_packages=python.system_site_packages, |
| 60 |
) |
| 61 |
|
| 62 |
if os.path.exists(virtual_environment_marker): |
| 63 |
display.info('Using existing Python %s virtual environment: %s' % (python.version, virtual_environment_path), verbosity=1) |
| 64 |
else: |
| 65 |
# a virtualenv without a marker is assumed to have been partially created |
| 66 |
remove_tree(virtual_environment_path) |
| 67 |
|
| 68 |
if not create_virtual_environment(args, python, virtual_environment_path, python.system_site_packages): |
| 69 |
raise ApplicationError(f'Python {python.version} does not provide virtual environment support.') |
| 70 |
|
| 71 |
commands = collect_bootstrap(virtual_environment_python) |
| 72 |
|
| 73 |
run_pip(args, virtual_environment_python, commands, None) # get_virtual_python() |
| 74 |
|
| 75 |
# touch the marker to keep track of when the virtualenv was last used |
| 76 |
pathlib.Path(virtual_environment_marker).touch() |
| 77 |
|
| 78 |
return virtual_environment_python |
| 79 |
|
| 80 |
|
| 81 |
def create_virtual_environment( |
| 82 |
args: EnvironmentConfig, |
| 83 |
python: PythonConfig, |
| 84 |
path: str, |
| 85 |
system_site_packages: bool = False, |
| 86 |
pip: bool = False, |
| 87 |
) -> bool: |
| 88 |
"""Create a virtual environment using venv for the requested Python version.""" |
| 89 |
if not os.path.exists(python.path): |
| 90 |
# the requested python version could not be found |
| 91 |
return False |
| 92 |
|
| 93 |
# creating a virtual environment using 'venv' when running in a virtual environment created by 'virtualenv' results |
| 94 |
# in a copy of the original virtual environment instead of creation of a new one |
| 95 |
# avoid this issue by only using "real" python interpreters to invoke 'venv' |
| 96 |
for real_python in iterate_real_pythons(python.version): |
| 97 |
if run_venv(args, real_python, system_site_packages, pip, path): |
| 98 |
display.info('Created Python %s virtual environment using "venv": %s' % (python.version, path), verbosity=1) |
| 99 |
return True |
| 100 |
|
| 101 |
# something went wrong, most likely the package maintainer for the Python installation removed ensurepip |
| 102 |
# which will prevent creation of a virtual environment without installation of other OS packages |
| 103 |
return False |
| 104 |
|
| 105 |
|
| 106 |
def iterate_real_pythons(version: str) -> c.Iterable[str]: |
| 107 |
""" |
| 108 |
Iterate through available real python interpreters of the requested version. |
| 109 |
The current interpreter will be checked and then the path will be searched. |
| 110 |
""" |
| 111 |
version_info = str_to_version(version) |
| 112 |
current_python = None |
| 113 |
|
| 114 |
if version_info == sys.version_info[:len(version_info)]: |
| 115 |
current_python = sys.executable |
| 116 |
real_prefix = get_python_real_prefix(current_python) |
| 117 |
|
| 118 |
if real_prefix: |
| 119 |
current_python = find_python(version, os.path.join(real_prefix, 'bin')) |
| 120 |
|
| 121 |
if current_python: |
| 122 |
yield current_python |
| 123 |
|
| 124 |
path = os.environ.get('PATH', os.path.defpath) |
| 125 |
|
| 126 |
if not path: |
| 127 |
return |
| 128 |
|
| 129 |
found_python = find_python(version, path) |
| 130 |
|
| 131 |
if not found_python: |
| 132 |
return |
| 133 |
|
| 134 |
if found_python == current_python: |
| 135 |
return |
| 136 |
|
| 137 |
real_prefix = get_python_real_prefix(found_python) |
| 138 |
|
| 139 |
if real_prefix: |
| 140 |
found_python = find_python(version, os.path.join(real_prefix, 'bin')) |
| 141 |
|
| 142 |
if found_python: |
| 143 |
yield found_python |
| 144 |
|
| 145 |
|
| 146 |
def get_python_real_prefix(python_path: str) -> t.Optional[str]: |
| 147 |
""" |
| 148 |
Return the real prefix of the specified interpreter or None if the interpreter is not a virtual environment created by 'virtualenv'. |
| 149 |
""" |
| 150 |
cmd = [python_path, os.path.join(os.path.join(ANSIBLE_TEST_TARGET_TOOLS_ROOT, 'virtualenvcheck.py'))] |
| 151 |
check_result = json.loads(raw_command(cmd, capture=True)[0]) |
| 152 |
real_prefix = check_result['real_prefix'] |
| 153 |
return real_prefix |
| 154 |
|
| 155 |
|
| 156 |
def run_venv( |
| 157 |
args: EnvironmentConfig, |
| 158 |
run_python: str, |
| 159 |
system_site_packages: bool, |
| 160 |
pip: bool, |
| 161 |
path: str, |
| 162 |
) -> bool: |
| 163 |
"""Create a virtual environment using the 'venv' module.""" |
| 164 |
cmd = [run_python, '-m', 'venv'] |
| 165 |
|
| 166 |
if system_site_packages: |
| 167 |
cmd.append('--system-site-packages') |
| 168 |
|
| 169 |
if not pip: |
| 170 |
cmd.append('--without-pip') |
| 171 |
|
| 172 |
cmd.append(path) |
| 173 |
|
| 174 |
try: |
| 175 |
run_command(args, cmd, capture=True) |
| 176 |
except SubprocessError as ex: |
| 177 |
remove_tree(path) |
| 178 |
|
| 179 |
if args.verbosity > 1: |
| 180 |
display.error(ex.message) |
| 181 |
|
| 182 |
return False |
| 183 |
|
| 184 |
return True |