Add the capability to export the package data as test fixture

This commit is contained in:
Stephan Kulow 2022-10-21 09:48:31 +02:00
parent 87d9fcc131
commit ce3863e1b5
5 changed files with 25546 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import sys
import osc.core
from lib.exporter import Exporter
from lib.importer import Importer
URL_OBS = "https://api.opensuse.org"
@ -88,6 +89,11 @@ def main():
action="store_true",
help="Import revisions into database only",
)
parser.add_argument(
"--export",
action="store_true",
help="Export database fields for the given package as JSON",
)
args = parser.parse_args()
@ -103,6 +109,10 @@ def main():
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
if args.export:
Exporter(args.package).run()
return
if not args.repodir:
args.repodir = pathlib.Path(args.package)
@ -117,6 +127,7 @@ def main():
importer = Importer(
PROJECTS, args.package, args.repodir, args.search_ancestor, args.rebase_devel
)
if args.db:
importer.import_into_db()
return

View File

@ -1,3 +1,6 @@
from lib.request import Request
class DBRevision:
def __init__(self, row):
# need to stay in sync with the schema creation in db.py
@ -22,6 +25,24 @@ class DBRevision:
def __repr__(self):
return f"[{self.__str__()}]"
def as_dict(self, db):
"""Return a dict we can put into YAML for test cases"""
ret = {
"project": self.project,
"package": self.package,
"rev": self.rev,
"unexpanded_srcmd5": self.unexpanded_srcmd5,
"commit_time": self.commit_time,
"userid": self.userid,
"comment": self.comment,
"broken": self.broken,
"expanded_srcmd5": self.expanded_srcmd5,
"files": self.files_list(db),
}
if self.request_id:
ret["request"] = Request.find(db, self.request_id).as_dict()
return ret
def links_to(self, db, project, package):
with db.cursor() as cur:
cur.execute(
@ -124,14 +145,22 @@ class DBRevision:
),
)
def files_list(self, db):
cur = db.cursor()
cur.execute("SELECT * from files where revision_id=%s", (self.dbid,))
ret = []
for row in cur.fetchall():
(_, _, name, md5, size, mtime) = row
ret.append({"md5": md5, "size": size, "mtime": mtime, "name": name})
return ret
@staticmethod
def requests_to_fetch(db, project, package):
def requests_to_fetch(self, db, project, package):
with db.cursor() as cur:
cur.execute(
"""SELECT request_number FROM revisions revs left join requests
reqs on reqs.number=revs.request_number where reqs.id is null and
revs.request_number is not null and project=%s and package=%s;""",
"""SELECT request_number FROM revisions revs LEFT JOIN requests
reqs ON reqs.number=revs.request_number WHERE reqs.id is null AND
revs.request_number IS NOT NULL and project=%s AND package=%s;""",
(project, package),
)
ret = [row[0] for row in cur.fetchall()]
return ret
return [row[0] for row in cur.fetchall()]

21
lib/exporter.py Normal file
View File

@ -0,0 +1,21 @@
import sys
import yaml
from lib.db import DB
from lib.db_revision import DBRevision
class Exporter:
def __init__(self, package):
self.package = package
def run(self):
db = DB()
cur = db.cursor()
cur.execute("SELECT * from revisions where package=%s", (self.package,))
data = {"revisions": []}
for row in cur.fetchall():
data["revisions"].append(DBRevision(row).as_dict(db))
yaml.dump(data, sys.stdout, default_flow_style=False)

View File

@ -54,3 +54,35 @@ class Request:
"UPDATE revisions SET request_id=%s WHERE request_number=%s",
(rowid, self.number),
)
@staticmethod
def find(db, request_id):
with db.cursor() as cur:
cur.execute("""SELECT * from requests WHERE id=%s""", (request_id,))
row = cur.fetchone()
ret = Request()
ret._from_db(row)
return ret
def _from_db(self, row):
(
self.dbid,
self.number,
self.creator,
self.type_,
self.state,
self.source_package,
self.source_project,
self.source_rev,
) = row
def as_dict(self):
return {
"number": self.number,
"creator": self.creator,
"type": self.type_,
"state": self.state,
"source_project": self.source_project,
"source_package": self.source_package,
"source_rev": self.source_rev,
}

25447
tests/fixtures/zsh-data.yaml vendored Normal file

File diff suppressed because it is too large Load Diff