1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-12-25 17:36:13 +01:00

buildhistory: Produce proper output using build_table() and a CSV writer

This commit is contained in:
Daniel Mach 2022-12-15 10:05:18 +01:00
parent 2735d5a0d1
commit 6e4ad5db1c
4 changed files with 87 additions and 55 deletions

View File

@ -3,6 +3,7 @@
#
# The cherry-picked imports will be the supported API.
from .api_build import BuildHistory
from .api_source import add_channels
from .api_source import add_containers
from .api_source import enable_channels

79
osc/_private/api_build.py Normal file
View File

@ -0,0 +1,79 @@
import csv
import io
import time
from . import api
class BuildHistory:
def __init__(
self,
apiurl: str,
project: str,
package: str,
repository: str,
arch: str,
limit: int = 0,
):
self.apiurl = apiurl
self.project = project
self.package = package
self.repository = repository
self.arch = arch
self._limit = limit
self.entries = self._get_entries()
def _get_entries(self):
url_path = [
"build",
self.project,
self.repository,
self.arch,
self.package,
"_history",
]
url_query = {}
if self._limit and self._limit > 0:
query["limit"] = self._limit
root = api.get(self.apiurl, url_path, url_query)
result = []
nodes = api.find_nodes(root, "buildhistory", "entry")
for node in nodes:
item = {
"rev": node.get("rev"),
"srcmd5": node.get("srcmd5"),
"ver_rel": node.get("versrel"),
"build_count": int(node.get("bcnt")),
"time": time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(int(node.get("time")))),
"duration": int(node.get("duration")),
}
result.append(item)
return result
def to_csv(self):
out = io.StringIO()
header = ["time", "srcmd5", "rev", "ver_rel", "build_count", "duration"]
writer = csv.DictWriter(out, fieldnames=header, quoting=csv.QUOTE_ALL)
writer.writeheader()
for i in self.entries:
writer.writerow(i)
return out.getvalue()
def to_text_table(self):
from ..core import build_table
header = ("TIME", "SRCMD5", "VER-REL.BUILD#", "REV", "DURATION")
data = []
for i in self.entries:
item = (
i["time"],
i["srcmd5"],
f"{i['ver_rel']}.{i['build_count']}",
i["rev"],
i["duration"],
)
data.extend(item)
return "\n".join(build_table(len(header), data, header))

View File

@ -6814,36 +6814,20 @@ Please submit there instead, or use --nodevelproject to force direct submission.
osc buildhist REPOSITORY ARCHITECTURE
osc buildhist PROJECT PACKAGE[:FLAVOR] REPOSITORY ARCHITECTURE
"""
args = slash_split(args)
if len(args) < 2 and is_package_dir('.'):
self.print_repos()
apiurl = self.get_api_url()
if len(args) == 4:
project = self._process_project_name(args[0])
package = args[1]
repository = args[2]
arch = args[3]
elif len(args) == 2:
wd = Path.cwd()
package = store_read_package(wd)
project = store_read_project(wd)
repository = args[0]
arch = args[1]
else:
raise oscerr.WrongArgs('Wrong number of arguments')
args = list(args)
project, package, repository, arch = pop_project_package_repository_arch_from_args(args)
ensure_no_remaining_args(args)
if opts.multibuild_package:
package = package + ":" + opts.multibuild_package
format = 'text'
history = _private.BuildHistory(apiurl, project, package, repository, arch, limit=opts.limit)
if opts.csv:
format = 'csv'
print('\n'.join(get_buildhistory(apiurl, project, package, repository, arch, format, opts.limit)))
print(history.to_csv(), end="")
else:
print(history.to_text_table())
@cmdln.option('', '--csv', action='store_true',
help='generate output in CSV (separated by |)')

View File

@ -6810,38 +6810,6 @@ def get_source_rev(apiurl: str, project: str, package: str, revision=None):
return e
def get_buildhistory(apiurl: str, prj: str, package: str, repository: str, arch: str, format="text", limit=None):
query = {}
if limit is not None and int(limit) > 0:
query['limit'] = int(limit)
u = makeurl(apiurl, ['build', prj, repository, arch, package, '_history'], query)
f = http_GET(u)
root = ET.parse(f).getroot()
r = []
for node in root.findall('entry'):
rev = node.get('rev')
srcmd5 = node.get('srcmd5')
versrel = node.get('versrel')
bcnt = int(node.get('bcnt'))
duration = node.get('duration')
t = time.gmtime(int(node.get('time')))
t = time.strftime('%Y-%m-%d %H:%M:%S', t)
if duration is None:
duration = ""
if format == 'csv':
r.append('%s|%s|%s|%s.%d|%s' % (t, srcmd5, rev, versrel, bcnt, duration))
else:
bversrel = '%s.%d' % (versrel, bcnt)
r.append('%s %s %s %s %s' % (t, srcmd5, bversrel.ljust(16)[:16], rev, duration.rjust(10)))
if format == 'text':
r.insert(0, 'time srcmd5 vers-rel.bcnt rev duration')
return r
def print_jobhistory(apiurl: str, prj: str, current_package: str, repository: str, arch: str, format="text", limit=20):
query = {}
if current_package: