From 89c033581274a4a9b281f5b0b4364d594dd6c6b5 Mon Sep 17 00:00:00 2001 From: Stephan Kulow Date: Tue, 18 Oct 2022 19:29:25 +0200 Subject: [PATCH] Import file list of revisions --- lib/db.py | 27 +++++++++++++++++++++++++-- lib/db_revision.py | 27 +++++++++++++++++++++++++++ lib/importer.py | 16 +++++++++++++++- lib/obs.py | 17 +++++++++++++++++ 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/lib/db.py b/lib/db.py index 324251e..0a93bfb 100644 --- a/lib/db.py +++ b/lib/db.py @@ -77,14 +77,37 @@ class DB: """ CREATE TABLE links ( id SERIAL PRIMARY KEY, - revision_id INTEGER, + revision_id INTEGER NOT NULL, project VARCHAR(255) NOT NULL, package VARCHAR(255) NOT NULL ) """, + "UPDATE scheme SET version=2", + ) + schemes[3] = ( """ - UPDATE scheme SET version=2 + ALTER TABLE revisions ADD broken boolean NOT NULL DEFAULT(FALSE) """, + "UPDATE scheme SET version=3", + ) + schemes[4] = ( + """ + ALTER TABLE revisions ADD expanded_srcmd5 VARCHAR(255) + """, + "UPDATE scheme SET version=4", + ) + schemes[5] = ( + """ + CREATE TABLE files ( + id SERIAL PRIMARY KEY, + revision_id INTEGER NOT NULL, + name VARCHAR(255) NOT NULL, + md5 VARCHAR(255) NOT NULL, + size INTEGER NOT NULL, + mtime INTEGER NOT NULL + ) + """, + "UPDATE scheme SET version=5", ) schema_version = self.schema_version() diff --git a/lib/db_revision.py b/lib/db_revision.py index 3840a5b..d804669 100644 --- a/lib/db_revision.py +++ b/lib/db_revision.py @@ -11,6 +11,8 @@ class DBRevision: self.userid, self.comment, self.requestid, + self.broken, + self.expanded_srcmd5, ) = row def __str__(self): @@ -101,3 +103,28 @@ class DBRevision: if revisions: return revisions[0] return None + + def set_broken(self, db): + cur = db.cursor() + cur.execute("UPDATE revisions SET broken=TRUE where id=%s", (self.dbid,)) + cur.close() + + def import_dir_list(self, db, xml): + cur = db.cursor() + cur.execute( + "UPDATE revisions SET expanded_srcmd5=%s where id=%s", + (xml.get("srcmd5"), self.dbid), + ) + for entry in xml.findall("entry"): + cur.execute( + """INSERT INTO files (name, md5, size, mtime, revision_id) + VALUES (%s,%s,%s,%s,%s)""", + ( + entry.get("name"), + entry.get("md5"), + entry.get("size"), + entry.get("mtime"), + self.dbid, + ), + ) + cur.close() diff --git a/lib/importer.py b/lib/importer.py index 938fed4..c658c2c 100644 --- a/lib/importer.py +++ b/lib/importer.py @@ -1,5 +1,6 @@ import functools import logging +import xml.etree.ElementTree as ET from lib.binary import is_binary_or_large from lib.db import DB @@ -154,9 +155,22 @@ class Importer: self.update_db_package(db, lproject, lpackage) for rev in DBRevision.all_revisions(db, project, self.package): - print(rev, rev.linked_rev(db)) + if rev.broken or rev.expanded_srcmd5: + continue + linked_rev = rev.linked_rev(db) + if linked_rev: + linked_rev = linked_rev.unexpanded_srcmd5 + list = self.obs.list( + project, self.package, rev.unexpanded_srcmd5, linked_rev + ) + if list: + print(ET.tostring(list).decode("utf-8")) + rev.import_dir_list(db, list) + else: + rev.set_broken(db) cur.close() + db.conn.commit() def import_all_revisions(self, gc): # Fetch all the requests and sort them. Ideally we should diff --git a/lib/obs.py b/lib/obs.py index 2ecf241..77c7c7a 100644 --- a/lib/obs.py +++ b/lib/obs.py @@ -140,3 +140,20 @@ class OBS: def download(self, project, package, name, revision, dirpath): with (dirpath / name).open("wb") as f: f.write(self._download(project, package, name, revision).read()) + + def list(self, project, package, srcmd5, linkrev): + params = {"rev": srcmd5, "expand": "1"} + if linkrev: + params["linkrev"] = linkrev + + try: + root = self._xml(f"source/{project}/{package}", **params) + except HTTPError as e: + if e.code == 400: + logging.error( + f"Package [{project}/{package} {params}] can't be expanded: {e}" + ) + return None + raise e + + return root