git-importer/lib/db_revision.py

138 lines
4.7 KiB
Python
Raw Normal View History

2022-10-18 12:17:43 +02:00
class DBRevision:
def __init__(self, row):
2022-10-18 13:13:52 +02:00
# 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,
2022-10-18 19:29:25 +02:00
self.broken,
self.expanded_srcmd5,
self.request_number,
self.request_id,
2022-10-18 13:13:52 +02:00
) = row
2022-10-18 15:40:11 +02:00
def __str__(self):
return f"Rev {self.project}/{self.rev} Md5 {self.unexpanded_srcmd5} {self.commit_time} {self.userid} {self.request_number}"
2022-10-18 15:40:11 +02:00
def __repr__(self):
return f"[{self.__str__()}]"
2022-10-18 13:13:52 +02:00
def links_to(self, db, project, package):
2022-10-26 11:58:01 +02:00
with db.cursor() as cur:
cur.execute(
"INSERT INTO links (revision_id, project, package) VALUES (%s,%s,%s)",
(self.dbid, project, package),
)
2022-10-18 13:13:52 +02:00
@classmethod
def import_obs_rev(cls, db, revision):
2022-10-26 11:58:01 +02:00
with db.cursor() as cur:
cur.execute(
"""INSERT INTO revisions (project, package, rev, unexpanded_srcmd5, commit_time, userid, comment, request_number)
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.request_number,
),
)
2022-10-18 13:13:52 +02:00
return cls.fetch_revision(db, revision.project, revision.package, revision.rev)
2022-10-18 12:17:43 +02:00
2022-10-26 11:58:01 +02:00
@staticmethod
def fetch_revision(db, project, package, rev):
with db.cursor() as cur:
cur.execute(
"SELECT * FROM revisions where project=%s and package=%s and rev=%s",
(project, package, str(rev)),
)
row = cur.fetchone()
2022-10-18 12:17:43 +02:00
return DBRevision(row)
2022-10-26 11:58:01 +02:00
@staticmethod
def latest_revision(db, project, package):
with db.cursor() as cur:
cur.execute(
"SELECT MAX(rev) FROM revisions where project=%s and package=%s",
(project, package),
)
max = cur.fetchone()[0]
2022-10-18 12:17:43 +02:00
if max:
return DBRevision.fetch_revision(db, project, package, int(max))
return None
2022-10-18 15:40:11 +02:00
2022-10-26 11:58:01 +02:00
@staticmethod
def all_revisions(db, project, package):
with db.cursor() as cur:
cur.execute(
"SELECT * FROM revisions where project=%s and package=%s",
(project, package),
)
ret = []
for row in cur.fetchall():
ret.append(DBRevision(row))
2022-10-18 15:40:11 +02:00
return ret
def linked_rev(self, db):
2022-10-26 11:58:01 +02:00
with db.cursor() as cur:
cur.execute(
"SELECT project,package FROM links where revision_id=%s", (self.dbid,)
)
row = cur.fetchone()
if not row:
return None
project, package = row
cur.execute(
"SELECT * FROM revisions where project=%s and package=%s and commit_time <= %s ORDER BY commit_time DESC LIMIT 1",
(project, package, self.commit_time),
)
revisions = [DBRevision(row) for row in cur.fetchall()]
2022-10-18 15:40:11 +02:00
if revisions:
return revisions[0]
return None
2022-10-18 19:29:25 +02:00
def set_broken(self, db):
2022-10-26 11:58:01 +02:00
with db.cursor() as cur:
cur.execute("UPDATE revisions SET broken=TRUE where id=%s", (self.dbid,))
2022-10-18 19:29:25 +02:00
def import_dir_list(self, db, xml):
2022-10-26 11:58:01 +02:00
with db.cursor() as cur:
2022-10-18 19:29:25 +02:00
cur.execute(
2022-10-26 11:58:01 +02:00
"UPDATE revisions SET expanded_srcmd5=%s where id=%s",
(xml.get("srcmd5"), self.dbid),
2022-10-18 19:29:25 +02:00
)
2022-10-26 11:58:01 +02:00
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,
),
)
2022-10-26 11:58:01 +02:00
@staticmethod
def requests_to_fetch(db, project, package):
with db.cursor() as cur:
cur.execute(
"""SELECT request_number FROM revisions revs left join requests
reqs on reqs.number=revs.request_number where reqs.id is null and
revs.request_number is not null and project=%s and package=%s;""",
(project, package),
)
ret = [row[0] for row in cur.fetchall()]
return ret