Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(craft-application): support root packages for core24 #4617

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions snapcraft/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
from snapcraft.commands import unimplemented
from snapcraft.extensions import apply_extensions
from snapcraft.models import Architecture
from snapcraft.models.project import validate_architectures
from snapcraft.models.project import (
apply_root_packages_transform,
validate_architectures,
)
from snapcraft.providers import SNAPCRAFT_BASE_TO_PROVIDER_BASE
from snapcraft.utils import get_effective_base, get_host_architecture

Expand Down Expand Up @@ -170,7 +173,8 @@ def _extra_yaml_transform(
) -> dict[str, Any]:
arch = build_on
target_arch = build_for if build_for else get_host_architecture()
return apply_extensions(yaml_data, arch=arch, target_arch=target_arch)
new_yaml_data = apply_extensions(yaml_data, arch=arch, target_arch=target_arch)
return apply_root_packages_transform(new_yaml_data)
syu-w marked this conversation as resolved.
Show resolved Hide resolved

@override
def _get_dispatcher(self) -> craft_cli.Dispatcher:
Expand Down
27 changes: 27 additions & 0 deletions snapcraft/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

"""Project file definition and helpers."""

import copy
import re
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Tuple, Union, cast

Expand Down Expand Up @@ -171,6 +172,32 @@ def _validate_architectures_all_keyword(architectures):
)


def apply_root_packages_transform(yaml_data: dict[str, Any]) -> dict[str, Any]:
syu-w marked this conversation as resolved.
Show resolved Hide resolved
"""Support Root Packages in Snapcraft.

This allows the user to use "build-packages" and "build-snaps"
at the root level of the snapcraft.yaml file.
syu-w marked this conversation as resolved.
Show resolved Hide resolved
"""
if "build-packages" not in yaml_data and "build-snaps" not in yaml_data:
return yaml_data

yaml_data = copy.deepcopy(yaml_data)
yaml_data.setdefault("parts", {})
yaml_data["parts"]["snapcraft/core"] = {"plugin": "nil"}
sergiusens marked this conversation as resolved.
Show resolved Hide resolved

if "build-packages" in yaml_data:
yaml_data["parts"]["snapcraft/core"]["build-packages"] = yaml_data.pop(
"build-packages"
)

if "build-snaps" in yaml_data:
yaml_data["parts"]["snapcraft/core"]["build-snaps"] = yaml_data.pop(
"build-snaps"
)

return yaml_data


class Socket(models.CraftBaseModel):
"""Snapcraft app socket definition."""

Expand Down
30 changes: 30 additions & 0 deletions tests/unit/models/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Hook,
Project,
)
from snapcraft.models.project import apply_root_packages_transform
from snapcraft.utils import get_host_architecture


Expand Down Expand Up @@ -1782,3 +1783,32 @@ def test_project_get_build_for_arch_triplet_all(self, project_yaml_data):
arch_triplet = project.get_build_for_arch_triplet()

assert not arch_triplet


class TestProjectTransform:
syu-w marked this conversation as resolved.
Show resolved Hide resolved
"""Test Transform the Project."""

def test_root_packages_transform(self, project_yaml_data):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be useful to have a test to ensure projects without build-packages and build-snaps are unmodified (i.e. the if statement on line 181)

"""Test transforming the project."""
syu-w marked this conversation as resolved.
Show resolved Hide resolved
data = project_yaml_data()
data["build-packages"] = ["pkg1", "pkg2"]
data["build-snaps"] = ["snap3", "snap4"]

data_transformed = apply_root_packages_transform(data)

project = Project.unmarshal(data_transformed)

assert project.parts["snapcraft/core"]["build-packages"] == ["pkg1", "pkg2"]
assert project.parts["snapcraft/core"]["build-snaps"] == ["snap3", "snap4"]

def test_root_packages_transform_no_affect(self, project_yaml_data):
"""Test transforming the project without root packages."""
syu-w marked this conversation as resolved.
Show resolved Hide resolved
data = project_yaml_data()

data_transformed = apply_root_packages_transform(data)

project = Project.unmarshal(data_transformed)

assert project.build_packages is None
assert project.build_snaps is None
assert "snapcraft/core" not in project.parts
Loading