aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/tools/metadata/common.py
blob: c17a738966ccb023026cc56483c4a5fbea3410bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Module for installation process."""
import logging
from abc import ABC
from abc import abstractmethod
from dataclasses import dataclass
from pathlib import Path
from typing import Callable
from typing import List
from typing import Optional
from typing import Union

from mlia.utils.misc import yes


logger = logging.getLogger(__name__)


@dataclass
class InstallFromPath:
    """Installation from the local path."""

    backend_path: Path


@dataclass
class DownloadAndInstall:
    """Download and install."""

    eula_agreement: bool = True


InstallationType = Union[InstallFromPath, DownloadAndInstall]


class Installation(ABC):
    """Base class for the installation process of the backends."""

    @property
    @abstractmethod
    def name(self) -> str:
        """Return name of the backend."""

    @property
    @abstractmethod
    def description(self) -> str:
        """Return description of the backend."""

    @property
    @abstractmethod
    def could_be_installed(self) -> bool:
        """Return true if backend could be installed in current environment."""

    @property
    @abstractmethod
    def already_installed(self) -> bool:
        """Return true if backend is already installed."""

    @abstractmethod
    def supports(self, install_type: InstallationType) -> bool:
        """Return true if installation supports requested installation type."""

    @abstractmethod
    def install(self, install_type: InstallationType) -> None:
        """Install the backend."""


InstallationFilter = Callable[[Installation], bool]


class AlreadyInstalledFilter:
    """Filter for already installed backends."""

    def __call__(self, installation: Installation) -> bool:
        """Installation filter."""
        return installation.already_installed


class ReadyForInstallationFilter:
    """Filter for ready to be installed backends."""

    def __call__(self, installation: Installation) -> bool:
        """Installation filter."""
        return installation.could_be_installed and not installation.already_installed


class SupportsInstallTypeFilter:
    """Filter backends that support certain type of the installation."""

    def __init__(self, installation_type: InstallationType) -> None:
        """Init filter."""
        self.installation_type = installation_type

    def __call__(self, installation: Installation) -> bool:
        """Installation filter."""
        return installation.supports(self.installation_type)


class SearchByNameFilter:
    """Filter installation by name."""

    def __init__(self, backend_name: Optional[str]) -> None:
        """Init filter."""
        self.backend_name = backend_name

    def __call__(self, installation: Installation) -> bool:
        """Installation filter."""
        return not self.backend_name or installation.name == self.backend_name


class InstallationManager(ABC):
    """Helper class for managing installations."""

    @abstractmethod
    def install_from(self, backend_path: Path, backend_name: Optional[str]) -> None:
        """Install backend from the local directory."""

    @abstractmethod
    def download_and_install(
        self, backend_name: Optional[str], eula_agreement: bool
    ) -> None:
        """Download and install backends."""

    @abstractmethod
    def show_env_details(self) -> None:
        """Show environment details."""

    @abstractmethod
    def backend_installed(self, backend_name: str) -> bool:
        """Return true if requested backend installed."""


class InstallationFiltersMixin:
    """Mixin for filtering installation based on different conditions."""

    installations: List[Installation]

    def filter_by(self, *filters: InstallationFilter) -> List[Installation]:
        """Filter installations."""
        return [
            installation
            for installation in self.installations
            if all(filter_(installation) for filter_ in filters)
        ]

    def could_be_installed_from(
        self, backend_path: Path, backend_name: Optional[str]
    ) -> List[Installation]:
        """Return installations that could be installed from provided directory."""
        return self.filter_by(
            SupportsInstallTypeFilter(InstallFromPath(backend_path)),
            SearchByNameFilter(backend_name),
        )

    def could_be_downloaded_and_installed(
        self, backend_name: Optional[str] = None
    ) -> List[Installation]:
        """Return installations that could be downloaded and installed."""
        return self.filter_by(
            SupportsInstallTypeFilter(DownloadAndInstall()),
            SearchByNameFilter(backend_name),
            ReadyForInstallationFilter(),
        )

    def already_installed(
        self, backend_name: Optional[str] = None
    ) -> List[Installation]:
        """Return list of backends that are already installed."""
        return self.filter_by(
            AlreadyInstalledFilter(), SearchByNameFilter(backend_name)
        )

    def ready_for_installation(self) -> List[Installation]:
        """Return list of the backends that could be installed."""
        return self.filter_by(ReadyForInstallationFilter())


class DefaultInstallationManager(InstallationManager, InstallationFiltersMixin):
    """Interactive installation manager."""

    def __init__(
        self, installations: List[Installation], noninteractive: bool = False
    ) -> None:
        """Init the manager."""
        self.installations = installations
        self.noninteractive = noninteractive

    def choose_installation_for_path(
        self, backend_path: Path, backend_name: Optional[str]
    ) -> Optional[Installation]:
        """Check available installation and select one if possible."""
        installs = self.could_be_installed_from(backend_path, backend_name)

        if not installs:
            logger.info(
                "Unfortunatelly, it was not possible to automatically "
                "detect type of the installed FVP. "
                "Please, check provided path to the installed FVP."
            )
            return None

        if len(installs) != 1:
            names = ",".join((install.name for install in installs))
            logger.info(
                "Unable to correctly detect type of the installed FVP."
                "The following FVPs are detected %s. Installation skipped.",
                names,
            )
            return None

        installation = installs[0]
        if installation.already_installed:
            logger.info(
                "%s was found in %s, but it has been already installed.",
                installation.name,
                backend_path,
            )
            return None

        return installation

    def install_from(self, backend_path: Path, backend_name: Optional[str]) -> None:
        """Install from the provided directory."""
        installation = self.choose_installation_for_path(backend_path, backend_name)

        if not installation:
            return

        prompt = (
            f"{installation.name} was found in {backend_path}. "
            "Would you like to install it?"
        )
        self._install(installation, InstallFromPath(backend_path), prompt)

    def download_and_install(
        self, backend_name: Optional[str] = None, eula_agreement: bool = True
    ) -> None:
        """Download and install available backends."""
        installations = self.could_be_downloaded_and_installed(backend_name)

        if not installations:
            logger.info("No backends available for the installation.")
            return

        names = ",".join((installation.name for installation in installations))
        logger.info("Following backends are available for downloading: %s", names)

        for installation in installations:
            prompt = f"Would you like to download and install {installation.name}?"
            self._install(
                installation, DownloadAndInstall(eula_agreement=eula_agreement), prompt
            )

    def show_env_details(self) -> None:
        """Print current state of the execution environment."""
        if installed := self.already_installed():
            logger.info("Installed backends:\n")

            for installation in installed:
                logger.info("  - %s", installation.name)

        if could_be_installed := self.ready_for_installation():
            logger.info("Following backends could be installed:")

            for installation in could_be_installed:
                logger.info("  - %s", installation.name)

        if not installed and not could_be_installed:
            logger.info("No backends installed")

    def _install(
        self,
        installation: Installation,
        installation_type: InstallationType,
        prompt: str,
    ) -> None:
        proceed = self.noninteractive or yes(prompt)

        if proceed:
            installation.install(installation_type)
            logger.info("%s successfully installed.", installation.name)
        else:
            logger.info("%s installation canceled.", installation.name)

    def backend_installed(self, backend_name: str) -> bool:
        """Return true if requested backend installed."""
        installations = self.already_installed(backend_name)

        return len(installations) == 1