How to use the snapcraft.internal.errors function in snapcraft

To help you get started, we’ve selected a few snapcraft examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github snapcore / snapcraft / tests / unit / test_os_release.py View on Github external
def test_no_name(self):
        release = os_release.OsRelease(
            os_release_file=self._write_os_release(
                dedent(
                    """\
                ID=arch
                PRETTY_NAME="Arch Linux"
                ID_LIKE=archlinux
                VERSION_ID="foo"
                VERSION_CODENAME="bar"
            """
                )
            )
        )

        self.assertRaises(errors.OsReleaseNameError, release.name)
github snapcore / snapcraft / tests / unit / lifecycle / test_lifecycle.py View on Github external
def test_prime_with_invalid_image_info_raises_exception(self):
        self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO", "1"))
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_IMAGE_INFO", "not-json")
        )
        project_config = self.make_snapcraft_project(
            textwrap.dedent(
                """\
                parts:
                  test-part:
                    plugin: nil
                """
            )
        )
        raised = self.assertRaises(
            errors.InvalidContainerImageInfoError,
            lifecycle.execute,
            steps.PRIME,
            project_config,
        )
        self.assertThat(raised.image_info, Equals("not-json"))
github snapcore / snapcraft / tests / unit / plugins / test_make.py View on Github external
def test_unsupported_base(self):
        project = snapcraft.project.Project(
            snapcraft_yaml_file_path=self.make_snapcraft_yaml(
                textwrap.dedent(
                    """\
                    name: make-snap
                    base: unsupported-base
                    """
                )
            )
        )

        raised = self.assertRaises(
            errors.PluginBaseError, make.MakePlugin, "test-part", self.options, project
        )

        self.assertThat(raised.part_name, Equals("test-part"))
        self.assertThat(raised.base, Equals("unsupported-base"))
github snapcore / snapcraft / tests / unit / plugins / test_meson.py View on Github external
def test_unsupported_base_raises(self):
        self.assertRaises(
            errors.PluginBaseError,
            meson.MesonPlugin,
            "test-part",
            self.options,
            self.project,
        )
github snapcore / snapcraft / tests / unit / pluginhandler / test_plugin_loader.py View on Github external
def test_known_module_but_unknown_plugin_must_raise_exception(self):
        fake_logger = fixtures.FakeLogger(level=logging.ERROR)
        self.useFixture(fake_logger)

        path = copy.copy(sys.path)

        # "_ros" is a valid module within the plugin path, but contains no
        # plugins.
        raised = self.assertRaises(
            errors.PluginError, self.load_part, "fake-part", "_ros"
        )

        self.assertThat(raised.message, Equals("no plugin found in module '_ros'"))

        # Make sure that nothing was added to sys.path.
        self.assertThat(path, Equals(sys.path))
github snapcore / snapcraft / snapcraft / internal / dirs.py View on Github external
if os.path.exists(data_dir):
            return data_dir

    # Handle Options (b) & (c).
    if topdir.startswith(sys.prefix):
        data_dir = os.path.join(sys.prefix, "share", "snapcraft")
        if os.path.exists(data_dir):
            return data_dir

    # Do our best to find something...
    for data_dir in [topdir, sys.prefix, site.USER_BASE]:
        data_dir = os.path.join(data_dir, "share", "snapcraft")
        if os.path.exists(data_dir):
            return data_dir

    raise snapcraft.internal.errors.SnapcraftDataDirectoryMissingError()
github snapcore / snapcraft / snapcraft / internal / project_loader / grammar / errors.py View on Github external
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .

from snapcraft.internal import errors


class GrammarError(errors.SnapcraftError):
    """Base class for grammar-related errors."""

    pass


class GrammarSyntaxError(GrammarError):

    fmt = "Invalid grammar syntax: {message}"

    def __init__(self, message):
        super().__init__(message=message)


class OnStatementSyntaxError(GrammarSyntaxError):
    def __init__(self, on_statement, *, message=None):
        components = ["{!r} is not a valid 'on' clause".format(on_statement)]
github snapcore / snapcraft / snapcraft / cli / lifecycle.py View on Github external
Examples:
        snapcraft clean
        snapcraft clean my-part
    """
    # This option is only valid in legacy.
    if step:
        option = "--step" if "--step" in ctx.obj["argv"] else "-s"
        raise click.BadOptionUsage(option, "no such option: {}".format(option))

    build_provider = get_build_provider(**kwargs)
    build_provider_flags = get_build_provider_flags(build_provider, **kwargs)
    is_managed_host = build_provider == "managed-host"

    try:
        project = get_project(is_managed_host=is_managed_host)
    except errors.ProjectNotFoundError:
        # Fresh environment, nothing to clean.
        return

    if unprime and not is_managed_host:
        raise click.BadOptionUsage("--unprime", "no such option: --unprime")

    if build_provider in ["host", "managed-host"]:
        apply_host_provider_flags(build_provider_flags)
        step = steps.PRIME if unprime else None
        lifecycle.clean(project, parts, step)
    else:
        build_provider_class = build_providers.get_provider_for(build_provider)
        if parts:
            with build_provider_class(
                project=project, echoer=echo, build_provider_flags=build_provider_flags
            ) as instance:
github snapcore / snapcraft / snapcraft / internal / sources / errors.py View on Github external
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .

from snapcraft import formatting_utils
from snapcraft.internal import errors

from typing import List


class SnapcraftSourceError(errors.SnapcraftError):
    pass


class VCSError(SnapcraftSourceError):
    fmt = "{message}"


class SnapcraftSourceNotFoundError(SnapcraftSourceError):

    fmt = (
        "Failed to pull source: {source!r}.\n"
        "Please ensure the source path is correct and that it is accessible.\n"
        "See `snapcraft help sources` for more information."
    )

    def __init__(self, source):
github snapcore / snapcraft / snapcraft / internal / pluginhandler / _plugin_loader.py View on Github external
def _load_module(module_name, plugin_name, local_plugins_dir):
    module = None
    with contextlib.suppress(ImportError):
        module = _load_local("x-{}".format(plugin_name), local_plugins_dir)
        logger.info("Loaded local plugin for %s", plugin_name)

    if not module:
        with contextlib.suppress(ImportError):
            module = importlib.import_module("snapcraft.plugins.{}".format(module_name))

    if not module:
        logger.info("Searching for local plugin for %s", plugin_name)
        with contextlib.suppress(ImportError):
            module = _load_local(module_name, local_plugins_dir)
        if not module:
            raise errors.PluginError("unknown plugin: {!r}".format(plugin_name))

    return module