|
4 | 4 |
|
5 | 5 | import os
|
6 | 6 | import sys
|
| 7 | +from abc import ABC |
7 | 8 | from pathlib import Path
|
8 | 9 | from typing import TYPE_CHECKING, Any, cast
|
9 | 10 |
|
10 | 11 | from virtualenv import __version__ as virtualenv_version
|
11 |
| -from virtualenv import session_via_cli |
| 12 | +from virtualenv import app_data, session_via_cli |
| 13 | +from virtualenv.discovery import cached_py_info |
| 14 | +from virtualenv.discovery.py_spec import PythonSpec |
12 | 15 |
|
13 | 16 | from tox.config.loader.str_convert import StrConvert
|
14 | 17 | from tox.execute.local_sub_process import LocalSubProcessExecutor
|
|
17 | 20 |
|
18 | 21 | if TYPE_CHECKING:
|
19 | 22 | from virtualenv.create.creator import Creator
|
| 23 | + from virtualenv.discovery.py_info import PythonInfo as VirtualenvPythonInfo |
20 | 24 | from virtualenv.run.session import Session
|
21 | 25 |
|
22 | 26 | from tox.execute.api import Execute
|
23 | 27 | from tox.tox_env.api import ToxEnvCreateArgs
|
24 | 28 |
|
25 | 29 |
|
26 |
| -class VirtualEnv(Python): |
| 30 | +class VirtualEnv(Python, ABC): |
27 | 31 | """A python executor that uses the virtualenv project with pip."""
|
28 | 32 |
|
29 | 33 | def __init__(self, create_args: ToxEnvCreateArgs) -> None:
|
@@ -167,3 +171,30 @@ def environment_variables(self) -> dict[str, str]:
|
167 | 171 | environment_variables = super().environment_variables
|
168 | 172 | environment_variables["VIRTUAL_ENV"] = str(self.conf["env_dir"])
|
169 | 173 | return environment_variables
|
| 174 | + |
| 175 | + @classmethod |
| 176 | + def python_spec_for_path(cls, path: Path) -> PythonSpec: |
| 177 | + """ |
| 178 | + Get the spec for an absolute path to a Python executable. |
| 179 | +
|
| 180 | + :param path: the path investigated |
| 181 | + :return: the found spec |
| 182 | + """ |
| 183 | + info = cls.get_virtualenv_py_info(path) |
| 184 | + return PythonSpec.from_string_spec( |
| 185 | + f"{info.implementation}{info.version_info.major}{info.version_info.minor}-{info.architecture}" |
| 186 | + ) |
| 187 | + |
| 188 | + @staticmethod |
| 189 | + def get_virtualenv_py_info(path: Path) -> VirtualenvPythonInfo: |
| 190 | + """ |
| 191 | + Get the version info for an absolute path to a Python executable. |
| 192 | +
|
| 193 | + :param path: the path investigated |
| 194 | + :return: the found information (cached) |
| 195 | + """ |
| 196 | + return cached_py_info.from_exe( |
| 197 | + cached_py_info.PythonInfo, |
| 198 | + app_data.make_app_data(None, read_only=False, env=os.environ), |
| 199 | + str(path), |
| 200 | + ) |
0 commit comments