Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions imagecraft/plugins/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This file is part of imagecraft.
#
# Copyright 2026 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 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 warranties of MERCHANTABILITY,
# SATISFACTORY QUALITY, 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 <http://www.gnu.org/licenses/>.

"""Shared plugin utilities."""


def resolve_snap(snap: str) -> str:
"""Resolve a snap reference to the format expected by snap prepare-image.

If the snap reference contains a channel (e.g. ``name/track/risk``), it is
converted to the ``name=track/risk`` form that ``snap prepare-image``
expects. Plain snap names and local ``.snap`` file paths are returned
unchanged.
"""
snap = snap.strip()
if "/" in snap and not snap.endswith(".snap"):
name, channel = snap.split("/", 1)
snap = f"{name}={channel}"
return snap
11 changes: 3 additions & 8 deletions imagecraft/plugins/snap_preseed_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from craft_parts.plugins import Plugin, PluginProperties
from typing_extensions import override

from ._utils import resolve_snap


class SnapPreseedPluginProperties(PluginProperties, frozen=True):
"""Properties for the 'snap-preseed' plugin."""
Expand Down Expand Up @@ -77,17 +79,10 @@ def get_build_commands(self) -> list[str]:
)

cmd.extend(
f"--snap={self._resolve_snap(snap)}" for snap in options.snap_preseed_snaps
f"--snap={resolve_snap(snap)}" for snap in options.snap_preseed_snaps
)

cmd.append(
f'"{options.snap_preseed_model_assert}" {self._part_info.part_install_dir}'
)
return [" ".join(cmd)]

def _resolve_snap(self, snap: str) -> str:
snap = snap.strip()
if "/" in snap and not snap.endswith(".snap"):
name, channel = snap.split("/", 1)
snap = f"{name}={channel}"
return snap
11 changes: 3 additions & 8 deletions imagecraft/plugins/uc_prepare_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from pydantic import model_validator
from typing_extensions import Self, override

from ._utils import resolve_snap


class UcPreparePluginProperties(PluginProperties, frozen=True):
"""Properties for the uc-prepare plugin."""
Expand Down Expand Up @@ -118,18 +120,11 @@ def get_build_commands(self) -> list[str]:
)

cmd.extend(
f"--snap={self._resolve_snap(snap)}" for snap in options.uc_prepare_snaps
f"--snap={resolve_snap(snap)}" for snap in options.uc_prepare_snaps
)

cmd.append(
f"{options.uc_prepare_model_assert} {self._part_info.part_install_dir}"
)

return [" ".join(cmd)]

def _resolve_snap(self, snap: str) -> str:
snap = snap.strip()
if "/" in snap and not snap.endswith(".snap"):
name, channel = snap.split("/", 1)
snap = f"{name}={channel}"
return snap
38 changes: 38 additions & 0 deletions tests/unit/plugins/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This file is part of imagecraft.
#
# Copyright 2026 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
# 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 <http://www.gnu.org/licenses/>.

import pytest
from imagecraft.plugins._utils import resolve_snap


@pytest.mark.parametrize(
("snap", "expected"),
[
# Plain snap name – unchanged
("core24", "core24"),
# Leading/trailing whitespace is stripped
(" core24 ", "core24"),
# Local .snap file path with a slash – unchanged (not a channel)
("./my-snap_1.0_amd64.snap", "./my-snap_1.0_amd64.snap"),
# snap name with channel – converted to name=channel
("hello-world/latest/stable", "hello-world=latest/stable"),
("core24/stable", "core24=stable"),
# snap name with channel and leading whitespace
(" hello-world/latest/stable ", "hello-world=latest/stable"),
],
)
def test_resolve_snap(snap, expected):
assert resolve_snap(snap) == expected