Skip to content

Commit

Permalink
refactor: use craft-platforms for build plans (#1894)
Browse files Browse the repository at this point in the history
Only applies to `platforms` charms.
  • Loading branch information
lengau authored Sep 13, 2024
1 parent 392f859 commit f1755e8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
56 changes: 24 additions & 32 deletions charmcraft/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
cast,
)

import craft_platforms
import pydantic
import pydantic.v1
from craft_application import errors, models, util
from craft_application.util import safe_yaml_load
from craft_cli import CraftError, emit
from craft_platforms import charm
from craft_providers import bases
from pydantic import dataclasses
from typing_extensions import Self
Expand Down Expand Up @@ -345,38 +347,28 @@ def get_build_plan(self) -> list[models.BuildInfo]:

if self.platforms is None:
raise CraftError("Must define at least one platform.")
build_infos = []
for platform_name, platform in self.platforms.items():
if platform is None:
if platform_name not in const.SUPPORTED_ARCHITECTURES:
raise CraftError(
f"Invalid platform {platform_name}.",
details="A platform name must either be a valid architecture name or the "
"platform must specify one or more build-on and build-for architectures.",
)
build_infos.append(
models.BuildInfo(
platform_name,
build_on=platform_name,
build_for=platform_name,
base=base,
)
)
else:
# TODO: this should go to craft-platforms, so silence mypy for now.
for build_on in platform.build_on: # type: ignore[union-attr]
build_infos.extend(
[
models.BuildInfo(
platform_name,
build_on=str(build_on),
build_for=str(build_for),
base=base,
)
for build_for in platform.build_for # type: ignore[union-attr]
]
)
return build_infos
platforms = cast(
# https://github.com/canonical/craft-platforms/issues/43
craft_platforms.Platforms, # pyright: ignore[reportPrivateImportUsage]
{
name: (platform.marshal() if platform else None)
for name, platform in self.platforms.items()
},
)
build_infos = charm.get_platforms_charm_build_plan(
base=self.base,
build_base=self.build_base,
platforms=platforms,
)
return [
models.BuildInfo(
platform=info.platform,
build_on=str(info.build_on),
build_for=str(info.build_for),
base=base,
)
for info in build_infos
]


class CharmcraftProject(models.Project, metaclass=abc.ABCMeta):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies = [
"craft-grammar>=2.0.0",
"craft-parts>=2.0.0",
"craft-providers>=2.0.0",
"craft-platforms~=0.1",
"craft-platforms~=0.3",
"craft-providers>=2.0.0",
"craft-store>=3.0.0",
"distro>=1.3.0",
Expand Down

0 comments on commit f1755e8

Please sign in to comment.