moved to asyncd

This commit is contained in:
alexander
2026-01-23 09:21:42 +01:00
parent efc2116de4
commit e93e743f65
11 changed files with 106 additions and 76 deletions

View File

@@ -1,23 +1,27 @@
from sources import CSSourceGitea, CSItem, CSInventoryItem
from sources.util import cache_cheatsheet, get_datestring
import requests
import httpx
from pathlib import Path
async def process_gitea(item: CSInventoryItem, outdir: str) -> list[CSItem] | None:
source: CSSourceGitea = item.source
commit_hash = get_release_commit_sha(source.base_url, source.owner, source.repo, source.tag)
asserts = list_release_assets(source.base_url, source.owner, source.repo, source.tag)
commit_hash = await get_release_commit_sha(source.base_url, source.owner, source.repo, source.tag)
asserts = await list_release_assets(source.base_url, source.owner, source.repo, source.tag)
asserts = filter(lambda a: a[1].endswith(".pdf"), asserts)
asserts = list(filter(lambda a: a[1].endswith(".pdf"), asserts))
asserts = list(map(lambda a: (a[0], f"{source.base_url}/repos/{source.owner}/{source.repo}/releases/download/{source.tag}/{a[0]}"), asserts))
print(f"Found {len(asserts)} PDF assets in Gitea release {source.owner}/{source.repo}@{source.tag}")
res = []
for a in asserts:
res_url = a[0]
if item.cache:
cache_url = cache_cheatsheet(a[0], outdir)
cache_url = await cache_cheatsheet(a[0], outdir)
if cache_url:
res_url = cache_url
else:
@@ -38,7 +42,7 @@ async def process_gitea(item: CSInventoryItem, outdir: str) -> list[CSItem] | No
return res
def get_release_commit_sha(base_url, owner, repo, tag_name, token=None):
async def get_release_commit_sha(base_url, owner, repo, tag_name, token=None):
"""
Resolve the commit SHA for a Gitea release tag.
@@ -50,49 +54,48 @@ def get_release_commit_sha(base_url, owner, repo, tag_name, token=None):
:return: commit SHA (str)
"""
headers = {}
if token:
headers["Authorization"] = f"token {token}"
session = requests.Session()
session.headers.update(headers)
async with httpx.AsyncClient() as client:
headers = {}
if token:
headers["Authorization"] = f"token {token}"
# 1) List tags and find the matching tag
tags_url = f"{base_url}/api/v1/repos/{owner}/{repo}/tags"
resp = session.get(tags_url)
resp.raise_for_status()
tags = resp.json()
# 1) List tags and find the matching tag
tags_url = f"{base_url}/api/v1/repos/{owner}/{repo}/tags"
resp = await client.get(tags_url, headers=headers)
resp.raise_for_status()
tags = resp.json()
tag = next((t for t in tags if t["name"] == tag_name), None)
if not tag:
raise ValueError(f"Tag '{tag_name}' not found")
tag = next((t for t in tags if t["name"] == tag_name), None)
if not tag:
raise ValueError(f"Tag '{tag_name}' not found")
# Lightweight tags usually already contain the commit SHA
commit_sha = tag.get("commit", {}).get("sha")
tag_obj_sha = tag.get("id")
# Lightweight tags usually already contain the commit SHA
commit_sha = tag.get("commit", {}).get("sha")
tag_obj_sha = tag.get("id")
# If commit.sha looks valid, return it
if commit_sha:
return commit_sha
# If commit.sha looks valid, return it
if commit_sha:
return commit_sha
# 2) Annotated tag: dereference via /git/tags/{sha}
if not tag_obj_sha:
raise RuntimeError("Tag object SHA missing; cannot dereference annotated tag")
# 2) Annotated tag: dereference via /git/tags/{sha}
if not tag_obj_sha:
raise RuntimeError("Tag object SHA missing; cannot dereference annotated tag")
git_tag_url = f"{base_url}/api/v1/repos/{owner}/{repo}/git/tags/{tag_obj_sha}"
resp = session.get(git_tag_url)
resp.raise_for_status()
annotated = resp.json()
git_tag_url = f"{base_url}/api/v1/repos/{owner}/{repo}/git/tags/{tag_obj_sha}"
resp = await client.get(git_tag_url, headers=headers)
resp.raise_for_status()
annotated = resp.json()
# The object pointed to by the tag (usually a commit)
target = annotated.get("object", {})
if target.get("type") != "commit":
raise RuntimeError(f"Tag points to a {target.get('type')} instead of a commit")
# The object pointed to by the tag (usually a commit)
target = annotated.get("object", {})
if target.get("type") != "commit":
raise RuntimeError(f"Tag points to a {target.get('type')} instead of a commit")
return target.get("sha")
def list_release_assets(base_url, owner, repo, tag, token=None):
async def list_release_assets(base_url, owner, repo, tag, token=None):
"""
Return a list of (download_url, filename) for all assets of a Gitea release.
@@ -103,26 +106,28 @@ def list_release_assets(base_url, owner, repo, tag, token=None):
:param token: optional API token
:returns: list of (download_url, filename) tuples
"""
headers = {}
if token:
headers["Authorization"] = f"token {token}"
# 1) Get release by tag
rel_url = f"{base_url}/api/v1/repos/{owner}/{repo}/releases/tags/{tag}"
rel_resp = requests.get(rel_url, headers=headers)
rel_resp.raise_for_status()
release = rel_resp.json()
async with httpx.AsyncClient() as client:
headers = {}
if token:
headers["Authorization"] = f"token {token}"
assets = release.get("assets", [])
result = []
# 1) Get release by tag
rel_url = f"{base_url}/api/v1/repos/{owner}/{repo}/releases/tags/{tag}"
rel_resp = await client.get(rel_url, headers=headers)
rel_resp.raise_for_status()
release: dict = rel_resp.json()
for asset in assets:
# Gitea asset info usually contains:
# - "browser_download_url" → direct URL
# - "name" → filename
download_url = asset.get("browser_download_url")
filename = asset.get("name")
if download_url and filename:
result.append((download_url, filename))
assets = release.get("assets", [])
result = []
for asset in assets:
# Gitea asset info usually contains:
# - "browser_download_url" → direct URL
# - "name" → filename
download_url = asset.get("browser_download_url")
filename = asset.get("name")
if download_url and filename:
result.append((download_url, filename))
return result