Skip to content

Commit 4f2efef

Browse files
committed
Replace venv_path with venv_python_executable
1 parent e012cf8 commit 4f2efef

File tree

7 files changed

+44
-163
lines changed

7 files changed

+44
-163
lines changed

tests/plugins/test_loader.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,9 @@ def test_plugin_in_venv(test_plugin_package_req: str) -> None:
474474

475475
with DefaultIsolatedEnv() as venv:
476476
venv.install([test_plugin_package_req])
477-
with PluginLoader(variant_info, venv_path=Path(venv.path)) as loader:
477+
with PluginLoader(
478+
variant_info, venv_python_executable=Path(venv.python_executable)
479+
) as loader:
478480
assert set(loader.namespaces) == {"installable_plugin"}
479481

480482

tests/plugins/test_py_envs.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/test_api.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,6 @@ def get_or_skip_combinations() -> Generator[VariantDescription]:
203203
f"Did not conform the `VariantsJsonDict` format: {variants_json}"
204204
)
205205

206-
mocker.patch(
207-
"variantlib.plugins.loader.BasePluginLoader._load_all_plugins_from_tuple"
208-
).return_value = None
209206
mocker.patch(
210207
"variantlib.plugins.loader.BasePluginLoader.get_supported_configs"
211208
).return_value = {provider_cfg.namespace: provider_cfg for provider_cfg in configs}

variantlib/api.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,22 @@
4747
def get_variant_hashes_by_priority(
4848
*,
4949
variants_json: VariantsJsonDict | VariantsJson,
50-
venv_path: str | pathlib.Path | None = None,
50+
venv_python_executable: str | pathlib.Path | None = None,
5151
enable_optional_plugins: bool | list[VariantNamespace] = False,
5252
) -> list[str]:
5353
supported_vprops = []
5454
if not isinstance(variants_json, VariantsJson):
5555
variants_json = VariantsJson(variants_json)
5656

57-
venv_path = venv_path if venv_path is None else pathlib.Path(venv_path)
57+
venv_python_executable = (
58+
venv_python_executable
59+
if venv_python_executable is None
60+
else pathlib.Path(venv_python_executable)
61+
)
5862

5963
with PluginLoader(
6064
variant_info=variants_json,
61-
venv_path=venv_path,
65+
venv_python_executable=venv_python_executable,
6266
enable_optional_plugins=enable_optional_plugins,
6367
) as plugin_loader:
6468
supported_vprops = list(
@@ -94,7 +98,7 @@ def get_variant_hashes_by_priority(
9498
def validate_variant(
9599
variant_desc: VariantDescription,
96100
variant_info: VariantInfo,
97-
venv_path: str | pathlib.Path | None = None,
101+
venv_python_executable: str | pathlib.Path | None = None,
98102
) -> VariantValidationResult:
99103
"""
100104
Validate all metas in the variant description
@@ -106,11 +110,15 @@ def validate_variant(
106110
be verified.
107111
"""
108112

109-
venv_path = venv_path if venv_path is None else pathlib.Path(venv_path)
113+
venv_python_executable = (
114+
venv_python_executable
115+
if venv_python_executable is None
116+
else pathlib.Path(venv_python_executable)
117+
)
110118

111119
with PluginLoader(
112120
variant_info=variant_info,
113-
venv_path=venv_path,
121+
venv_python_executable=venv_python_executable,
114122
enable_optional_plugins=True,
115123
filter_plugins=list({vprop.namespace for vprop in variant_desc.properties}),
116124
) as plugin_loader:
@@ -150,7 +158,7 @@ def check_variant_supported(
150158
*,
151159
vdesc: VariantDescription | None = None,
152160
variant_info: VariantInfo,
153-
venv_path: str | pathlib.Path | None = None,
161+
venv_python_executable: str | pathlib.Path | None = None,
154162
enable_optional_plugins: bool | list[VariantNamespace] = False,
155163
) -> bool:
156164
"""Check if variant description is supported
@@ -170,11 +178,15 @@ def check_variant_supported(
170178
)
171179
vdesc = next(iter(variant_info.variants.values()))
172180

173-
venv_path = venv_path if venv_path is None else pathlib.Path(venv_path)
181+
venv_python_executable = (
182+
venv_python_executable
183+
if venv_python_executable is None
184+
else pathlib.Path(venv_python_executable)
185+
)
174186

175187
with PluginLoader(
176188
variant_info=variant_info,
177-
venv_path=venv_path,
189+
venv_python_executable=venv_python_executable,
178190
enable_optional_plugins=enable_optional_plugins,
179191
) as plugin_loader:
180192
supported_vprops = list(

variantlib/commands/make_variant.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ def _make_variant(
188188
vdesc_valid = validate_variant(
189189
vdesc,
190190
variant_info=variant_info,
191-
venv_path=venv.path if venv is not None else None,
191+
venv_python_executable=venv.python_executable
192+
if venv is not None
193+
else None,
192194
)
193195
if vdesc_valid.invalid_properties:
194196
invalid_str = ", ".join(

variantlib/plugins/loader.py

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import sys
1010
from abc import abstractmethod
1111
from collections.abc import Sequence
12-
from functools import partial
1312
from pathlib import Path
1413
from tempfile import TemporaryDirectory
1514
from typing import TYPE_CHECKING
@@ -24,17 +23,14 @@
2423
from variantlib.errors import PluginMissingError
2524
from variantlib.models.provider import ProviderConfig
2625
from variantlib.models.provider import VariantFeatureConfig
27-
from variantlib.plugins.py_envs import python_env
2826
from variantlib.validators.base import validate_matches_re
2927

3028
if TYPE_CHECKING:
31-
from contextlib import AbstractContextManager
3229
from types import TracebackType
3330

3431
from variantlib.models.variant import VariantDescription
3532
from variantlib.models.variant_info import ProviderInfo
3633
from variantlib.models.variant_info import VariantInfo
37-
from variantlib.plugins.py_envs import PythonEnv
3834
from variantlib.protocols import VariantNamespace
3935

4036
if sys.version_info >= (3, 10):
@@ -56,29 +52,22 @@ class BasePluginLoader:
5652
"""Load and query plugins"""
5753

5854
_namespace_map: dict[str, str] | None = None
59-
_python_ctx_manager: AbstractContextManager[PythonEnv] | None = None
60-
_python_ctx: PythonEnv | None = None
55+
_python_executable: Path
6156

6257
def __init__(
6358
self,
64-
venv_path: Path | None = None,
59+
venv_python_executable: Path | None = None,
6560
) -> None:
66-
self._python_ctx_factory = partial(python_env, venv_path=venv_path)
61+
self._python_executable = (
62+
venv_python_executable
63+
if venv_python_executable is not None
64+
else Path(sys.executable)
65+
)
6766

6867
def __enter__(self) -> Self:
69-
if self._python_ctx is not None:
68+
if self._namespace_map is not None:
7069
raise RuntimeError("Already inside the context manager!")
71-
72-
self._python_ctx_manager = self._python_ctx_factory()
73-
self._python_ctx = self._python_ctx_manager.__enter__()
74-
try:
75-
self._load_all_plugins()
76-
except Exception:
77-
self._python_ctx_manager.__exit__(*sys.exc_info())
78-
self._python_ctx = None
79-
self._python_ctx_manager = None
80-
raise
81-
70+
self._load_all_plugins()
8271
return self
8372

8473
def __exit__(
@@ -87,19 +76,13 @@ def __exit__(
8776
exc_value: BaseException | None,
8877
traceback: TracebackType | None,
8978
) -> None:
90-
if self._python_ctx is None:
79+
if self._namespace_map is None:
9180
raise RuntimeError("Context manager not entered!")
92-
assert self._python_ctx_manager is not None
93-
9481
self._namespace_map = None
95-
self._python_ctx = None
96-
self._python_ctx_manager.__exit__(exc_type, exc_value, traceback)
9782

9883
def _call_subprocess(
9984
self, plugin_apis: list[str], commands: dict[str, Any]
10085
) -> dict[str, dict[str, Any]]:
101-
assert self._python_ctx is not None
102-
10386
with TemporaryDirectory(prefix="variantlib") as temp_dir:
10487
script = Path(temp_dir) / "loader.py"
10588
script.write_bytes(
@@ -125,7 +108,7 @@ def _call_subprocess(
125108
args += ["-p", plugin_api]
126109

127110
process = subprocess.run( # noqa: S603
128-
[self._python_ctx.python_executable, script, *args],
111+
[self._python_executable, script, *args],
129112
input=json.dumps(commands).encode("utf8"),
130113
capture_output=True,
131114
check=False,
@@ -140,8 +123,6 @@ def _call_subprocess(
140123
def _load_all_plugins(self) -> None: ...
141124

142125
def _load_all_plugins_from_tuple(self, plugin_apis: list[str]) -> None:
143-
if self._python_ctx is None:
144-
raise RuntimeError("Context manager not entered!")
145126
if self._namespace_map is not None:
146127
raise RuntimeError(
147128
"Impossible to load plugins - `self._namespace_map` is not None"
@@ -189,8 +170,6 @@ def _load_all_plugins_from_tuple(self, plugin_apis: list[str]) -> None:
189170
)
190171

191172
def _check_plugins_loaded(self) -> None:
192-
if self._python_ctx is None:
193-
raise RuntimeError("Context manager not entered!")
194173
if self._namespace_map is None:
195174
raise NoPluginFoundError("No plugin has been loaded in the environment.")
196175

@@ -285,15 +264,15 @@ class PluginLoader(BasePluginLoader):
285264
def __init__(
286265
self,
287266
variant_info: VariantInfo,
288-
venv_path: Path | None = None,
267+
venv_python_executable: Path | None = None,
289268
enable_optional_plugins: bool | list[VariantNamespace] = False,
290269
filter_plugins: list[VariantNamespace] | None = None,
291270
) -> None:
292271
self._variant_info = variant_info
293272
self._enable_optional_plugins = enable_optional_plugins
294273
self._filter_plugins = filter_plugins
295274
self._environment = cast("dict[str, str]", default_environment())
296-
super().__init__(venv_path=venv_path)
275+
super().__init__(venv_python_executable=venv_python_executable)
297276

298277
def _optional_provider_enabled(self, namespace: str) -> bool:
299278
# if enable_optional_plugins is a bool, it controls all plugins
@@ -348,9 +327,9 @@ class EntryPointPluginLoader(BasePluginLoader):
348327

349328
def __init__(
350329
self,
351-
venv_path: Path | None = None,
330+
venv_python_executable: Path | None = None,
352331
) -> None:
353-
super().__init__(venv_path=venv_path)
332+
super().__init__(venv_python_executable=venv_python_executable)
354333

355334
def _load_all_plugins(self) -> None:
356335
if self._namespace_map is not None:
@@ -396,10 +375,10 @@ class ListPluginLoader(BasePluginLoader):
396375
def __init__(
397376
self,
398377
plugin_apis: list[str],
399-
venv_path: Path | None = None,
378+
venv_python_executable: Path | None = None,
400379
) -> None:
401380
self._plugin_apis = list(plugin_apis)
402-
super().__init__(venv_path=venv_path)
381+
super().__init__(venv_python_executable=venv_python_executable)
403382

404383
def _load_all_plugins(self) -> None:
405384
if self._namespace_map is not None:

variantlib/plugins/py_envs.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)