From be0887ec7c5ecf477a98f64887e33049e5342b43 Mon Sep 17 00:00:00 2001 From: Jan Koscielniak Date: Mon, 14 Oct 2024 17:32:43 +0200 Subject: [PATCH] generic fetcher: Add models for lockfile Adds pydantic models that will be used to represent and validate the lockfile for the generic fetcher. Signed-off-by: Jan Koscielniak --- .../core/package_managers/generic/models.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 cachi2/core/package_managers/generic/models.py diff --git a/cachi2/core/package_managers/generic/models.py b/cachi2/core/package_managers/generic/models.py new file mode 100644 index 000000000..6806f936b --- /dev/null +++ b/cachi2/core/package_managers/generic/models.py @@ -0,0 +1,37 @@ +from typing import Literal, Optional + +from pydantic import BaseModel, field_validator + + +class LockfileMetadata(BaseModel): + """Defines format of the metadata section in the lockfile.""" + + version: Literal["1.0"] + + +class LockfileArtifact(BaseModel): + """Defines format of a single artifact in the lockfile.""" + + download_url: str + target: Optional[str] = None + checksums: dict[str, str] + + @field_validator("checksums") + @classmethod + def no_empty_checksums(cls, value: dict[str, str]) -> dict[str, str]: + """ + Validate that at least one checksum is present for an artifact. + + :param value: the checksums dict to validate + :return: the validated checksum dict + """ + if len(value) == 0: + raise ValueError("At least one checksum must be provided.") + return value + + +class GenericLockfileV1(BaseModel): + """Defines format of the cachi2 generic lockfile, version 1.0.""" + + metadata: LockfileMetadata + artifacts: list[LockfileArtifact]