Switch to walker for tree print

This commit is contained in:
Stephan Kulow 2022-10-31 07:03:19 +01:00
parent 6f83425538
commit 822329279d
3 changed files with 32 additions and 4 deletions

View File

@ -130,6 +130,7 @@ def main():
if args.db: if args.db:
importer.import_into_db() importer.import_into_db()
importer.export_as_git()
return return
importer.import_all_revisions(args.gc) importer.import_all_revisions(args.gc)

View File

@ -259,6 +259,10 @@ class Importer:
) )
return [DBRevision(row) for row in cur.fetchall()] return [DBRevision(row) for row in cur.fetchall()]
def export_as_git(self):
db = DB()
TreeBuilder(db).build(self.package).print()
def import_into_db(self): def import_into_db(self):
db = DB() db = DB()
for project, _, api_url in self.projects: for project, _, api_url in self.projects:
@ -303,7 +307,6 @@ 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).print()
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

View File

@ -2,6 +2,19 @@ from lib.db_revision import DBRevision
from lib.request import Request from lib.request import Request
class AbstractWalker:
def call(self, node, is_source):
pass
class PrintWalker(AbstractWalker):
def call(self, node, is_source):
if is_source:
print(" ", node.revision, node.revision.files_hash)
else:
print(node.revision, node.revision.files_hash)
class TreeNode: class TreeNode:
""" """
Nodes in this "tree" have either no parent (root), one parent (in a chain) Nodes in this "tree" have either no parent (root), one parent (in a chain)
@ -13,20 +26,24 @@ class TreeNode:
self.merged = None self.merged = None
self.revision = rev self.revision = rev
self.merged_into = None self.merged_into = None
self.git_commit = None
def print(self): def walk(self, walker: AbstractWalker):
node = self node = self
while node: while node:
print(node.revision, node.revision.files_hash) walker.call(node, False)
if node.merged: if node.merged:
source_node = node.merged source_node = node.merged
while source_node: while source_node:
print(" ", source_node.revision, source_node.revision.files_hash) walker.call(node, True)
source_node = source_node.parent source_node = source_node.parent
if source_node and source_node.merged_into: if source_node and source_node.merged_into:
break break
node = node.parent node = node.parent
def print(self):
self.walk(PrintWalker())
def as_list(self): def as_list(self):
"""Return a list for test cases""" """Return a list for test cases"""
node = self node = self
@ -51,6 +68,8 @@ class TreeBuilder:
self.db = db self.db = db
def revisions_chain(self, project, package): def revisions_chain(self, project, package):
"""Build a tree without branches (chain) from a project's
history ignoring empty and broken revisions"""
revisions = DBRevision.all_revisions(self.db, project, package) revisions = DBRevision.all_revisions(self.db, project, package)
revisions.sort() revisions.sort()
prev = None prev = None
@ -69,6 +88,8 @@ class TreeBuilder:
return tree return tree
def find_merge(self, revision, source_chain): def find_merge(self, revision, source_chain):
"""For a given revision in the target, find the node in the source chain
that matches the files"""
node = source_chain node = source_chain
while node: while node:
# exclude reverts happening after the merge # exclude reverts happening after the merge
@ -80,6 +101,8 @@ class TreeBuilder:
node = node.parent node = node.parent
def add_merge_points(self, factory_revisions): def add_merge_points(self, factory_revisions):
"""For all target revisions that accepted a request, look up the merge
points in the source chains (ignoring the actual revision submitted for now)"""
source_revisions = dict() source_revisions = dict()
factory_node = factory_revisions factory_node = factory_revisions
while factory_node: while factory_node:
@ -120,6 +143,7 @@ class TreeBuilder:
factory_node = factory_node.parent factory_node = factory_node.parent
def build(self, package): def build(self, package):
"""Create a Factory tree (returning the top)"""
factory_revisions = self.revisions_chain("openSUSE:Factory", package) factory_revisions = self.revisions_chain("openSUSE:Factory", package)
self.add_merge_points(factory_revisions) self.add_merge_points(factory_revisions)