try automatic package version detection

This commit is contained in:
parent b046b4ec48
commit 5108c6c04c
2 changed files with 76 additions and 10 deletions

View File

@ -6,25 +6,56 @@ import json
import re
import subprocess
import textwrap
from dataclasses import dataclass
from graphlib import TopologicalSorter
from pathlib import Path
import requests
import tomli
import yaml
from jinja2 import Template
ORG_DIR = Path("org")
PKG_VERSION: str = ""
@dataclass
class Image:
def __init__(self, canonical_name):
canonical_name: str
is_versioned: bool | None
source_packages_list: list[str] | None
def __init__(self, canonical_name: str):
self.canonical_name = canonical_name
self.path = ORG_DIR / canonical_name
self.base_name = re.sub("^[^/]+/", "", canonical_name)
def __str__(self):
return (f'Image(canonical_name="{self.canonical_name}", '
f'path="{self.path}", base_name="{self.base_name}")')
info_file = self.path / "info.yaml"
if not info_file.exists():
self.is_versioned = None
self.source_packages_list = None
return
info: dict = yaml.safe_load(info_file.read_text())
if "is_versioned" not in info:
raise RuntimeError(
f"info.yaml for {self.canonical_name} doesn't contain 'is_versioned' key"
)
if "source_packages_list" not in info:
raise RuntimeError(
f"info.yaml for {self.canonical_name} doesn't contain 'source_packages_list' key"
)
self.is_versioned = info["is_versioned"]
self.source_packages_list = info["source_packages_list"]
if self.is_versioned and not self.source_packages_list:
raise RuntimeError(
f"source_packages_list for {self.canonical_name} doesn't contain any values"
)
class Tasks:
@ -49,18 +80,40 @@ class Tasks:
]
def api_get_source_package_version(branch: str, package_name: str) -> str:
api_url = "https://rdb.altlinux.org/api/site/package_versions_from_tasks"
params = {"branch": branch, "name": package_name}
response = requests.get(api_url, params)
if response.status_code != 200:
print(response)
raise RuntimeError(
f"failed to retrieve package version: package {package_name!r}, branch {branch!r} "
)
result = response.json()
return result["versions"][0]["version"]
class Tags:
def __init__(self, tags_file, latest):
def __init__(self, tags_file: str | None, latest: str):
if tags_file is None:
self._tags = None
else:
tags_file = Path(tags_file)
self._tags = tomli.loads(tags_file.read_text())
self._tags = tomli.loads(Path(tags_file).read_text())
self._latest = latest
def tags(self, branch, image: Image):
def tags(self, branch: str, image: Image):
if self._tags is None:
tags = [branch]
if image.is_versioned and image.source_packages_list:
package_name = image.source_packages_list[0]
if "{version}" in package_name:
assert PKG_VERSION is not None
package_name = package_name.format(version=PKG_VERSION)
version = api_get_source_package_version(branch, package_name)
tags = [version]
else:
tags = [branch]
else:
tags = self._tags[image.canonical_name][branch].copy()
if branch == self._latest:
@ -578,6 +631,7 @@ class DockerBuilder:
"--force-rm",
f"--manifest={manifest}",
f"--platform={platforms}",
f"--build-arg=PKG_VERSION={PKG_VERSION}",
".",
]
self.run(build_cmd, cwd=image.path)
@ -749,6 +803,10 @@ def parse_args():
choices=stages,
help="list of stages to skip",
)
parser.add_argument(
"--package-version",
help="from which package to build",
)
args = parser.parse_args()
args.stages = set(args.stages) - set(args.skip_stages)
@ -760,7 +818,10 @@ def parse_args():
def main():
global PKG_VERSION
args = parse_args()
PKG_VERSION = args.package_version
arches = args.arches
images_info = ImagesInfo()
tags = Tags(args.tags, args.latest)

View File

@ -0,0 +1,5 @@
---
is_versioned: true
source_packages_list:
- kubernetes{version}
...