diff --git a/lib/db_revision.py b/lib/db_revision.py index 0e84edc..8876e7a 100644 --- a/lib/db_revision.py +++ b/lib/db_revision.py @@ -1,3 +1,5 @@ +from hashlib import md5 + from lib.request import Request @@ -19,6 +21,7 @@ class DBRevision: self.request_id, ) = row self.rev = int(self.rev) + self._files = None def __str__(self): return f"Rev {self.project}/{self.rev} Md5 {self.unexpanded_srcmd5} {self.commit_time} {self.userid} {self.request_number}" @@ -156,14 +159,31 @@ class DBRevision: ), ) + def fileshash(self, db): + m = md5() + for file_dict in self.files_list(db): + m.update( + ( + file_dict["name"] + + "/" + + file_dict["md5"] + + "/" + + str(file_dict["size"]) + ).encode("utf-8") + ) + return m.hexdigest() + def files_list(self, db): + if self._files: + return self._files cur = db.cursor() cur.execute("SELECT * from files where revision_id=%s", (self.dbid,)) - ret = [] + self._files = [] for row in cur.fetchall(): (_, _, name, md5, size, mtime) = row - ret.append({"md5": md5, "size": size, "mtime": mtime, "name": name}) - return ret + self._files.append({"md5": md5, "size": size, "mtime": mtime, "name": name}) + self._files.sort(key=lambda x: x["name"]) + return self._files @staticmethod def requests_to_fetch(self, db, project, package): diff --git a/lib/request.py b/lib/request.py index 4333cfd..fcdba9f 100644 --- a/lib/request.py +++ b/lib/request.py @@ -3,6 +3,7 @@ class Request: self.number = int(xml.get("id")) self.creator = xml.get("creator") self.state = xml.find("state").get("name") + # not used in the database self.target = xml.find("action/target").get("project") self.type_ = xml.find("action").get("type") @@ -28,7 +29,7 @@ class Request: return self.type_ def __str__(self): - return f"Req {self.number} {self.creator} {self.type_} {self.source_project}->{self.target} {self.state}" + return f"Req {self.number} {self.creator} {self.type_} {self.source_project} {self.state}" def __repr__(self): return f"[{self.__str__()}]" diff --git a/tests/tree_test.py b/tests/tree_test.py index 50abba0..401ec68 100644 --- a/tests/tree_test.py +++ b/tests/tree_test.py @@ -1,11 +1,11 @@ import os import unittest -from pprint import pprint import yaml from lib.db import DB from lib.db_revision import DBRevision +from lib.request import Request class TestTreeMethods(unittest.TestCase): @@ -19,7 +19,21 @@ class TestTreeMethods(unittest.TestCase): def test_create_tree(self): factory_revisions = DBRevision.all_revisions(self.db, "openSUSE:Factory", "zsh") - pprint(sorted(factory_revisions)) + source_revisions = dict() + for rev in sorted(factory_revisions): + print(rev, rev.fileshash(self.db)) + if rev.request_id: + req = Request.find(self.db, rev.request_id) + print(" ", req) + key = f"{req.source_project}/{req.source_package}" + if key not in source_revisions: + source_revisions[key] = sorted( + DBRevision.all_revisions( + self.db, req.source_project, req.source_package + ) + ) + for rev2 in source_revisions.get(key): + print(rev2, rev2.fileshash(self.db)) if __name__ == "__main__":