Prepare link collection
This commit is contained in:
parent
25b45c5073
commit
95412bc834
19
lib/db.py
19
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()
|
||||
|
@ -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:
|
||||
|
@ -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):
|
||||
|
@ -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}")
|
||||
|
@ -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):
|
||||
</revision>"""
|
||||
)
|
||||
)
|
||||
self.db.import_rev(test_rev)
|
||||
DBRevision.import_obs_rev(self.db, test_rev)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user