forked from importers/git-importer
Implement a fileshash function for revision
This commit is contained in:
parent
5bd790e915
commit
bc74b6950b
@ -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):
|
||||
|
@ -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__()}]"
|
||||
|
@ -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__":
|
||||
|
Loading…
Reference in New Issue
Block a user