Build a revision tree

This commit is contained in:
Stephan Kulow 2022-10-22 12:03:21 +02:00
parent bc74b6950b
commit 089b215f4c
4 changed files with 48 additions and 21 deletions

View File

@ -22,6 +22,7 @@ class DBRevision:
) = row ) = row
self.rev = int(self.rev) self.rev = int(self.rev)
self._files = None self._files = None
self._hash = 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}"
@ -159,7 +160,9 @@ class DBRevision:
), ),
) )
def fileshash(self, db): def files_hash(self, db):
if self._hash:
return self._hash
m = md5() m = md5()
for file_dict in self.files_list(db): for file_dict in self.files_list(db):
m.update( m.update(
@ -171,7 +174,8 @@ class DBRevision:
+ str(file_dict["size"]) + str(file_dict["size"])
).encode("utf-8") ).encode("utf-8")
) )
return m.hexdigest() self._hash = m.hexdigest()
return self._hash
def files_list(self, db): def files_list(self, db):
if self._files: if self._files:

View File

@ -9,10 +9,9 @@ from lib.history import History
from lib.obs import OBS from lib.obs import OBS
from lib.obs_revision import OBSRevision from lib.obs_revision import OBSRevision
from lib.proxy_sha256 import ProxySHA256, md5, sha256 from lib.proxy_sha256 import ProxySHA256, md5, sha256
from lib.request import Request
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from lib.user import User from lib.user import User
from lib.tree_builder import TreeBuilder
def _files_hash(hash_alg, dirpath): def _files_hash(hash_alg, dirpath):
"""List of (filepath, md5) for a directory""" """List of (filepath, md5) for a directory"""
@ -185,6 +184,7 @@ class Importer:
self.obs.request(number).import_into_db(db) self.obs.request(number).import_into_db(db)
db.conn.commit() db.conn.commit()
TreeBuilder(db).build(self.package)
def import_all_revisions(self, gc): def import_all_revisions(self, gc):
# Fetch all the requests and sort them. Ideally we should # Fetch all the requests and sort them. Ideally we should

38
lib/tree_builder.py Normal file
View File

@ -0,0 +1,38 @@
from lib.db_revision import DBRevision
from lib.request import Request
class TreeBuilder:
def __init__(self, db):
self.db = db
def filtered_revisions(self, project, package):
revisions = DBRevision.all_revisions(self.db, project, package)
revisions.sort()
ret = []
prev = None
for rev in revisions:
if rev.broken: continue
if prev and prev.files_hash(self.db) == rev.files_hash(self.db):
continue
ret.append(rev)
prev = rev
return ret
def build(self, package):
factory_revisions = self.filtered_revisions("openSUSE:Factory", package)
source_revisions = dict()
for rev in factory_revisions:
print(rev, rev.files_hash(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] = self.filtered_revisions(
req.source_project, req.source_package
)
for rev2 in source_revisions.get(key):
if rev2.files_hash(self.db) == rev.files_hash(self.db):
print(" ", rev2)

View File

@ -5,7 +5,7 @@ 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 from lib.tree_builder import TreeBuilder
class TestTreeMethods(unittest.TestCase): class TestTreeMethods(unittest.TestCase):
@ -18,22 +18,7 @@ class TestTreeMethods(unittest.TestCase):
DBRevision.import_fixture_dict(self.db, rev) DBRevision.import_fixture_dict(self.db, rev)
def test_create_tree(self): def test_create_tree(self):
factory_revisions = DBRevision.all_revisions(self.db, "openSUSE:Factory", "zsh") TreeBuilder(self.db).build("zsh")
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__":