unify logic between the waitrepo task and koji wait-repo

This commit is contained in:
Mike Bonnet 2008-05-16 12:34:36 -04:00
parent 20daea06f2
commit 2b28053397
4 changed files with 96 additions and 68 deletions

View file

@ -1419,6 +1419,7 @@ class ChainBuildTask(BaseTaskHandler):
for build_level in srcs:
subtasks = []
build_tasks = []
nvrs = []
for src in build_level:
if SCM.is_scm_url(src):
task_id = session.host.subtask(method='build',
@ -1426,11 +1427,14 @@ class ChainBuildTask(BaseTaskHandler):
label=src,
parent=self.id)
build_tasks.append(task_id)
subtasks.append(task_id)
else:
task_id = session.host.subtask(method='waitrepo',
arglist=[target, None, src],
label=src,
parent=self.id)
nvrs.append(src)
if nvrs:
task_id = session.host.subtask(method='waitrepo',
arglist=[target, None, nvrs],
label=','.join(nvrs),
parent=self.id)
subtasks.append(task_id)
if not subtasks:
continue
@ -2255,42 +2259,47 @@ class WaitrepoTask(BaseTaskHandler):
_taskWeight = 0.2
PAUSE = 60
# time in seconds before we fail this task
TIMEOUT = 7200
def checkForNVR(self, tag, nvrs, repo_creation_event):
if not isinstance(nvrs, list):
nvrs = [nvrs]
nvr_list = [build['nvr'] for build in session.listTagged(tag, event=repo_creation_event, inherit=True)]
if not nvr_list:
return False
for nvr in nvrs:
if not nvr in nvr_list:
return False
return True
def handler(self, build_target_info, newer_then=None, nvr=None):
# time in minutes before we fail this task
TIMEOUT = 120
def handler(self, build_target_info, newer_then=None, nvrs=None):
start = time.time()
build_target = session.getBuildTarget(build_target_info)
if not build_target:
raise koji.GenericError("Error: Invalid BuildTarget: %s" % build_target_info)
raise koji.GenericError, "invalid build target: %s" % build_target_info
repo = None
last_repo = None
if not newer_then and not nvr:
if not nvrs:
nvrs = []
builds = [koji.parse_NVR(nvr) for nvr in nvrs]
if not newer_then and not builds:
newer_then = time.time()
last_repo = None
repo = session.getRepo(build_target['build_tag'])
while True:
repo = session.getRepo(build_target['build_tag_name'])
if repo and repo != last_repo:
if (not nvr or self.checkForNVR(build_target['dest_tag'], nvr, repo['create_event'])) and \
(not newer_then or repo['create_ts'] > newer_then):
break
if (time.time() - start) > self.TIMEOUT:
raise koji.GenericError("Waited %d seconds and still no repo meeting conditions, timing out" % self.TIMEOUT)
last_repo = repo
if builds and repo and repo != last_repo:
if koji.util.checkForBuilds(session, build_target['build_tag'], builds, repo['create_event']):
return "Successfully waited %s for %s to appear in the %s repo" % \
(koji.util.duration(start), koji.util.printList(nvrs), build_target['build_tag_name'])
elif newer_then:
if repo['create_ts'] > newer_then:
return "Successfully waited %s for a new %s repo" % \
(koji.util.duration(start), build_target['build_tag_name'])
if (time.time() - start) > (self.TIMEOUT * 60.0):
if builds:
raise koji.GenericError, "Unsuccessfully waited %s for %s to appear in the %s repo" % \
(koji.util.duration(start), koji.util.printList(nvrs), build_target['build_tag_name'])
else:
raise koji.GenericError, "Unsuccessfully waited %s for a new %s repo" % \
(koji.util.duration(start), build_target['build_tag_name'])
time.sleep(self.PAUSE)
return "Successfully waited %s seconds for a %s repo (%s)" % (int(time.time() - start), build_target['build_tag_name'], repo['id'])
last_repo = repo
repo = session.getRepo(build_target['build_tag'])
class SCM(object):
"SCM abstraction class"

View file

@ -3858,31 +3858,14 @@ def anon_handle_wait_repo(options, session, args):
parser = OptionParser(usage=usage)
parser.add_option("--build", metavar="NVR", dest="builds", action="append", default=[],
help=_("Check that the given build (or a later build of the same package) is in the newly-generated repo (may be used multiple times)"))
parser.add_option("--target", action="store_true", help=_("Interpret the argument as a build target name"))
parser.add_option("--timeout", type="int", help=_("Amount of time to wait (in minutes) before giving up (default: 120)"), default=120)
parser.add_option("--quiet", action="store_true", help=_("Suppress output, success or failure will be indicated by the return value only"))
(suboptions, args) = parser.parse_args(args)
def _duration(s):
elapsed = time.time() - s
mins = int(elapsed / 60)
secs = int(elapsed % 60)
return '%s:%02i' % (mins, secs)
def _print_list(l):
if len(l) == 0:
return ''
elif len(l) == 1:
return l[0]
elif len(l) == 2:
return ' and '.join(l)
else:
ret = ', '.join(l[:-1])
ret += ', and '
ret += l[-1]
return ret
start = time.time()
builds = []
for build in suboptions.builds:
builds.append(koji.parse_NVR(build))
builds = [koji.parse_NVR(build) for build in suboptions.builds]
if len(args) < 1:
parser.error(_("Please specify a tag name"))
elif len(args) > 1:
@ -3890,42 +3873,44 @@ def anon_handle_wait_repo(options, session, args):
tag = args[0]
start = time.time()
if suboptions.target:
target_info = session.getBuildTarget(tag)
if not target_info:
parser.error("Invalid build target: %s" % tag)
tag = target_info['build_tag_name']
tag_id = target_info['build_tag']
else:
tag_info = session.getTag(tag)
if not tag_info:
parser.error("Invalid tag: %s" % tag)
tag_id = tag_info['id']
last_repo = None
repo = session.getRepo(tag)
repo = session.getRepo(tag_id)
while True:
if builds and repo and repo != last_repo:
# check if the current repo contains the NVR we're waiting for
for build in builds:
tagged_list = session.listTagged(tag, event=repo['create_event'], package=build['name'], inherit=True)
for tagged in tagged_list:
if tagged['version'] == build['version'] and tagged['release'] == build['release']:
break
else:
break
else:
if koji.util.checkForBuilds(session, tag_id, builds, repo['create_event']):
if not suboptions.quiet:
print "Successfully waited %s for %s to appear in the %s repo" % (_duration(start), _print_list(suboptions.builds), tag)
print "Successfully waited %s for %s to appear in the %s repo" % (koji.util.duration(start), koji.util.printList(suboptions.builds), tag)
return
time.sleep(60)
last_repo = repo
repo = session.getRepo(tag)
repo = session.getRepo(tag_id)
if not builds:
if repo != last_repo:
if not suboptions.quiet:
print "Successfully waited %s for a new %s repo" % (_duration(start), tag)
print "Successfully waited %s for a new %s repo" % (koji.util.duration(start), tag)
return
if (time.time() - start) > (suboptions.timeout * 60.0):
if not suboptions.quiet:
if builds:
print "Unsuccessfully waited %s for %s to appear in the %s repo" % (_duration(start), _print_list(suboptions.builds), tag)
print "Unsuccessfully waited %s for %s to appear in the %s repo" % (koji.util.duration(start), koji.util.printList(suboptions.builds), tag)
else:
print "Unsuccessfully waited %s for a new %s repo" % (_duration(start), tag)
print "Unsuccessfully waited %s for a new %s repo" % (koji.util.duration(start), tag)
return 1
def handle_help(options, session, args):

View file

@ -45,6 +45,7 @@ import traceback
import urllib
import urllib2
import urlparse
import util
import xmlrpclib
from xmlrpclib import loads, Fault
import ssl.XMLRPCServerProxy

View file

@ -31,3 +31,36 @@ def formatChangelog(entries):
""" % (_changelogDate(entry['date']), entry['author'], entry['text'])
return result
def checkForBuilds(session, tag, builds, event):
"""Check that the builds existed in tag at the time of the event."""
for build in builds:
tagged_list = session.listTagged(tag, event=event, package=build['name'], inherit=True)
for tagged in tagged_list:
if tagged['version'] == build['version'] and tagged['release'] == build['release']:
break
else:
return False
return True
def duration(start):
"""Return the duration between start and now in MM:SS format"""
elapsed = time.time() - start
mins = int(elapsed / 60)
secs = int(elapsed % 60)
return '%s:%02i' % (mins, secs)
def printList(l):
"""Print the contents of the list comma-separated"""
if len(l) == 0:
return ''
elif len(l) == 1:
return l[0]
elif len(l) == 2:
return ' and '.join(l)
else:
ret = ', '.join(l[:-1])
ret += ', and '
ret += l[-1]
return ret