9
9
import sys
10
10
from abc import abstractmethod
11
11
from collections .abc import Sequence
12
- from functools import partial
13
12
from pathlib import Path
14
13
from tempfile import TemporaryDirectory
15
14
from typing import TYPE_CHECKING
24
23
from variantlib .errors import PluginMissingError
25
24
from variantlib .models .provider import ProviderConfig
26
25
from variantlib .models .provider import VariantFeatureConfig
27
- from variantlib .plugins .py_envs import python_env
28
26
from variantlib .validators .base import validate_matches_re
29
27
30
28
if TYPE_CHECKING :
31
- from contextlib import AbstractContextManager
32
29
from types import TracebackType
33
30
34
31
from variantlib .models .variant import VariantDescription
35
32
from variantlib .models .variant_info import ProviderInfo
36
33
from variantlib .models .variant_info import VariantInfo
37
- from variantlib .plugins .py_envs import PythonEnv
38
34
from variantlib .protocols import VariantNamespace
39
35
40
36
if sys .version_info >= (3 , 10 ):
@@ -56,29 +52,22 @@ class BasePluginLoader:
56
52
"""Load and query plugins"""
57
53
58
54
_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
61
56
62
57
def __init__ (
63
58
self ,
64
- venv_path : Path | None = None ,
59
+ venv_python_executable : Path | None = None ,
65
60
) -> 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
+ )
67
66
68
67
def __enter__ (self ) -> Self :
69
- if self ._python_ctx is not None :
68
+ if self ._namespace_map is not None :
70
69
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 ()
82
71
return self
83
72
84
73
def __exit__ (
@@ -87,19 +76,13 @@ def __exit__(
87
76
exc_value : BaseException | None ,
88
77
traceback : TracebackType | None ,
89
78
) -> None :
90
- if self ._python_ctx is None :
79
+ if self ._namespace_map is None :
91
80
raise RuntimeError ("Context manager not entered!" )
92
- assert self ._python_ctx_manager is not None
93
-
94
81
self ._namespace_map = None
95
- self ._python_ctx = None
96
- self ._python_ctx_manager .__exit__ (exc_type , exc_value , traceback )
97
82
98
83
def _call_subprocess (
99
84
self , plugin_apis : list [str ], commands : dict [str , Any ]
100
85
) -> dict [str , dict [str , Any ]]:
101
- assert self ._python_ctx is not None
102
-
103
86
with TemporaryDirectory (prefix = "variantlib" ) as temp_dir :
104
87
script = Path (temp_dir ) / "loader.py"
105
88
script .write_bytes (
@@ -125,7 +108,7 @@ def _call_subprocess(
125
108
args += ["-p" , plugin_api ]
126
109
127
110
process = subprocess .run ( # noqa: S603
128
- [self ._python_ctx . python_executable , script , * args ],
111
+ [self ._python_executable , script , * args ],
129
112
input = json .dumps (commands ).encode ("utf8" ),
130
113
capture_output = True ,
131
114
check = False ,
@@ -140,8 +123,6 @@ def _call_subprocess(
140
123
def _load_all_plugins (self ) -> None : ...
141
124
142
125
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!" )
145
126
if self ._namespace_map is not None :
146
127
raise RuntimeError (
147
128
"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:
189
170
)
190
171
191
172
def _check_plugins_loaded (self ) -> None :
192
- if self ._python_ctx is None :
193
- raise RuntimeError ("Context manager not entered!" )
194
173
if self ._namespace_map is None :
195
174
raise NoPluginFoundError ("No plugin has been loaded in the environment." )
196
175
@@ -285,15 +264,15 @@ class PluginLoader(BasePluginLoader):
285
264
def __init__ (
286
265
self ,
287
266
variant_info : VariantInfo ,
288
- venv_path : Path | None = None ,
267
+ venv_python_executable : Path | None = None ,
289
268
enable_optional_plugins : bool | list [VariantNamespace ] = False ,
290
269
filter_plugins : list [VariantNamespace ] | None = None ,
291
270
) -> None :
292
271
self ._variant_info = variant_info
293
272
self ._enable_optional_plugins = enable_optional_plugins
294
273
self ._filter_plugins = filter_plugins
295
274
self ._environment = cast ("dict[str, str]" , default_environment ())
296
- super ().__init__ (venv_path = venv_path )
275
+ super ().__init__ (venv_python_executable = venv_python_executable )
297
276
298
277
def _optional_provider_enabled (self , namespace : str ) -> bool :
299
278
# if enable_optional_plugins is a bool, it controls all plugins
@@ -348,9 +327,9 @@ class EntryPointPluginLoader(BasePluginLoader):
348
327
349
328
def __init__ (
350
329
self ,
351
- venv_path : Path | None = None ,
330
+ venv_python_executable : Path | None = None ,
352
331
) -> None :
353
- super ().__init__ (venv_path = venv_path )
332
+ super ().__init__ (venv_python_executable = venv_python_executable )
354
333
355
334
def _load_all_plugins (self ) -> None :
356
335
if self ._namespace_map is not None :
@@ -396,10 +375,10 @@ class ListPluginLoader(BasePluginLoader):
396
375
def __init__ (
397
376
self ,
398
377
plugin_apis : list [str ],
399
- venv_path : Path | None = None ,
378
+ venv_python_executable : Path | None = None ,
400
379
) -> None :
401
380
self ._plugin_apis = list (plugin_apis )
402
- super ().__init__ (venv_path = venv_path )
381
+ super ().__init__ (venv_python_executable = venv_python_executable )
403
382
404
383
def _load_all_plugins (self ) -> None :
405
384
if self ._namespace_map is not None :
0 commit comments