55from abc import abstractmethod
66from contextlib import contextmanager
77from typing import Generator
8- from typing import List
98from typing import Optional
109
1110from ...models .base import BaseRequirements
12- from .commands import get_manager_command
1311
1412logger = logging .getLogger (__name__ )
1513
@@ -34,18 +32,17 @@ class BaseManager:
3432 """
3533
3634 NAME = NotImplemented
35+ PRIORITY = NotImplemented
3736
3837 def __init__ (self , * command : str ) -> None :
3938 if not command :
40- command = get_manager_command ( self . NAME )
39+ raise ValueError ( f" { type ( self ). __name__ } needs an associated shell command" )
4140 self ._cmd_args = command
4241
4342 def gather_requirements (self ) -> Optional [BaseRequirements ]:
4443 """Return requirements generated from the current python environment."""
45- from .supported import get_supported_managers
46-
47- manager_version = get_supported_managers ()[self .NAME ].version
48- if not manager_version :
44+ manager_version = self .current_version ()
45+ if manager_version is None :
4946 raise RuntimeError (f"{ self .NAME !r} is not installed" )
5047
5148 try :
@@ -56,6 +53,16 @@ def gather_requirements(self) -> Optional[BaseRequirements]:
5653 )
5754 return None
5855
56+ @abstractmethod
57+ def version (self ) -> Optional [str ]:
58+ """Returns None when this manager is not available."""
59+ pass
60+
61+ @abstractmethod
62+ def is_active (self ) -> bool :
63+ """Manager is explicitly active."""
64+ pass
65+
5966 def install_requirements (self , requirements : BaseRequirements ) -> None :
6067 """Install requirements into the current python environment."""
6168 try :
@@ -74,13 +81,25 @@ def _gather_requirements(self, manager_version: str) -> BaseRequirements:
7481 def _install_requirements (self , requirements : BaseRequirements ) -> None :
7582 pass
7683
77- def _check_output (self , * args ) -> str :
78- return _check_output ( [* self ._cmd_args , * args ])
84+ def _check_output (self , * args : str ) -> str :
85+ return self . _check_output_raw ( * [* self ._cmd_args , * args ])
7986
80- def _check_call (self , * args , raw : bool = False ) -> int :
81- if raw :
82- return _check_call ([* args ])
83- return _check_call ([* self ._cmd_args , * args ])
87+ def _check_call (self , * args : str ) -> int :
88+ return self ._check_call_raw (* [* self ._cmd_args , * args ])
89+
90+ @staticmethod
91+ def _check_output_raw (* args : str ) -> str :
92+ try :
93+ return subprocess .check_output (args , text = True )
94+ except Exception as ex :
95+ raise RuntimeError (f"Command failed: { args } " ) from ex
96+
97+ @staticmethod
98+ def _check_call_raw (* args : str ) -> int :
99+ try :
100+ return subprocess .check_call (args )
101+ except Exception as ex :
102+ raise RuntimeError (f"Command failed: { args } " ) from ex
84103
85104 @contextmanager
86105 def _temporary_file (self , text : str , suffix : str ) -> Generator [str , None , None ]:
@@ -98,17 +117,3 @@ def _temporary_file(self, text: str, suffix: str) -> Generator[str, None, None]:
98117 os .remove (tmp_path )
99118 except OSError :
100119 logger .debug ("Could not delete temporary file: %s" , tmp_path )
101-
102-
103- def _check_output (args : List [str ]) -> str :
104- try :
105- return subprocess .check_output (args , text = True )
106- except Exception as ex :
107- raise RuntimeError (f"Command failed: { args } " ) from ex
108-
109-
110- def _check_call (args : List [str ]) -> int :
111- try :
112- return subprocess .check_call (args )
113- except Exception as ex :
114- raise RuntimeError (f"Command failed: { args } " ) from ex
0 commit comments