Prepare link collection

This commit is contained in:
Stephan Kulow 2022-10-18 13:13:52 +02:00
parent 25b45c5073
commit 95412bc834
5 changed files with 64 additions and 30 deletions

View File

@ -69,7 +69,7 @@ class DB:
""", """,
""" """
CREATE UNIQUE INDEX ppr ON revisions (project, package, rev); CREATE UNIQUE INDEX ppr ON revisions (project, package, rev);
""" """,
) )
if self.schema_version() > 0: if self.schema_version() > 0:
@ -91,23 +91,6 @@ class DB:
def cursor(self): def cursor(self):
return self.conn.cursor() 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__": if __name__ == "__main__":
db = DB() db = DB()

View File

@ -1,12 +1,48 @@
class DBRevision: class DBRevision:
def __init__(self, row): 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 @classmethod
def fetch_revision(cls, db, project, package, rev): def fetch_revision(cls, db, project, package, rev):
cur = db.cursor() 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() row = cur.fetchone()
cur.close() cur.close()
return DBRevision(row) return DBRevision(row)
@ -14,7 +50,10 @@ class DBRevision:
@classmethod @classmethod
def latest_revision(cls, db, project, package): def latest_revision(cls, db, project, package):
cur = db.cursor() 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] max = cur.fetchone()[0]
cur.close() cur.close()
if max: if max:

View File

@ -3,11 +3,11 @@ import logging
from lib.binary import is_binary_or_large from lib.binary import is_binary_or_large
from lib.db import DB from lib.db import DB
from lib.db_revision import DBRevision
from lib.git import Git from lib.git import Git
from lib.history import History from lib.history import History
from lib.obs import OBS from lib.obs import OBS
from lib.obs_revision import OBSRevision from lib.obs_revision import OBSRevision
from lib.db_revision import DBRevision
from lib.proxy_sha256 import ProxySHA256, md5, sha256 from lib.proxy_sha256 import ProxySHA256, md5, sha256
@ -131,7 +131,12 @@ class Importer:
for r in root.findall("revision"): for r in root.findall("revision"):
rev = OBSRevision(self.obs, self, project, self.package).parse(r) rev = OBSRevision(self.obs, self, project, self.package).parse(r)
if not latest or rev.rev > latest.rev: 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() db.conn.commit()
def import_into_db(self): def import_into_db(self):

View File

@ -61,24 +61,30 @@ class OBSRevision:
def __repr__(self): def __repr__(self):
return f"[{self.__str__()}]" return f"[{self.__str__()}]"
def check_link(self): def read_link(self):
"""Add 'linkrev' attribute into the revision. Returns False if the link is invalid"""
try: try:
root = self.obs._xml( return self.obs._xml(
f"source/{self.project}/{self.package}/_link", rev=self.srcmd5 f"source/{self.project}/{self.package}/_link", rev=self.srcmd5
) )
except HTTPError as e: except HTTPError as e:
if e.code == 404: if e.code == 404:
logging.debug("No _link for the revision") logging.debug("No _link for the revision")
return True return None
raise e 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: except ET.ParseError:
logging.error( logging.error(
f"_link can't be parsed [{self.project}/{self.package} rev={self.srcmd5}]" f"_link can't be parsed [{self.project}/{self.package} rev={self.srcmd5}]"
) )
return False return False
target_project = root.get("project")
rev = self.history.find_last_rev_after_time(target_project, self.time) rev = self.history.find_last_rev_after_time(target_project, self.time)
if rev: if rev:
logging.debug(f"Linkrev found: {rev}") logging.debug(f"Linkrev found: {rev}")

View File

@ -2,6 +2,7 @@ import unittest
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from lib.db import DB from lib.db import DB
from lib.db_revision import DBRevision
from lib.history import History from lib.history import History
from lib.obs import OBS from lib.obs import OBS
from lib.obs_revision import OBSRevision from lib.obs_revision import OBSRevision
@ -27,7 +28,7 @@ class TestDBMethods(unittest.TestCase):
</revision>""" </revision>"""
) )
) )
self.db.import_rev(test_rev) DBRevision.import_obs_rev(self.db, test_rev)
if __name__ == "__main__": if __name__ == "__main__":