From fd91c016077a5d64db9774dc15f176bbd5b85cc0 Mon Sep 17 00:00:00 2001 From: Luca Beltrame Date: Sat, 15 Jan 2022 10:00:29 +0100 Subject: [PATCH] WIP experiment --- obs/update_unstable.py | 97 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 18 deletions(-) diff --git a/obs/update_unstable.py b/obs/update_unstable.py index 8a480ea..0836dfe 100755 --- a/obs/update_unstable.py +++ b/obs/update_unstable.py @@ -8,7 +8,7 @@ from collections import defaultdict import logging import json from pathlib import Path -from typing import Union, Dict, Any +from typing import Union, Dict, Any, List from urllib.parse import quote import git @@ -20,6 +20,7 @@ import sarge API_URL = "https://invent.kde.org/api/v4/projects/" OBS_URL = "https://api.opensuse.org/trigger/runservice" MATRIX_COMMANDER = "/home/mocker/local-venv/bin/matrix-commander.py" +REPO_TEMPLATE = "https://invent.kde.org/{}" MESSAGE_TEMPLATE = f""" ### OBS package update complete @@ -29,9 +30,86 @@ Stats for {date.today().strftime('%Y-%m-%d')}: """ +def project_exists(project: str) -> bool: + # We want / to get quoted, so put safe to "" + project_name = quote(project, safe="") + request = requests.get(API_URL + project_name) + + if request: + return True + + return False + + +def get_remote_hash(url: str, branch: str = "master") -> str: + + gitcmd = git.cmd.Git() + revision = gitcmd.ls_remote(url, branch, quiet=True, refs=True) + git_hash, branch = revision.split("\t") + return git_hash + + +class RepoUpdater: + + def __init__(self, config: str, cache_file: str) -> None: + + if not Path(cache_file).exists(): + logging.debug("File cache not found, not loading") + self._data: Dict[str, Dict[str, str]] = dict() + else: + with open(self.cache) as handle: + self._data = json.load(handle) + + with open(config, "r") as mapping: + repo_data = json.load(mapping) + self.config = repo_data + + def __getitem__(self, key: str) -> Dict[str, str]: + + if key not in self._data: + raise KeyError(f"{key}") + + return self._data[key] + + def __setitem__(self, key: str, value: Dict[str, str]) -> None: + self._data[key] = value + + def get(self, key: str, + *args: Any, **kwargs: Any) -> Union[None, str, Dict[str, str]]: + return self._data.get(key, *args, **kwargs) + + def package_lists(self, repo: str) -> List[str]: + return self[repo] + + def get_updated_remotes(self, repository: str) -> List[str]: + + to_update = list() + + repodata = self.config[repository] + for repo in repodata: + for item in repo: + kde_name = item["kde"] + branch = item["branch"] + url = REPO_TEMPLATE.format(kde_name) + if not project_exists(kde_name): + logging.warning("Repository %s not found, skipping", + kde_name) + continue + + local_hash = self[repository].get(kde_name, "") + remote_hash = get_remote_hash(url, branch) + + if local_hash != remote_hash: + to_update.append(item["obs"]) + + return to_update + + + class GitHashCache: def __init__(self, cache_file: str) -> None: + self.cache = cache_file self._data: Dict[str, Dict[str, str]] = dict() @@ -63,24 +141,7 @@ class GitHashCache: with open(self.cache) as handle: self._data = json.load(handle) - -def project_exists(project: str) -> bool: - # We want / to get quoted, so put safe to "" - project_name = quote(project, safe="") - request = requests.get(API_URL + project_name) - - if request: - return True - - return False - - -def get_remote_hash(url: str, branch: str = "master") -> str: - - gitcmd = git.cmd.Git() revision = gitcmd.ls_remote(url, branch, refs=True) - git_hash, branch = revision.split("\t") - return git_hash def trigger_update(repository: str, package_name: str,