From d9ed54ffcfa7d0635ebb1555bac579a209676284 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Wed, 29 Mar 2023 05:49:10 +0000 Subject: [PATCH] merge the two github upstream classes it allows the use of aiohttp --- ccb/upstream_project.py | 102 +++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/ccb/upstream_project.py b/ccb/upstream_project.py index 6b0df75b..b958eb3d 100644 --- a/ccb/upstream_project.py +++ b/ccb/upstream_project.py @@ -9,7 +9,6 @@ import traceback import logging import aiohttp -import requests from .version import Version, VersionMeta from .project_specifics import ( @@ -228,10 +227,58 @@ def __init__(self, recipe): super().__init__(recipe, git_url) self.owner = owner self.repo = repo + self.__versions = None + + async def versions(self): + if self.__versions is None: + try: + with aiohttp.ClientSession(raise_for_status=True) as client: + github_token = get_github_token() + headers = {"Accept": "application/vnd.github.v3+json"} + if github_token: + headers["Authorization"] = f"token {github_token}" + with client.get( + f"https://api.github.com/repos/{self.owner}/{self.repo}/releases", headers=headers + ) as resp: + def _get_url_for_version(v): + artifacts = v["assets"] + for a in artifacts: + if a["content_type"] == "application/x-xz": + return a["browser_download_url"] + for a in artifacts: + if a["content_type"] == "application/x-gzip": + return a["browser_download_url"] + for a in artifacts: + if a["content_type"] == "application/gzip": + return a["browser_download_url"] + for a in artifacts: + if a["content_type"] == "application/x-bzip2": + return a["browser_download_url"] + return f"https://github.com/{self.owner}/{self.repo}/archive/refs/tags/{v['tag_name']}.tar.gz" + self.__versions = [] + for v in await resp.json(): + tag_name = v["tag_name"] or v["name"] + r = Version(tag_name, fixer=self.fixer) + if not self._valid_tags(tag_name): + continue + r.url = _get_url_for_version(v) + self.__versions.append(r) + + except Exception as exc: + logger.info("%s: error parsing repository: %s", self.recipe.name, exc) + logger.debug(traceback.format_exc()) + self.__versions = [] + if self.__versions: + return self.__versions + else: + return super().versions + def source_url(self, version): if version.unknown: return None + if hasattr(version, "url"): + return version.url return f"https://github.com/{self.owner}/{self.repo}/archive/{version.original}.tar.gz" @classmethod @@ -251,57 +298,6 @@ def _get_owner_repo(cls, recipe): raise _Unsupported() -class GithubReleaseProject(GithubProject): - - def __init__(self, recipe): - super().__init__(recipe) - try: - with requests.Session() as client: - github_token = get_github_token() - headers = {"Accept": "application/vnd.github.v3+json"} - if github_token: - headers["Authorization"] = f"token {github_token}" - with client.get( - f"https://api.github.com/repos/{self.owner}/{self.repo}/releases", headers=headers - ) as resp: - resp.raise_for_status() - def _get_url_for_version(v): - artifacts = v["assets"] - for a in artifacts: - if a["content_type"] == "application/x-xz": - return a["browser_download_url"] - for a in artifacts: - if a["content_type"] == "application/gzip": - return a["browser_download_url"] - for a in artifacts: - if a["content_type"] == "application/x-bzip2": - return a["browser_download_url"] - return f"https://github.com/{self.owner}/{self.repo}/archive/refs/tags/{v['tag_name']}.tar.gz" - self.__versions = [] - for v in resp.json(): - tag_name = v["tag_name"] or v["name"] - r = Version(tag_name, fixer=self.fixer) - if not self._valid_tags(tag_name): - continue - r.url = _get_url_for_version(v) - self.__versions.append(r) - - except Exception as exc: - logger.info("%s: error parsing repository: %s", self.recipe.name, exc) - logger.debug(traceback.format_exc()) - self.__versions = [] - if not self.__versions: - raise _Unsupported() - - async def versions(self): - return self.__versions - - def source_url(self, version): - if version.unknown: - return None - return version.url - - class GitlabProject(GitProject): SOURCE_URL_RE = re.compile( r"https?://([^/]*gitlab[^/]+)/([^/]+)/([^/]+)/-/archive/" @@ -387,4 +383,4 @@ def source_url(self, version): return f"https://{self.domain}/sources/{self.project}/{major}.{minor}/{self.project}-{major}.{minor}.{patch}.tar.xz" -_CLASSES = [GithubReleaseProject, GithubProject, GitlabProject, GnomeProject] +_CLASSES = [GithubProject, GitlabProject, GnomeProject]