How to use the snapcraft.internal.sources 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 / sources / test_tar.py View on Github external
def test_pull_tarball_must_download_to_sourcedir(self, mock_prov):
        plugin_name = "test_plugin"
        dest_dir = os.path.join("parts", plugin_name, "src")
        os.makedirs(dest_dir)
        tar_file_name = "test.tar"
        source = "http://{}:{}/{file_name}".format(
            *self.server.server_address, file_name=tar_file_name
        )
        tar_source = sources.Tar(source, dest_dir)

        tar_source.pull()

        source_file = os.path.join(dest_dir, tar_file_name)
        mock_prov.assert_called_once_with(dest_dir, src=source_file, clean_target=False)
        with open(os.path.join(dest_dir, tar_file_name), "r") as tar_file:
            self.assertThat(tar_file.read(), Equals("Test fake file"))
github snapcore / snapcraft / tests / unit / sources / test_local.py View on Github external
def test_pull_with_existing_source_file_error(self):
        os.makedirs(os.path.join("src", "dir"))
        open(os.path.join("src", "dir", "file"), "w").close()

        # Note that this is a file now instead of a directory
        open("destination", "w").close()

        local = sources.Local("src", "destination")
        self.assertRaises(errors.SnapcraftEnvironmentError, local.pull)
github snapcore / snapcraft / tests / unit / sources / test_bazaar.py View on Github external
def test_init_with_source_depth_raises_exception(self):
        raised = self.assertRaises(
            sources.errors.SnapcraftSourceInvalidOptionError,
            sources.Bazaar,
            "lp://mysource",
            "source_dir",
            source_depth=2,
        )

        self.assertThat(raised.source_type, Equals("bzr"))
        self.assertThat(raised.option, Equals("source-depth"))
github snapcore / snapcraft / tests / unit / sources / test_local.py View on Github external
def test_pull_does_not_change_snapcraft_files_list(self, mock_glob):
        # Regression test for https://bugs.launchpad.net/snapcraft/+bug/1614913
        # Verify that SNAPCRAFT_FILES was not modified by the pull when there
        # are files to ignore.
        snapcraft_files_before_pull = copy.copy(common.SNAPCRAFT_FILES)
        mock_glob.return_value = ["a.snap", "b.snap", "c.snap"]

        local = sources.Local(".", "destination")
        local.pull()

        self.assertThat(snapcraft_files_before_pull, Equals(common.SNAPCRAFT_FILES))
github snapcore / snapcraft / tests / unit / sources / test_sources.py View on Github external
def test_get_source_with_branch_must_raise_error(self):
        handler = sources.get_source_handler(
            "https://source.com", source_type=self.source_type
        )
        raised = self.assertRaises(
            sources.errors.SnapcraftSourceInvalidOptionError,
            handler,
            "https://source.com",
            source_dir=".",
            source_branch=self.source_branch,
            source_tag=self.source_tag,
            source_commit=self.source_commit,
        )

        self.assertThat(raised.source_type, Equals(self.source_type))
        self.assertThat(raised.option, Equals(self.error))
github snapcore / snapcraft / tests / unit / sources / test_local.py View on Github external
source = "source"
        destination = "destination"
        os.mkdir(source)
        os.mkdir(destination)

        with open(os.path.join(source, "file"), "w") as f:
            f.write("1")

        # Now make a reference file with a timestamp later than the file was
        # created. We'll ensure this by setting it ourselves
        shutil.copy2(os.path.join(source, "file"), "reference")
        access_time = os.stat("reference").st_atime
        modify_time = os.stat("reference").st_mtime
        os.utime("reference", (access_time, modify_time + 1))

        local = sources.Local(source, destination)
        local.pull()
        self.assertFalse(
            local.check("reference"), "Expected no updates to be available"
        )
        self.assertThat(os.path.join(destination, "file"), FileContains("1"))

        # Now update the file in source, and make sure it has a timestamp
        # later than our reference (this whole test happens too fast)
        with open(os.path.join(source, "file"), "w") as f:
            f.write("2")

        access_time = os.stat("reference").st_atime
        modify_time = os.stat("reference").st_mtime
        os.utime(os.path.join(source, "file"), (access_time, modify_time + 1))

        self.assertTrue(local.check("reference"), "Expected update to be available")
github snapcore / snapcraft / snapcraft / cli / help.py View on Github external
# 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 .
import importlib
import sys
from textwrap import dedent

import click

import snapcraft
from . import echo
from snapcraft.internal import sources


_TOPICS = {"sources": sources, "plugins": snapcraft}


@click.group()
def helpcli():
    """Help commands"""
    pass


@helpcli.command("help")
@click.argument("topic", metavar="", required=False)
@click.option(
    "--devel", is_flag=True, help="Show more details for snapcraft developers"
)
@click.pass_context
def help_command(ctx, topic, devel):
    """Obtain help for a certain topic, plugin or command.
github snapcore / snapcraft / snapcraft / internal / parser.py View on Github external
origin_tag = data.get("origin-tag")

    # Get required wiki entry fields.
    try:
        entry_parts = data["parts"]
        origin = data["origin"]
        maintainer = data["maintainer"]
        description = data["description"]
    except KeyError as e:
        raise errors.InvalidWikiEntryError("Missing key in wiki entry: {}".format(e))

    logger.info("Processing origin {origin!r}".format(origin=origin))
    origin_dir = os.path.join(_get_base_dir(), _encode_origin(origin))
    os.makedirs(origin_dir, exist_ok=True)

    source_handler = sources.get_source_handler(origin, source_type=origin_type)
    handler = source_handler(origin, source_dir=origin_dir)
    repo.check_for_command(handler.command)
    handler.source_branch = origin_branch
    handler.source_commit = origin_commit
    handler.source_tag = origin_tag
    handler.pull()

    try:
        origin_data = _get_origin_data(origin_dir)
    except project.errors.MissingSnapcraftYamlError as e:
        raise errors.InvalidWikiEntryError(
            "Origin {origin!r} is missing a snapcraft.yaml file.".format(origin=origin)
        ) from e
    except errors.SnapcraftEnvironmentError as e:
        raise errors.InvalidWikiEntryError("snapcraft.yaml error: {}".format(e)) from e
github 3v1n0 / telegram-snap / snap / plugins / x-autotools-subsource.py View on Github external
def pull(self):
        super().pull()

        for src in self.options.sub_sources:
            [name] = src.keys()
            [values] = src.values()

            if 'source' in values:
                dest = values['dest'] if 'dest' in values else ''
                sources.get(os.path.join(self.sourcedir, dest),
                    os.path.join(self.build_basedir, dest),
                    Dict2Object(values))
github snapcore / snapcraft / snapcraft / internal / pluginhandler / __init__.py View on Github external
def _get_source_handler(self, properties):
        """Returns a source_handler for the source in properties."""
        # TODO: we cannot pop source as it is used by plugins. We also make
        # the default '.'
        source_handler = None
        if self._source:
            handler_class = sources.get_source_handler(
                self._source, source_type=properties["source-type"]
            )
            source_handler = handler_class(
                self._source,
                self.plugin.sourcedir,
                source_checksum=properties["source-checksum"],
                source_branch=properties["source-branch"],
                source_tag=properties["source-tag"],
                source_depth=properties["source-depth"],
                source_commit=properties["source-commit"],
            )

        return source_handler