appstream: build cache on startup and after updates

parent 19fece2d
......@@ -31,6 +31,7 @@ class DataInfo:
def __init__(self, client: AppStreamClient, appstream_dir: str):
self.client = client
self.dir = appstream_dir
self._on_update = None
os.makedirs(self.dir, exist_ok=True)
async def load_by_branch(self, branch: str, version: str):
......@@ -47,6 +48,9 @@ class DataInfo:
with open(filepath, "w", encoding="utf-8") as f:
f.write(text)
if self._on_update:
self._on_update(branch)
def get_file_path(self, branch: str) -> str | None:
for fname in os.listdir(self.dir):
if fname.startswith(f"{branch}-") and fname.endswith(".xml"):
......@@ -68,23 +72,41 @@ class PackageInfo:
def __init__(self, client: AppStreamClient, data: DataInfo):
self.client = client
self._data = data
self._cache: dict[str, dict[str, str]] = {}
def id_by_pkgname(self, pkgname: str, branch: str) -> str | None:
def rebuild_cache(self, branch: str) -> None:
path = self._data.get_file_path(branch)
if not path:
return None
self._cache.pop(branch, None)
return
result: dict[str, str] = {}
context = etree.iterparse(path, events=("end",), recover=True)
for _, elem in context:
if elem.tag == "component" and elem.get("type") != "addon":
if elem.findtext("pkgname") == pkgname:
return elem.findtext("id")
pkgname = elem.findtext("pkgname")
app_id = elem.findtext("id")
if pkgname and app_id:
result[pkgname] = app_id
elem.clear()
return None
self._cache[branch] = result
def id_by_pkgname(self, pkgname: str, branch: str) -> str | None:
if branch not in self._cache:
return None
return self._cache[branch].get(pkgname)
class ALTRepoAppStream:
BRANCHES = ["sisyphus", "p11"]
def __init__(self, session: aiohttp.ClientSession, appstream_dir: str):
self._client = AppStreamClient(session)
self.data = DataInfo(self._client, appstream_dir)
self.package = PackageInfo(self._client, self.data)
self.data._on_update = self.package.rebuild_cache
for branch in self.BRANCHES:
if self.data.has_file(branch):
self.package.rebuild_cache(branch)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment