From 95412bc83405ab2ab79f59563de6a287b9d02187 Mon Sep 17 00:00:00 2001 From: Stephan Kulow Date: Tue, 18 Oct 2022 13:13:52 +0200 Subject: [PATCH] Prepare link collection --- lib/db.py | 19 +---------------- lib/db_revision.py | 47 +++++++++++++++++++++++++++++++++++++---- lib/importer.py | 9 ++++++-- lib/obs_revision.py | 16 +++++++++----- tests/db_import_test.py | 3 ++- 5 files changed, 64 insertions(+), 30 deletions(-) diff --git a/lib/db.py b/lib/db.py index dfb4f9a..a6bca83 100644 --- a/lib/db.py +++ b/lib/db.py @@ -69,7 +69,7 @@ class DB: """, """ CREATE UNIQUE INDEX ppr ON revisions (project, package, rev); - """ + """, ) if self.schema_version() > 0: @@ -91,23 +91,6 @@ class DB: def cursor(self): return self.conn.cursor() - def import_rev(self, revision): - cur = self.conn.cursor() - cur.execute( - """INSERT INTO revisions (project, package, rev, unexpanded_srcmd5, commit_time, userid, comment, requestid) - VALUES(%s, %s, %s, %s, %s, %s, %s, %s)""", - ( - revision.project, - revision.package, - revision.rev, - revision.unexpanded_srcmd5, - revision.time, - revision.userid, - revision.comment, - revision.requestid, - ), - ) - if __name__ == "__main__": db = DB() diff --git a/lib/db_revision.py b/lib/db_revision.py index 62f9cb0..02109d6 100644 --- a/lib/db_revision.py +++ b/lib/db_revision.py @@ -1,12 +1,48 @@ class DBRevision: - def __init__(self, row): - (self.dbid, self.project, self.package, self.rev, self.unexpanded_srcmd5, self.commit_time, self.userid, self.comment, self.requestid) = row + # need to stay in sync with the schema creation in db.py + ( + self.dbid, + self.project, + self.package, + self.rev, + self.unexpanded_srcmd5, + self.commit_time, + self.userid, + self.comment, + self.requestid, + ) = row + + def links_to(self, db, project, package): + pass + + @classmethod + def import_obs_rev(cls, db, revision): + cur = db.cursor() + cur.execute( + """INSERT INTO revisions (project, package, rev, unexpanded_srcmd5, commit_time, userid, comment, requestid) + VALUES(%s, %s, %s, %s, %s, %s, %s, %s)""", + ( + revision.project, + revision.package, + revision.rev, + revision.unexpanded_srcmd5, + revision.time, + revision.userid, + revision.comment, + revision.requestid, + ), + ) + cur.close() + return cls.fetch_revision(db, revision.project, revision.package, revision.rev) @classmethod def fetch_revision(cls, db, project, package, rev): cur = db.cursor() - cur.execute("SELECT * FROM revisions where project=%s and package=%s and rev=%s", (project, package, rev)) + cur.execute( + "SELECT * FROM revisions where project=%s and package=%s and rev=%s", + (project, package, rev), + ) row = cur.fetchone() cur.close() return DBRevision(row) @@ -14,7 +50,10 @@ class DBRevision: @classmethod def latest_revision(cls, db, project, package): cur = db.cursor() - cur.execute("SELECT MAX(rev) FROM revisions where project=%s and package=%s", (project, package)) + cur.execute( + "SELECT MAX(rev) FROM revisions where project=%s and package=%s", + (project, package), + ) max = cur.fetchone()[0] cur.close() if max: diff --git a/lib/importer.py b/lib/importer.py index ecc008d..97c43d1 100644 --- a/lib/importer.py +++ b/lib/importer.py @@ -3,11 +3,11 @@ import logging from lib.binary import is_binary_or_large from lib.db import DB +from lib.db_revision import DBRevision from lib.git import Git from lib.history import History from lib.obs import OBS from lib.obs_revision import OBSRevision -from lib.db_revision import DBRevision from lib.proxy_sha256 import ProxySHA256, md5, sha256 @@ -131,7 +131,12 @@ class Importer: for r in root.findall("revision"): rev = OBSRevision(self.obs, self, project, self.package).parse(r) if not latest or rev.rev > latest.rev: - db.import_rev(rev) + dbrev = DBRevision.import_obs_rev(db, rev) + root = rev.read_link() + if root: + tprj = root.get("project") or project + tpkg = root.get("package") or self.package + dbrev.links_to(db, tprj, tpkg) db.conn.commit() def import_into_db(self): diff --git a/lib/obs_revision.py b/lib/obs_revision.py index 1c0875e..0cc6799 100644 --- a/lib/obs_revision.py +++ b/lib/obs_revision.py @@ -61,24 +61,30 @@ class OBSRevision: def __repr__(self): return f"[{self.__str__()}]" - def check_link(self): - """Add 'linkrev' attribute into the revision. Returns False if the link is invalid""" + def read_link(self): try: - root = self.obs._xml( + return self.obs._xml( f"source/{self.project}/{self.package}/_link", rev=self.srcmd5 ) except HTTPError as e: if e.code == 404: logging.debug("No _link for the revision") - return True + return None raise e + + def check_link(self): + """Add 'linkrev' attribute into the revision. Returns False if the link is invalid""" + try: + root = self.read_link() + if root is None: + return True + target_project = root.get("project") except ET.ParseError: logging.error( f"_link can't be parsed [{self.project}/{self.package} rev={self.srcmd5}]" ) return False - target_project = root.get("project") rev = self.history.find_last_rev_after_time(target_project, self.time) if rev: logging.debug(f"Linkrev found: {rev}") diff --git a/tests/db_import_test.py b/tests/db_import_test.py index aa9ca2a..aa4d53f 100644 --- a/tests/db_import_test.py +++ b/tests/db_import_test.py @@ -2,6 +2,7 @@ import unittest import xml.etree.ElementTree as ET from lib.db import DB +from lib.db_revision import DBRevision from lib.history import History from lib.obs import OBS from lib.obs_revision import OBSRevision @@ -27,7 +28,7 @@ class TestDBMethods(unittest.TestCase): """ ) ) - self.db.import_rev(test_rev) + DBRevision.import_obs_rev(self.db, test_rev) if __name__ == "__main__":