2022-11-02 13:29:18 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-11-01 11:23:40 +01:00
|
|
|
import logging
|
2022-10-21 15:02:02 +02:00
|
|
|
from hashlib import md5
|
2022-11-02 15:14:38 +01:00
|
|
|
from pathlib import PurePath
|
2022-11-02 13:29:18 +01:00
|
|
|
from typing import Optional
|
2022-10-21 15:02:02 +02:00
|
|
|
|
2022-10-31 16:31:10 +01:00
|
|
|
from lib.db import DB
|
2022-11-02 18:07:24 +01:00
|
|
|
from lib.obs_revision import OBSRevision
|
2022-10-21 09:48:31 +02:00
|
|
|
from lib.request import Request
|
|
|
|
|
|
|
|
|
2022-10-18 12:17:43 +02:00
|
|
|
class DBRevision:
|
2022-11-02 18:07:24 +01:00
|
|
|
def __init__(self, db: DB, row: tuple):
|
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,
|
2022-10-19 14:00:07 +02:00
|
|
|
self.request_number,
|
|
|
|
self.request_id,
|
2022-10-24 20:21:53 +02:00
|
|
|
self.files_hash,
|
2022-10-18 13:13:52 +02:00
|
|
|
) = row
|
2022-10-24 20:21:53 +02:00
|
|
|
self.rev = float(self.rev)
|
2022-10-21 15:02:02 +02:00
|
|
|
self._files = None
|
2022-11-02 18:07:24 +01:00
|
|
|
self.db = db
|
2022-10-18 13:13:52 +02:00
|
|
|
|
2022-10-27 13:16:50 +02:00
|
|
|
def short_string(self):
|
2022-10-31 08:50:05 +01:00
|
|
|
return f"{self.project}/{self.package}/{self.rev}"
|
2022-10-27 13:16:50 +02:00
|
|
|
|
2022-10-18 15:40:11 +02:00
|
|
|
def __str__(self):
|
2022-10-24 12:01:28 +02:00
|
|
|
return f"Rev {self.project}/{self.package}/{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-21 12:08:18 +02:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self.dbid == other.dbid
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
if self.project != other.project:
|
|
|
|
return self.project < other.project
|
|
|
|
if self.package != other.package:
|
|
|
|
return self.package < other.package
|
|
|
|
return self.rev < other.rev
|
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def as_dict(self):
|
2022-10-21 09:48:31 +02:00
|
|
|
"""Return a dict we can put into YAML for test cases"""
|
|
|
|
ret = {
|
|
|
|
"project": self.project,
|
|
|
|
"package": self.package,
|
|
|
|
"rev": self.rev,
|
|
|
|
"unexpanded_srcmd5": self.unexpanded_srcmd5,
|
|
|
|
"commit_time": self.commit_time,
|
|
|
|
"userid": self.userid,
|
|
|
|
"comment": self.comment,
|
|
|
|
"broken": self.broken,
|
|
|
|
"expanded_srcmd5": self.expanded_srcmd5,
|
2022-10-24 20:21:53 +02:00
|
|
|
"files_hash": self.files_hash,
|
2022-11-02 18:07:24 +01:00
|
|
|
"files": self.files_list(),
|
2022-10-21 09:48:31 +02:00
|
|
|
}
|
|
|
|
if self.request_id:
|
2022-11-02 18:07:24 +01:00
|
|
|
ret["request"] = Request.find(self.db, self.request_id).as_dict()
|
2022-10-21 09:48:31 +02:00
|
|
|
return ret
|
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def links_to(self, project: str, package: str) -> None:
|
|
|
|
with self.db.cursor() as cur:
|
2022-10-26 11:58:01 +02:00
|
|
|
cur.execute(
|
|
|
|
"INSERT INTO links (revision_id, project, package) VALUES (%s,%s,%s)",
|
|
|
|
(self.dbid, project, package),
|
|
|
|
)
|
2022-10-18 13:48:27 +02:00
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
@staticmethod
|
|
|
|
def import_obs_rev(db: DB, revision: OBSRevision):
|
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-11-02 18:07:24 +01:00
|
|
|
return DBRevision.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-24 20:21:53 +02:00
|
|
|
if row:
|
2022-11-02 18:07:24 +01:00
|
|
|
return DBRevision(db, row)
|
2022-10-18 12:17:43 +02:00
|
|
|
|
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:
|
2022-10-24 20:21:53 +02:00
|
|
|
return DBRevision.fetch_revision(db, project, package, max)
|
2022-10-18 12:17:43 +02:00
|
|
|
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():
|
2022-11-02 18:07:24 +01:00
|
|
|
ret.append(DBRevision(db, row))
|
2022-10-18 15:40:11 +02:00
|
|
|
return ret
|
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def linked_rev(self):
|
2022-10-24 12:01:28 +02:00
|
|
|
if self.broken:
|
|
|
|
return None
|
2022-11-02 18:07:24 +01:00
|
|
|
with self.db.cursor() as cur:
|
2022-10-26 11:58:01 +02:00
|
|
|
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),
|
|
|
|
)
|
2022-11-02 18:07:24 +01:00
|
|
|
revisions = [DBRevision(self.db, row) for row in cur.fetchall()]
|
2022-10-18 15:40:11 +02:00
|
|
|
if revisions:
|
|
|
|
return revisions[0]
|
2022-10-24 12:01:28 +02:00
|
|
|
else:
|
2022-11-02 18:07:24 +01:00
|
|
|
self.set_broken()
|
2022-10-18 15:40:11 +02:00
|
|
|
return None
|
2022-10-18 19:29:25 +02:00
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def set_broken(self):
|
|
|
|
with self.db.cursor() as cur:
|
2022-10-26 11:58:01 +02:00
|
|
|
cur.execute("UPDATE revisions SET broken=TRUE where id=%s", (self.dbid,))
|
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def import_dir_list(self, xml):
|
|
|
|
with self.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-19 14:00:07 +02:00
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def previous_commit(self):
|
|
|
|
return DBRevision.fetch_revision(
|
|
|
|
self.db, self.project, self.package, int(self.rev) - 1
|
|
|
|
)
|
2022-10-24 20:21:53 +02:00
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def next_commit(self):
|
|
|
|
return DBRevision.fetch_revision(
|
|
|
|
self.db, self.project, self.package, int(self.rev) + 1
|
|
|
|
)
|
2022-10-24 20:21:53 +02:00
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def calculate_files_hash(self):
|
2022-10-21 15:02:02 +02:00
|
|
|
m = md5()
|
2022-11-02 18:07:24 +01:00
|
|
|
for file_dict in self.files_list():
|
2022-10-21 15:02:02 +02:00
|
|
|
m.update(
|
|
|
|
(
|
|
|
|
file_dict["name"]
|
|
|
|
+ "/"
|
|
|
|
+ file_dict["md5"]
|
|
|
|
+ "/"
|
|
|
|
+ str(file_dict["size"])
|
|
|
|
).encode("utf-8")
|
|
|
|
)
|
2022-10-24 20:21:53 +02:00
|
|
|
return m.hexdigest()
|
2022-10-21 15:02:02 +02:00
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def files_list(self):
|
2022-10-21 15:02:02 +02:00
|
|
|
if self._files:
|
|
|
|
return self._files
|
2022-11-02 18:07:24 +01:00
|
|
|
with self.db.cursor() as cur:
|
2022-10-26 15:49:14 +02:00
|
|
|
cur.execute("SELECT * from files where revision_id=%s", (self.dbid,))
|
|
|
|
self._files = []
|
|
|
|
for row in cur.fetchall():
|
|
|
|
(_, _, name, md5, size, mtime) = row
|
|
|
|
self._files.append(
|
|
|
|
{"md5": md5, "size": size, "mtime": mtime, "name": name}
|
|
|
|
)
|
2022-10-21 15:02:02 +02:00
|
|
|
self._files.sort(key=lambda x: x["name"])
|
|
|
|
return self._files
|
2022-10-21 09:48:31 +02:00
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def calc_delta(self, current_rev: Optional[DBRevision]):
|
2022-11-02 13:29:18 +01:00
|
|
|
"""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.
|
|
|
|
"""
|
2022-10-31 16:31:10 +01:00
|
|
|
to_download = []
|
|
|
|
to_delete = []
|
|
|
|
if current_rev:
|
2022-11-02 13:29:18 +01:00
|
|
|
old_files = {
|
2022-11-02 18:07:24 +01:00
|
|
|
e["name"]: f"{e['md5']}-{e['size']}" for e in current_rev.files_list()
|
2022-11-02 13:29:18 +01:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
old_files = dict()
|
2022-11-02 18:07:24 +01:00
|
|
|
for entry in self.files_list():
|
2022-11-02 13:29:18 +01:00
|
|
|
if old_files.get(entry["name"]) != f"{entry['md5']}-{entry['size']}":
|
2022-11-01 11:23:40 +01:00
|
|
|
logging.debug(f"Download {entry['name']}")
|
2022-11-02 15:14:38 +01:00
|
|
|
to_download.append((PurePath(entry["name"]), entry["md5"]))
|
2022-10-31 16:31:10 +01:00
|
|
|
old_files.pop(entry["name"], None)
|
|
|
|
for entry in old_files.keys():
|
2022-11-01 11:23:40 +01:00
|
|
|
logging.debug(f"Delete {entry}")
|
2022-11-02 15:14:38 +01:00
|
|
|
to_delete.append(PurePath(entry))
|
2022-10-31 16:31:10 +01:00
|
|
|
return to_download, to_delete
|
|
|
|
|
2022-10-26 11:58:01 +02:00
|
|
|
@staticmethod
|
2022-10-27 07:33:46 +02:00
|
|
|
def requests_to_fetch(db):
|
2022-10-26 11:58:01 +02:00
|
|
|
with db.cursor() as cur:
|
|
|
|
cur.execute(
|
2022-10-21 09:48:31 +02:00
|
|
|
"""SELECT request_number FROM revisions revs LEFT JOIN requests
|
|
|
|
reqs ON reqs.number=revs.request_number WHERE reqs.id is null AND
|
2022-10-27 07:33:46 +02:00
|
|
|
revs.request_number IS NOT NULL""",
|
2022-10-26 11:58:01 +02:00
|
|
|
)
|
2022-10-21 09:48:31 +02:00
|
|
|
return [row[0] for row in cur.fetchall()]
|
2022-10-21 12:08:18 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def import_fixture_dict(db, rev_dict):
|
|
|
|
"""Used in test cases to read a revision from fixtures into the test database"""
|
|
|
|
with db.cursor() as cur:
|
|
|
|
cur.execute(
|
2022-10-24 20:21:53 +02:00
|
|
|
"""INSERT INTO revisions (project, package, rev, unexpanded_srcmd5, expanded_srcmd5,
|
|
|
|
commit_time, userid, comment, broken, files_hash)
|
|
|
|
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id""",
|
2022-10-21 12:08:18 +02:00
|
|
|
(
|
|
|
|
rev_dict["project"],
|
|
|
|
rev_dict["package"],
|
|
|
|
rev_dict["rev"],
|
|
|
|
rev_dict["unexpanded_srcmd5"],
|
|
|
|
rev_dict["expanded_srcmd5"],
|
|
|
|
rev_dict["commit_time"],
|
|
|
|
rev_dict["userid"],
|
|
|
|
rev_dict["comment"],
|
|
|
|
rev_dict["broken"],
|
2022-10-24 20:21:53 +02:00
|
|
|
rev_dict["files_hash"],
|
2022-10-21 12:08:18 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
rev_id = cur.fetchone()[0]
|
|
|
|
for file_dict in rev_dict["files"]:
|
|
|
|
cur.execute(
|
|
|
|
"INSERT INTO files (md5, mtime, name, size, revision_id) VALUES(%s, %s, %s, %s, %s)",
|
|
|
|
(
|
|
|
|
file_dict["md5"],
|
|
|
|
file_dict["mtime"],
|
|
|
|
file_dict["name"],
|
|
|
|
file_dict["size"],
|
|
|
|
rev_id,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
request = rev_dict.get("request")
|
|
|
|
if request:
|
|
|
|
cur.execute(
|
|
|
|
"""INSERT INTO requests (creator, number, source_project, source_package,
|
|
|
|
source_rev, state, type) VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING id""",
|
|
|
|
(
|
|
|
|
request["creator"],
|
|
|
|
request["number"],
|
|
|
|
request.get("source_project"),
|
|
|
|
request.get("source_package"),
|
|
|
|
request.get("source_rev"),
|
|
|
|
request["state"],
|
|
|
|
request["type"],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
request_id = cur.fetchone()[0]
|
|
|
|
cur.execute(
|
|
|
|
"UPDATE revisions SET request_id=%s, request_number=%s WHERE id=%s",
|
|
|
|
(request_id, request["number"], rev_id),
|
|
|
|
)
|