exception-free watch_builds

Related: https://pagure.io/koji/issue/2981
This commit is contained in:
Tomas Kopecek 2022-06-20 10:39:23 +02:00
parent d2247940b6
commit 4e6aca0bd0
3 changed files with 26 additions and 37 deletions

View file

@ -7086,12 +7086,13 @@ def anon_handle_wait_repo(options, session, args):
warn("nvr %s is not current in tag %s\n latest build in %s is %s" %
(expected_nvr, tag, tag, present_nvr))
try:
watch_builds(
session, tag_id, builds, quiet=suboptions.quiet,
poll_interval=options.poll_interval, timeout=suboptions.timeout)
except koji.GenericError as e:
error(str(e))
success, msg = watch_builds(session, tag_id, builds,
poll_interval=options.poll_interval, timeout=suboptions.timeout)
if success:
if not suboptions.quiet:
print(msg)
else:
error('' if suboptions.quiet else msg)
def handle_regen_repo(options, session, args):

View file

@ -831,25 +831,23 @@ def _list_tasks(options, session):
return tasklist
def watch_builds(session, tag_id, builds, quiet=False, poll_interval=5, timeout=120):
"""Watch for given builds to appear in given tag. If no build are given,
def watch_builds(session, tag_id, builds, poll_interval=5, timeout=120):
"""Watch for given builds to appear in given tag. If no builds are given,
watch for new repo for given tag.
:param session: Koji session object
:param tag_id: Tag id
:param builds: List of builds as NVR dicts
:param quiet: no/verbose
:param poll_interval: Poll interval in seconds
:param timeout: Watch timeout in minutes"""
:param int tag_id: Tag id
:param [dict] builds: List of builds as NVR dicts
:param int poll_interval: Poll interval in seconds
:param int timeout: Watch timeout in minutes
:returns bool, msg: False if timeouted
"""
last_repo = None
repo = session.getRepo(tag_id)
# String representations for logs and exceptions
builds_str = koji.util.printList(
[koji.buildLabel(build) for build in builds])
tag_info = session.getTag(tag_id)
if not tag_info:
raise koji.GenericError("No tag with id: %s" % tag_id)
builds_str = koji.util.printList([koji.buildLabel(build) for build in builds])
tag_info = session.getTag(tag_id, strict=True)
tag_name = tag_info['name']
start = time.time()
@ -857,24 +855,16 @@ def watch_builds(session, tag_id, builds, quiet=False, poll_interval=5, timeout=
if builds and repo and repo != last_repo:
if koji.util.checkForBuilds(session, tag_id, builds,
repo['create_event'], latest=True):
if not quiet:
print("Successfully waited %s for %s "
"to appear in the %s repo" %
(koji.util.duration(start), builds_str, tag_name))
return
return (True, "Successfully waited %s for %s to appear in the %s repo" %
(koji.util.duration(start), builds_str, tag_name))
if (time.time() - start >= timeout * 60.0):
if not quiet:
if builds:
raise koji.GenericError(
"Unsuccessfully waited %s for %s "
"to appear in the %s repo" %
if builds:
return (False, "Unsuccessfully waited %s for %s to appear in the %s repo" %
(koji.util.duration(start), builds_str, tag_name))
else:
raise koji.GenericError(
"Unsuccessfully waited %s for a new %s repo" %
else:
return (False, "Unsuccessfully waited %s for a new %s repo" %
(koji.util.duration(start), tag_name))
raise koji.GenericError()
time.sleep(poll_interval)
last_repo = repo
@ -882,10 +872,8 @@ def watch_builds(session, tag_id, builds, quiet=False, poll_interval=5, timeout=
if not builds:
if repo != last_repo:
if not quiet:
print("Successfully waited %s for a new %s repo" %
(koji.util.duration(start), tag_name))
return
return (True, "Successfully waited %s for a new %s repo" %
(koji.util.duration(start), tag_name))
def format_inheritance_flags(parent):

View file

@ -36,7 +36,7 @@ class TestWaitRepo(utils.CliTestCase):
self.options = mock.MagicMock()
self.options.quiet = True
self.options.poll_interval = 1 # second
self.options.poll_interval = 0.0001 # keep it fast
self.options.weburl = 'https://localhost.local'
self.session = mock.MagicMock()