Implement a fileshash function for revision

This commit is contained in:
Stephan Kulow 2022-10-21 15:02:02 +02:00
parent 5bd790e915
commit bc74b6950b
3 changed files with 41 additions and 6 deletions

View File

@ -1,3 +1,5 @@
from hashlib import md5
from lib.request import Request from lib.request import Request
@ -19,6 +21,7 @@ class DBRevision:
self.request_id, self.request_id,
) = row ) = row
self.rev = int(self.rev) self.rev = int(self.rev)
self._files = None
def __str__(self): def __str__(self):
return f"Rev {self.project}/{self.rev} Md5 {self.unexpanded_srcmd5} {self.commit_time} {self.userid} {self.request_number}" 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): def files_list(self, db):
if self._files:
return self._files
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT * from files where revision_id=%s", (self.dbid,)) cur.execute("SELECT * from files where revision_id=%s", (self.dbid,))
ret = [] self._files = []
for row in cur.fetchall(): for row in cur.fetchall():
(_, _, name, md5, size, mtime) = row (_, _, name, md5, size, mtime) = row
ret.append({"md5": md5, "size": size, "mtime": mtime, "name": name}) self._files.append({"md5": md5, "size": size, "mtime": mtime, "name": name})
return ret self._files.sort(key=lambda x: x["name"])
return self._files
@staticmethod @staticmethod
def requests_to_fetch(self, db, project, package): def requests_to_fetch(self, db, project, package):

View File

@ -3,6 +3,7 @@ class Request:
self.number = int(xml.get("id")) self.number = int(xml.get("id"))
self.creator = xml.get("creator") self.creator = xml.get("creator")
self.state = xml.find("state").get("name") self.state = xml.find("state").get("name")
# not used in the database
self.target = xml.find("action/target").get("project") self.target = xml.find("action/target").get("project")
self.type_ = xml.find("action").get("type") self.type_ = xml.find("action").get("type")
@ -28,7 +29,7 @@ class Request:
return self.type_ return self.type_
def __str__(self): 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): def __repr__(self):
return f"[{self.__str__()}]" return f"[{self.__str__()}]"

View File

@ -1,11 +1,11 @@
import os import os
import unittest import unittest
from pprint import pprint
import yaml import yaml
from lib.db import DB from lib.db import DB
from lib.db_revision import DBRevision from lib.db_revision import DBRevision
from lib.request import Request
class TestTreeMethods(unittest.TestCase): class TestTreeMethods(unittest.TestCase):
@ -19,7 +19,21 @@ class TestTreeMethods(unittest.TestCase):
def test_create_tree(self): def test_create_tree(self):
factory_revisions = DBRevision.all_revisions(self.db, "openSUSE:Factory", "zsh") 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__": if __name__ == "__main__":