From 4a104934dba7aa2d3043c8256e8a9dbceb24cdb6 Mon Sep 17 00:00:00 2001 From: Callahan Kovacs Date: Mon, 11 Mar 2024 10:55:04 -0500 Subject: [PATCH 1/4] feat: get build env from command line, env, and snap config Signed-off-by: Callahan Kovacs --- snapcraft/application.py | 6 ++++-- snapcraft/commands/unimplemented.py | 11 +++++++++++ .../manifest/manifest-info-envvars/task.yaml | 3 +-- tests/spread/core24/remove-hook/task.yaml | 4 +--- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/snapcraft/application.py b/snapcraft/application.py index 5e420026fc..c471d0f599 100644 --- a/snapcraft/application.py +++ b/snapcraft/application.py @@ -82,7 +82,9 @@ def __init__(self, *args, **kwargs): os.environ[craft_var] = env_val @override - def _configure_services(self, platform: str | None, build_for: str | None) -> None: + def _configure_services( + self, platform: str | None, build_for: str | None, provider_name: str | None + ) -> None: self.services.set_kwargs( "package", platform=platform, @@ -90,7 +92,7 @@ def _configure_services(self, platform: str | None, build_for: str | None) -> No snapcraft_yaml_path=self._snapcraft_yaml_path, ) - super()._configure_services(platform, build_for) + super()._configure_services(platform, build_for, provider_name) @property def command_groups(self): diff --git a/snapcraft/commands/unimplemented.py b/snapcraft/commands/unimplemented.py index d14a68bd68..e783a211f5 100644 --- a/snapcraft/commands/unimplemented.py +++ b/snapcraft/commands/unimplemented.py @@ -51,6 +51,17 @@ def run_managed( """Overridden to always return False, for now.""" return False + def provider_name( + self, + parsed_args: argparse.Namespace, # noqa: ARG002 (the unused argument is for subclasses) + ) -> str | None: + """Name of the provider where the command should be run inside of. + + By default returns None. Subclasses can override this method to change this, + including by inspecting the arguments in `parsed_args`. + """ + return None + always_load_project: bool = False diff --git a/tests/spread/core24-suites/manifest/manifest-info-envvars/task.yaml b/tests/spread/core24-suites/manifest/manifest-info-envvars/task.yaml index d9a8e2c753..735e89f428 100644 --- a/tests/spread/core24-suites/manifest/manifest-info-envvars/task.yaml +++ b/tests/spread/core24-suites/manifest/manifest-info-envvars/task.yaml @@ -13,8 +13,7 @@ execute: | export SNAPCRAFT_IMAGE_INFO='{"test-var": "value"}' # If this next line fails, delete it and replace it with the one following it - snapcraft --use-lxd 2>&1 | MATCH 'unrecognized arguments: --use-lxd'; exit 0 - # snapcraft --use-lxd + snapcraft --use-lxd unsquashfs manifest_0.1_*.snap grep '^ test-var: value' squashfs-root/snap/manifest.yaml diff --git a/tests/spread/core24/remove-hook/task.yaml b/tests/spread/core24/remove-hook/task.yaml index 6503be8dda..59078fa7fc 100644 --- a/tests/spread/core24/remove-hook/task.yaml +++ b/tests/spread/core24/remove-hook/task.yaml @@ -14,9 +14,7 @@ execute: | unset SNAPCRAFT_BUILD_ENVIRONMENT # create a base instance by running snapcraft - # If this next line fails, delete it and replace it with the one following it - snapcraft pull --use-lxd --verbosity=trace 2>&1 | MATCH 'Error: unrecognized arguments: --use-lxd'; exit 0 - # snapcraft pull --use-lxd --verbosity=trace + snapcraft pull --use-lxd --verbosity=trace # verify base instance was created instances="$(lxc list --project=snapcraft --format=csv --columns="n")" From 17f7b8b962bc3aec6e2c947ae4088353302c22cb Mon Sep 17 00:00:00 2001 From: Alex Lowe Date: Fri, 8 Mar 2024 21:08:23 -0500 Subject: [PATCH 2/4] fix(application): update configure_services --- snapcraft/application.py | 7 ++----- snapcraft/services/package.py | 14 ++++++++------ tests/unit/conftest.py | 1 - tests/unit/services/test_package.py | 1 - 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/snapcraft/application.py b/snapcraft/application.py index c471d0f599..721deacade 100644 --- a/snapcraft/application.py +++ b/snapcraft/application.py @@ -82,17 +82,14 @@ def __init__(self, *args, **kwargs): os.environ[craft_var] = env_val @override - def _configure_services( - self, platform: str | None, build_for: str | None, provider_name: str | None - ) -> None: + def _configure_services(self, provider_name: str | None) -> None: self.services.set_kwargs( "package", - platform=platform, build_plan=self._build_plan, snapcraft_yaml_path=self._snapcraft_yaml_path, ) - super()._configure_services(platform, build_for, provider_name) + super()._configure_services(provider_name) @property def command_groups(self): diff --git a/snapcraft/services/package.py b/snapcraft/services/package.py index 779531272e..8c52604d19 100644 --- a/snapcraft/services/package.py +++ b/snapcraft/services/package.py @@ -1,6 +1,6 @@ # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- # -# Copyright 2023 Canonical Ltd. +# Copyright 2023-2024 Canonical Ltd. # # 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 @@ -50,13 +50,13 @@ def __init__( # noqa: PLR0913 (Too many arguments) *, project: models.Project, snapcraft_yaml_path: pathlib.Path, - platform: str | None, build_plan: list[BuildInfo], ) -> None: super().__init__(app, services, project=project) - self._platform = platform - self._build_for = build_plan[0].build_for self._snapcraft_yaml_path = snapcraft_yaml_path + self._build_plan = build_plan + self._platform = build_plan[0].platform + self._build_for = build_plan[0].build_for @override def pack(self, prime_dir: pathlib.Path, dest: pathlib.Path) -> list[pathlib.Path]: @@ -81,7 +81,7 @@ def pack(self, prime_dir: pathlib.Path, dest: pathlib.Path) -> list[pathlib.Path compression=self._project.compression, name=self._project.name, version=process_version(self._project.version), - target_arch=self._build_for, + target_arch=self._build_plan[0].build_for, ) ) ] @@ -137,7 +137,9 @@ def write_metadata(self, path: pathlib.Path) -> None: def metadata(self) -> snap_yaml.SnapMetadata: """Get the metadata model for this project.""" return snap_yaml.get_metadata_from_project( - self._project, self._services.lifecycle.prime_dir, arch=self._build_for + self._project, + self._services.lifecycle.prime_dir, + arch=self._build_plan[0].build_for, ) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 0fc5591760..1e6fe20e8d 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -475,7 +475,6 @@ def package_service( project=default_project, services=default_factory, snapcraft_yaml_path=file_path, - platform="amd64", build_plan=default_build_plan, ) diff --git a/tests/unit/services/test_package.py b/tests/unit/services/test_package.py index 85b4eb423c..52b56a68b0 100644 --- a/tests/unit/services/test_package.py +++ b/tests/unit/services/test_package.py @@ -59,7 +59,6 @@ def test_pack_target_arch( app=APP_METADATA, project=default_project, services=default_factory, - platform="amd64", build_plan=default_build_plan, snapcraft_yaml_path=tmp_path / "snapcraft.yaml", ) From b6bba0d2bef17b0a3a483e3e1a92a96e9a31acf1 Mon Sep 17 00:00:00 2001 From: Tiago Nobrega Date: Mon, 11 Mar 2024 16:29:36 -0300 Subject: [PATCH 3/4] fix(tests): create a suitable build plan Create a default build plan with a build info matching the host's architecture and base, so that individual plan-dependent tests don't have to worry about tweaking the plan. --- tests/unit/conftest.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 1e6fe20e8d..e31211b761 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -415,15 +415,20 @@ def default_factory(default_project): @pytest.fixture() def default_build_plan(): + from craft_application import util from craft_application.models import BuildInfo - from craft_providers import bases + + # Set the build info base to match the host's, so we can test in destructive + # mode with no issues. + arch = util.get_host_architecture() + base = util.get_host_base() return [ BuildInfo( platform="generic-x86-64", - build_on="amd64", - build_for="amd64", - base=bases.BaseName("ubuntu", "24.04"), + build_on=arch, + build_for=arch, + base=base, ) ] From fda349bb33b350dccaf4fd85caf17ede393dc76e Mon Sep 17 00:00:00 2001 From: Callahan Kovacs Date: Wed, 13 Mar 2024 08:28:55 -0500 Subject: [PATCH 4/4] build(deps): add craft-application to doc requirements Signed-off-by: Callahan Kovacs --- docs/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/requirements.txt b/docs/requirements.txt index 2e0e78bbdc..fadcfd5f9d 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,6 +1,7 @@ attrs==23.1.0 catkin-pkg==0.5.2 click==8.1.7 +craft-application @ git+https://github.com/canonical/craft-application@main craft-archives==1.1.3 craft-cli==2.5.1 craft-grammar==1.1.1