forked from importers/git-importer
Keep a reference to the database in DBRevision
To avoid passing the db to all actions
This commit is contained in:
@@ -6,11 +6,12 @@ from pathlib import PurePath
|
||||
from typing import Optional
|
||||
|
||||
from lib.db import DB
|
||||
from lib.obs_revision import OBSRevision
|
||||
from lib.request import Request
|
||||
|
||||
|
||||
class DBRevision:
|
||||
def __init__(self, row):
|
||||
def __init__(self, db: DB, row: tuple):
|
||||
# need to stay in sync with the schema creation in db.py
|
||||
(
|
||||
self.dbid,
|
||||
@@ -29,6 +30,7 @@ class DBRevision:
|
||||
) = row
|
||||
self.rev = float(self.rev)
|
||||
self._files = None
|
||||
self.db = db
|
||||
|
||||
def short_string(self):
|
||||
return f"{self.project}/{self.package}/{self.rev}"
|
||||
@@ -49,7 +51,7 @@ class DBRevision:
|
||||
return self.package < other.package
|
||||
return self.rev < other.rev
|
||||
|
||||
def as_dict(self, db):
|
||||
def as_dict(self):
|
||||
"""Return a dict we can put into YAML for test cases"""
|
||||
ret = {
|
||||
"project": self.project,
|
||||
@@ -62,21 +64,21 @@ class DBRevision:
|
||||
"broken": self.broken,
|
||||
"expanded_srcmd5": self.expanded_srcmd5,
|
||||
"files_hash": self.files_hash,
|
||||
"files": self.files_list(db),
|
||||
"files": self.files_list(),
|
||||
}
|
||||
if self.request_id:
|
||||
ret["request"] = Request.find(db, self.request_id).as_dict()
|
||||
ret["request"] = Request.find(self.db, self.request_id).as_dict()
|
||||
return ret
|
||||
|
||||
def links_to(self, db, project, package):
|
||||
with db.cursor() as cur:
|
||||
def links_to(self, project: str, package: str) -> None:
|
||||
with self.db.cursor() as cur:
|
||||
cur.execute(
|
||||
"INSERT INTO links (revision_id, project, package) VALUES (%s,%s,%s)",
|
||||
(self.dbid, project, package),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def import_obs_rev(cls, db, revision):
|
||||
@staticmethod
|
||||
def import_obs_rev(db: DB, revision: OBSRevision):
|
||||
with db.cursor() as cur:
|
||||
cur.execute(
|
||||
"""INSERT INTO revisions (project, package, rev, unexpanded_srcmd5, commit_time, userid, comment, request_number)
|
||||
@@ -92,7 +94,9 @@ class DBRevision:
|
||||
revision.request_number,
|
||||
),
|
||||
)
|
||||
return cls.fetch_revision(db, revision.project, revision.package, revision.rev)
|
||||
return DBRevision.fetch_revision(
|
||||
db, revision.project, revision.package, revision.rev
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def fetch_revision(db, project, package, rev):
|
||||
@@ -103,7 +107,7 @@ class DBRevision:
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if row:
|
||||
return DBRevision(row)
|
||||
return DBRevision(db, row)
|
||||
|
||||
@staticmethod
|
||||
def latest_revision(db, project, package):
|
||||
@@ -126,13 +130,13 @@ class DBRevision:
|
||||
)
|
||||
ret = []
|
||||
for row in cur.fetchall():
|
||||
ret.append(DBRevision(row))
|
||||
ret.append(DBRevision(db, row))
|
||||
return ret
|
||||
|
||||
def linked_rev(self, db):
|
||||
def linked_rev(self):
|
||||
if self.broken:
|
||||
return None
|
||||
with db.cursor() as cur:
|
||||
with self.db.cursor() as cur:
|
||||
cur.execute(
|
||||
"SELECT project,package FROM links where revision_id=%s", (self.dbid,)
|
||||
)
|
||||
@@ -144,19 +148,19 @@ class DBRevision:
|
||||
"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()]
|
||||
revisions = [DBRevision(self.db, row) for row in cur.fetchall()]
|
||||
if revisions:
|
||||
return revisions[0]
|
||||
else:
|
||||
self.set_broken(db)
|
||||
self.set_broken()
|
||||
return None
|
||||
|
||||
def set_broken(self, db):
|
||||
with db.cursor() as cur:
|
||||
def set_broken(self):
|
||||
with self.db.cursor() as cur:
|
||||
cur.execute("UPDATE revisions SET broken=TRUE where id=%s", (self.dbid,))
|
||||
|
||||
def import_dir_list(self, db, xml):
|
||||
with db.cursor() as cur:
|
||||
def import_dir_list(self, xml):
|
||||
with self.db.cursor() as cur:
|
||||
cur.execute(
|
||||
"UPDATE revisions SET expanded_srcmd5=%s where id=%s",
|
||||
(xml.get("srcmd5"), self.dbid),
|
||||
@@ -174,15 +178,19 @@ class DBRevision:
|
||||
),
|
||||
)
|
||||
|
||||
def previous_commit(self, db):
|
||||
return self.fetch_revision(db, self.project, self.package, int(self.rev) - 1)
|
||||
def previous_commit(self):
|
||||
return DBRevision.fetch_revision(
|
||||
self.db, self.project, self.package, int(self.rev) - 1
|
||||
)
|
||||
|
||||
def next_commit(self, db):
|
||||
return self.fetch_revision(db, self.project, self.package, int(self.rev) + 1)
|
||||
def next_commit(self):
|
||||
return DBRevision.fetch_revision(
|
||||
self.db, self.project, self.package, int(self.rev) + 1
|
||||
)
|
||||
|
||||
def calculate_files_hash(self, db):
|
||||
def calculate_files_hash(self):
|
||||
m = md5()
|
||||
for file_dict in self.files_list(db):
|
||||
for file_dict in self.files_list():
|
||||
m.update(
|
||||
(
|
||||
file_dict["name"]
|
||||
@@ -194,10 +202,10 @@ class DBRevision:
|
||||
)
|
||||
return m.hexdigest()
|
||||
|
||||
def files_list(self, db):
|
||||
def files_list(self):
|
||||
if self._files:
|
||||
return self._files
|
||||
with db.cursor() as cur:
|
||||
with self.db.cursor() as cur:
|
||||
cur.execute("SELECT * from files where revision_id=%s", (self.dbid,))
|
||||
self._files = []
|
||||
for row in cur.fetchall():
|
||||
@@ -208,7 +216,7 @@ class DBRevision:
|
||||
self._files.sort(key=lambda x: x["name"])
|
||||
return self._files
|
||||
|
||||
def calc_delta(self, db: DB, current_rev: Optional[DBRevision]):
|
||||
def calc_delta(self, current_rev: Optional[DBRevision]):
|
||||
"""Calculate the list of files to download and to delete.
|
||||
Param current_rev is the revision that's currently checked out.
|
||||
If it's None, the repository is empty.
|
||||
@@ -217,11 +225,11 @@ class DBRevision:
|
||||
to_delete = []
|
||||
if current_rev:
|
||||
old_files = {
|
||||
e["name"]: f"{e['md5']}-{e['size']}" for e in current_rev.files_list(db)
|
||||
e["name"]: f"{e['md5']}-{e['size']}" for e in current_rev.files_list()
|
||||
}
|
||||
else:
|
||||
old_files = dict()
|
||||
for entry in self.files_list(db):
|
||||
for entry in self.files_list():
|
||||
if old_files.get(entry["name"]) != f"{entry['md5']}-{entry['size']}":
|
||||
logging.debug(f"Download {entry['name']}")
|
||||
to_download.append((PurePath(entry["name"]), entry["md5"]))
|
||||
|
||||
Reference in New Issue
Block a user