1
0
Fork 0

WIP experiment

This commit is contained in:
Luca Beltrame 2022-01-15 10:00:29 +01:00
parent a3596a214f
commit fd91c01607
Signed by: einar
GPG key ID: 4707F46E9EC72DEC

View file

@ -8,7 +8,7 @@ from collections import defaultdict
import logging import logging
import json import json
from pathlib import Path from pathlib import Path
from typing import Union, Dict, Any from typing import Union, Dict, Any, List
from urllib.parse import quote from urllib.parse import quote
import git import git
@ -20,6 +20,7 @@ import sarge
API_URL = "https://invent.kde.org/api/v4/projects/" API_URL = "https://invent.kde.org/api/v4/projects/"
OBS_URL = "https://api.opensuse.org/trigger/runservice" OBS_URL = "https://api.opensuse.org/trigger/runservice"
MATRIX_COMMANDER = "/home/mocker/local-venv/bin/matrix-commander.py" MATRIX_COMMANDER = "/home/mocker/local-venv/bin/matrix-commander.py"
REPO_TEMPLATE = "https://invent.kde.org/{}"
MESSAGE_TEMPLATE = f""" MESSAGE_TEMPLATE = f"""
### OBS package update complete ### 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: class GitHashCache:
def __init__(self, cache_file: str) -> None: def __init__(self, cache_file: str) -> None:
self.cache = cache_file self.cache = cache_file
self._data: Dict[str, Dict[str, str]] = dict() self._data: Dict[str, Dict[str, str]] = dict()
@ -63,24 +141,7 @@ class GitHashCache:
with open(self.cache) as handle: with open(self.cache) as handle:
self._data = json.load(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) 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, def trigger_update(repository: str, package_name: str,