|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import tempfile |
| 5 | +from abc import abstractmethod |
| 6 | +from contextlib import contextmanager |
| 7 | +from typing import Generator |
| 8 | +from typing import List |
| 9 | +from typing import Optional |
| 10 | + |
| 11 | +from ...models.base import BaseRequirements |
| 12 | +from .commands import get_manager_command |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class BaseManager: |
| 18 | + """Defines the interface all package managers must implement. |
| 19 | +
|
| 20 | + If `MyManager` is an impementation of this interface then |
| 21 | + to get the Ewoks workflow requirements like this: |
| 22 | +
|
| 23 | + .. code-block:: python |
| 24 | +
|
| 25 | + manager = MyManager() |
| 26 | + requirements = manager.gather_requirements() |
| 27 | +
|
| 28 | + Ewoks workflow requirements can be installed like this: |
| 29 | +
|
| 30 | + .. code-block:: python |
| 31 | +
|
| 32 | + manager = MyManager() |
| 33 | + install_requirements.install_requirements(requirements) |
| 34 | + """ |
| 35 | + |
| 36 | + NAME = NotImplemented |
| 37 | + |
| 38 | + def __init__(self, *command: str) -> None: |
| 39 | + if not command: |
| 40 | + command = get_manager_command(self.NAME) |
| 41 | + self._cmd_args = command |
| 42 | + |
| 43 | + def gather_requirements(self) -> Optional[BaseRequirements]: |
| 44 | + """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: |
| 49 | + raise RuntimeError(f"{self.NAME!r} is not installed") |
| 50 | + |
| 51 | + try: |
| 52 | + return self._gather_requirements(manager_version) |
| 53 | + except Exception as ex: |
| 54 | + logger.error( |
| 55 | + "%s: failed to generate requirements (%s)", type(self).__name__, ex |
| 56 | + ) |
| 57 | + return None |
| 58 | + |
| 59 | + def install_requirements(self, requirements: BaseRequirements) -> None: |
| 60 | + """Install requirements into the current python environment.""" |
| 61 | + try: |
| 62 | + return self._install_requirements(requirements) |
| 63 | + except Exception as ex: |
| 64 | + logger.error( |
| 65 | + "%s: failed to install requirements (%s)", type(self).__name__, ex |
| 66 | + ) |
| 67 | + raise |
| 68 | + |
| 69 | + @abstractmethod |
| 70 | + def _gather_requirements(self, manager_version: str) -> BaseRequirements: |
| 71 | + pass |
| 72 | + |
| 73 | + @abstractmethod |
| 74 | + def _install_requirements(self, requirements: BaseRequirements) -> None: |
| 75 | + pass |
| 76 | + |
| 77 | + def _check_output(self, *args) -> str: |
| 78 | + return _check_output([*self._cmd_args, *args]) |
| 79 | + |
| 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]) |
| 84 | + |
| 85 | + @contextmanager |
| 86 | + def _temporary_file(self, text: str, suffix: str) -> Generator[str, None, None]: |
| 87 | + tmp_path = None |
| 88 | + try: |
| 89 | + with tempfile.NamedTemporaryFile("w", suffix=suffix, delete=False) as tmp: |
| 90 | + tmp.write(text) |
| 91 | + tmp_path = tmp.name |
| 92 | + |
| 93 | + yield tmp_path |
| 94 | + |
| 95 | + finally: |
| 96 | + if tmp_path: |
| 97 | + try: |
| 98 | + os.remove(tmp_path) |
| 99 | + except OSError: |
| 100 | + 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