add templating for tag and package name in info.yaml

This commit is contained in:
Александр Степченко 2024-10-23 16:15:16 +03:00
parent 0c09627b12
commit d4f88412bf
3 changed files with 30 additions and 14 deletions

View File

@ -24,7 +24,8 @@ PKG_VERSION: str = ""
class Image:
canonical_name: str
is_versioned: bool | None
source_packages_list: list[str] | None
verion_template: str | None
source_packages: list[str] | None
def __init__(self, canonical_name: str):
self.canonical_name = canonical_name
@ -34,7 +35,7 @@ class Image:
info_file = self.path / "info.yaml"
if not info_file.exists():
self.is_versioned = None
self.source_packages_list = None
self.source_packages = None
return
info: dict = yaml.safe_load(info_file.read_text())
@ -44,19 +45,23 @@ class Image:
f"info.yaml for {self.canonical_name} doesn't contain 'is_versioned' key"
)
if "source_packages_list" not in info:
if "source_packages" not in info:
raise RuntimeError(
f"info.yaml for {self.canonical_name} doesn't contain 'source_packages_list' key"
f"info.yaml for {self.canonical_name} doesn't contain 'source_packages' key"
)
self.is_versioned = info["is_versioned"]
self.source_packages_list = info["source_packages_list"]
self.source_packages = info["source_packages"]
if self.is_versioned and not self.source_packages_list:
if self.is_versioned and not self.source_packages:
raise RuntimeError(
f"source_packages_list for {self.canonical_name} doesn't contain any values"
f"source_packages for {self.canonical_name} doesn't contain any values"
)
self.version_template = None
if "version_template" in info:
self.version_template = info["version_template"]
class Tasks:
def __init__(self, tasks):
@ -87,7 +92,7 @@ def api_get_source_package_version(branch: str, package_name: str) -> str:
if response.status_code != 200:
print(response)
raise RuntimeError(
f"failed to retrieve package version: package {package_name!r}, branch {branch!r} "
f"failed to retrieve source package version: source package {package_name!r}, branch {branch!r} "
)
result = response.json()
@ -105,12 +110,16 @@ class Tags:
def tags(self, branch: str, image: Image):
if self._tags is None:
if image.is_versioned and image.source_packages_list:
package_name = image.source_packages_list[0]
if "{version}" in package_name:
if image.is_versioned and image.source_packages:
package_name = image.source_packages[0]
if mat := re.search("{{.*}}", package_name):
assert PKG_VERSION is not None
package_name = package_name.format(version=PKG_VERSION)
package_name = Template(package_name).render(version=PKG_VERSION)
version = api_get_source_package_version(branch, package_name)
if image.version_template is not None:
version = Template(image.version_template).render(version=version)
tags = [version]
else:
tags = [branch]

View File

@ -0,0 +1,6 @@
---
is_versioned: true
version_template: v{{ version.rsplit('.', 1) | first }}-flannel{{ version.rsplit('.', 1) | last }}
source_packages:
- cni-plugin-flannel
...

View File

@ -1,5 +1,6 @@
---
is_versioned: true
source_packages_list:
- kubernetes{version}
version_template: v{{ version }}
source_packages:
- kubernetes{{ version }}
...