"Fossies" - the Fresh Open Source Software Archive

Member "snapcraft-3.8/snapcraft/internal/build_providers/errors.py" (9 Sep 2019, 9376 Bytes) of package /linux/misc/snapcraft-3.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "errors.py": 3.7.2_vs_3.8.

    1 # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
    2 #
    3 # Copyright (C) 2018-2019 Canonical Ltd
    4 #
    5 # This program is free software: you can redistribute it and/or modify
    6 # it under the terms of the GNU General Public License version 3 as
    7 # published by the Free Software Foundation.
    8 #
    9 # This program is distributed in the hope that it will be useful,
   10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 # GNU General Public License for more details.
   13 #
   14 # You should have received a copy of the GNU General Public License
   15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   16 
   17 import shlex
   18 from typing import Any, Dict, Optional
   19 from typing import Sequence  # noqa: F401
   20 
   21 from snapcraft.internal.errors import SnapcraftError as _SnapcraftError
   22 
   23 
   24 class ProviderBaseError(_SnapcraftError):
   25     """Base Exception for all provider related exceptions."""
   26 
   27 
   28 class ProviderNotSupportedError(ProviderBaseError):
   29 
   30     fmt = (
   31         "The {provider!r} provider is not supported, please choose a "
   32         "different one and try again."
   33     )
   34 
   35     def __init__(self, *, provider: str) -> None:
   36         super().__init__(provider=provider)
   37 
   38 
   39 class ProviderNotFound(ProviderBaseError):
   40 
   41     fmt = "You need {provider!r} set-up to build snaps: {error_message}."
   42 
   43     def __init__(
   44         self, *, provider: str, prompt_installable: bool, error_message: str
   45     ) -> None:
   46         super().__init__(
   47             provider=provider,
   48             prompt_installable=prompt_installable,
   49             error_message=error_message,
   50         )
   51         self.prompt_installable = prompt_installable
   52         self.provider = provider
   53 
   54 
   55 class _GenericProviderError(ProviderBaseError):
   56 
   57     _FMT_ERROR_MESSAGE_AND_EXIT_CODE = (
   58         "An error occurred with the instance when trying to {action} with "
   59         "{provider_name!r}: returned exit code {exit_code!r}: {error_message}.\n"
   60         "Ensure that {provider_name!r} is setup correctly and try again."
   61     )
   62 
   63     _FMT_ERROR_MESSAGE = (
   64         "An error occurred with the instance when trying to {action} with "
   65         "{provider_name!r}: {error_message}.\n"
   66         "Ensure that {provider_name!r} is setup correctly and try again."
   67     )
   68 
   69     _FMT_EXIT_CODE = (
   70         "An error occurred with the instance when trying to {action} with "
   71         "{provider_name!r}: returned exit code {exit_code!r}.\n"
   72         "Ensure that {provider_name!r} is setup correctly and try again."
   73     )
   74 
   75     def __init__(
   76         self,
   77         *,
   78         provider_name: str,
   79         action: str,
   80         error_message: Optional[str] = None,
   81         exit_code: Optional[int] = None
   82     ) -> None:
   83         if exit_code is not None and error_message is not None:
   84             fmt = self._FMT_ERROR_MESSAGE_AND_EXIT_CODE
   85         elif error_message:
   86             fmt = self._FMT_ERROR_MESSAGE
   87         elif exit_code:
   88             fmt = self._FMT_EXIT_CODE
   89         else:
   90             raise RuntimeError("error_message nor exit_code are set")
   91 
   92         self.fmt = fmt
   93 
   94         super().__init__(
   95             provider_name=provider_name,
   96             action=action,
   97             error_message=error_message,
   98             exit_code=exit_code,
   99         )
  100 
  101 
  102 class ProviderCommunicationError(ProviderBaseError):
  103 
  104     fmt = (
  105         "An error occurred when trying to communicate with the "
  106         "{provider_name!r} provider: {message}."
  107     )
  108 
  109     def __init__(self, *, provider_name: str, message: str) -> None:
  110         super().__init__(provider_name=provider_name, message=message)
  111 
  112 
  113 class ProviderLaunchError(_GenericProviderError):
  114     def __init__(
  115         self,
  116         *,
  117         provider_name: str,
  118         error_message: Optional[str] = None,
  119         exit_code: Optional[int] = None
  120     ) -> None:
  121         super().__init__(
  122             action="launch",
  123             provider_name=provider_name,
  124             error_message=error_message,
  125             exit_code=exit_code,
  126         )
  127 
  128 
  129 class ProviderStartError(_GenericProviderError):
  130     def __init__(
  131         self,
  132         *,
  133         provider_name: str,
  134         error_message: Optional[str] = None,
  135         exit_code: Optional[int] = None
  136     ) -> None:
  137         super().__init__(
  138             action="start",
  139             provider_name=provider_name,
  140             error_message=error_message,
  141             exit_code=exit_code,
  142         )
  143 
  144 
  145 class ProviderStopError(_GenericProviderError):
  146     def __init__(
  147         self,
  148         *,
  149         provider_name: str,
  150         error_message: Optional[str] = None,
  151         exit_code: Optional[int] = None
  152     ) -> None:
  153         super().__init__(
  154             action="stop",
  155             provider_name=provider_name,
  156             error_message=error_message,
  157             exit_code=exit_code,
  158         )
  159 
  160 
  161 class ProviderDeleteError(_GenericProviderError):
  162     def __init__(
  163         self,
  164         *,
  165         provider_name: str,
  166         error_message: Optional[str] = None,
  167         exit_code: Optional[int] = None
  168     ) -> None:
  169         super().__init__(
  170             action="delete",
  171             provider_name=provider_name,
  172             error_message=error_message,
  173             exit_code=exit_code,
  174         )
  175 
  176 
  177 class ProviderExecError(ProviderBaseError):
  178 
  179     fmt = (
  180         "An error occurred when trying to execute {command_string!r} with "
  181         "{provider_name!r}: returned exit code {exit_code!r}."
  182     )
  183 
  184     def __init__(
  185         self, *, provider_name: str, command: Sequence[str], exit_code: int
  186     ) -> None:
  187         command_string = " ".join(shlex.quote(i) for i in command)
  188         super().__init__(
  189             provider_name=provider_name,
  190             command=command,
  191             command_string=command_string,
  192             exit_code=exit_code,
  193         )
  194 
  195 
  196 class ProviderShellError(_GenericProviderError):
  197     def __init__(
  198         self,
  199         *,
  200         provider_name: str,
  201         error_message: Optional[str] = None,
  202         exit_code: Optional[int] = None
  203     ) -> None:
  204         super().__init__(
  205             action="shell",
  206             provider_name=provider_name,
  207             error_message=error_message,
  208             exit_code=exit_code,
  209         )
  210 
  211 
  212 class ProviderMountError(_GenericProviderError):
  213     def __init__(
  214         self,
  215         *,
  216         provider_name: str,
  217         error_message: Optional[str] = None,
  218         exit_code: Optional[int] = None
  219     ) -> None:
  220         super().__init__(
  221             action="mount",
  222             provider_name=provider_name,
  223             error_message=error_message,
  224             exit_code=exit_code,
  225         )
  226 
  227 
  228 class ProviderUnMountError(_GenericProviderError):
  229     def __init__(
  230         self,
  231         *,
  232         provider_name: str,
  233         error_message: Optional[str] = None,
  234         exit_code: Optional[int] = None
  235     ) -> None:
  236         super().__init__(
  237             action="unmount",
  238             provider_name=provider_name,
  239             error_message=error_message,
  240             exit_code=exit_code,
  241         )
  242 
  243 
  244 class ProviderFileCopyError(_GenericProviderError):
  245     def __init__(
  246         self,
  247         *,
  248         provider_name: str,
  249         error_message: Optional[str] = None,
  250         exit_code: Optional[int] = None
  251     ) -> None:
  252         super().__init__(
  253             action="copy files",
  254             provider_name=provider_name,
  255             error_message=error_message,
  256             exit_code=exit_code,
  257         )
  258 
  259 
  260 class ProviderInfoError(ProviderBaseError):
  261 
  262     fmt = (
  263         "An error occurred when using {provider_name!r} to "
  264         "query the status of the instance: returned exit code {exit_code!r}: {stderr!s}."
  265     )
  266 
  267     def __init__(self, *, provider_name: str, exit_code: int, stderr: bytes) -> None:
  268         super().__init__(
  269             provider_name=provider_name, exit_code=exit_code, stderr=stderr.decode()
  270         )
  271 
  272 
  273 class ProviderInstanceNotFoundError(ProviderBaseError):
  274 
  275     fmt = "Cannot find an instance named {instance_name!r}."
  276 
  277     def __init__(self, *, instance_name: str) -> None:
  278         super().__init__(instance_name=instance_name)
  279 
  280 
  281 class ProviderInfoDataKeyError(ProviderBaseError):
  282 
  283     fmt = (
  284         "The data returned by {provider_name!r} was not expected. "
  285         "It is missing a required key {missing_key!r} in {data!r}."
  286     )
  287 
  288     def __init__(
  289         self, *, provider_name: str, missing_key: str, data: Dict[str, Any]
  290     ) -> None:
  291         super().__init__(
  292             provider_name=provider_name, missing_key=missing_key, data=data
  293         )
  294 
  295 
  296 class ProviderBadDataError(ProviderBaseError):
  297 
  298     fmt = (
  299         "The data returned by {provider_name!r} was not expected "
  300         "or in the wrong format: {data!r}."
  301     )
  302 
  303     def __init__(self, *, provider_name: str, data: str) -> None:
  304         super().__init__(provider_name=provider_name, data=data)
  305 
  306 
  307 class ProviderMultipassDownloadFailed(ProviderBaseError):
  308     fmt = (
  309         "Failed to download Multipass: {message!r}\n"
  310         "Please install manually. You can find the latest release at:\n"
  311         "https://multipass.run"
  312     )
  313 
  314     def __init__(self, message):
  315         super().__init__(message=message)
  316 
  317 
  318 class ProviderMultipassInstallationFailed(ProviderBaseError):
  319     fmt = (
  320         "Failed to install Multipass: {message!r}\n"
  321         "Please install manually. You can find the latest release at:\n"
  322         "https://multipass.run"
  323     )
  324 
  325     def __init__(self, message):
  326         super().__init__(message=message)