From afe1746319f847f4e614f2d7fc9c215f907fcae2 Mon Sep 17 00:00:00 2001 From: Stanislav Ochotnicky Date: Nov 25 2013 14:03:42 +0000 Subject: [download-scratch] Refactor into a function and add options - Move functionality into a separate function that can be imported if needed - Add optional --huburl and --baseurl for defining alternative koji instances for downloads (rhbz#1027616) --- diff --git a/koji-download-scratch b/koji-download-scratch index eac0c28..0e29a1a 100755 --- a/koji-download-scratch +++ b/koji-download-scratch @@ -11,63 +11,71 @@ import urlgrabber.progress import optparse BASEURL = 'http://kojipkgs.fedoraproject.org/work/' +HUBURL = 'http://koji.fedoraproject.org/kojihub' -usage = 'usage: %prog [options] task-ID [task-ID...]' -parser = optparse.OptionParser(usage=usage) -parser.add_option('--arch', action='append', dest='arches', metavar='ARCH', default=[], - help='Only download packages of the given arch (may be specified multiple times)') -opts, args = parser.parse_args() -if not args: - parser.error('At least one task ID must be specified') -session = koji.ClientSession('http://koji.fedoraproject.org/kojihub') +def download_scratch_rpms(task_ids, arches=[], koji_session=None, koji_baseurl=None): + if not os.access(os.getcwd(), os.R_OK | os.W_OK | os.X_OK): + raise IOError("Insufficient permissons for current directory. Aborting download") -for task_id in args: - if not task_id.isdigit(): - parser.error('%s is not an integer task ID' % task_id) + for task_id in task_ids: + task = session.getTaskInfo(task_id, request=True) + if not task: + parser.error('Invalid task ID: %i' % task_id) + elif task['state'] in (koji.TASK_STATES['FREE'], koji.TASK_STATES['OPEN']): + parser.error('Task %i has not completed' % task['id']) + elif task['state'] != koji.TASK_STATES['CLOSED']: + parser.error('Task %i did not complete successfully' % task['id']) - if not os.access(os.getcwd(), os.R_OK | os.W_OK | os.X_OK): - print("Insufficient permissons for current directory. Aborting download") - sys.exit(1) + if task['method'] == 'build': + print 'Getting rpms from children of task %i: %s' % (task['id'], koji.taskLabel(task)) + tasks = session.listTasks(opts={'parent': task_id, 'method': 'buildArch', 'state': [koji.TASK_STATES['CLOSED']], + 'decode': True}) + elif task['method'] == 'buildArch': + tasks = [task] + else: + parser.error('Task %i is not a build or buildArch task' % task['id']) - task_id = int(task_id) - task = session.getTaskInfo(task_id, request=True) - if not task: - parser.error('Invalid task ID: %i' % task_id) - elif task['state'] in (koji.TASK_STATES['FREE'], koji.TASK_STATES['OPEN']): - parser.error('Task %i has not completed' % task['id']) - elif task['state'] != koji.TASK_STATES['CLOSED']: - parser.error('Task %i did not complete successfully' % task['id']) + prog_meter = urlgrabber.progress.TextMeter() - if task['method'] == 'build': - print 'Getting rpms from children of task %i: %s' % (task['id'], koji.taskLabel(task)) - tasks = session.listTasks(opts={'parent': task_id, 'method': 'buildArch', 'state': [koji.TASK_STATES['CLOSED']], - 'decode': True}) - elif task['method'] == 'buildArch': - tasks = [task] - else: - parser.error('Task %i is not a build or buildArch task' % task['id']) + for task in tasks: + if arches: + print 'Downloading %s rpms from task %i: %s' % (', '.join(arches), task['id'], koji.taskLabel(task)) + else: + print 'Downloading rpms from task %i: %s' % (task['id'], koji.taskLabel(task)) - prog_meter = urlgrabber.progress.TextMeter() + base_path = koji.pathinfo.taskrelpath(task['id']) + output = session.listTaskOutput(task['id']) + if output == []: + print "This build is empty, no files to download" + sys.exit(1) + for filename in output: + if filename.endswith('.rpm') and arches: + arch = filename.rsplit('.', 3)[2] + download = arch in arches + else: + download = True + if download: + urlgrabber.grabber.urlgrab(BASEURL + base_path + '/' + filename, + progress_obj=prog_meter) - for task in tasks: - if opts.arches: - print 'Downloading %s rpms from task %i: %s' % (', '.join(opts.arches), task['id'], koji.taskLabel(task)) - else: - print 'Downloading rpms from task %i: %s' % (task['id'], koji.taskLabel(task)) +if __name__ == '__main__': + usage = 'usage: %prog [options] task-ID [task-ID...]' + parser = optparse.OptionParser(usage=usage) + parser.add_option('--arch', action='append', dest='arches', metavar='ARCH', default=[], + help='Only download packages of the given arch (may be specified multiple times)') + parser.add_option('--huburl', '-u', default=HUBURL, + help='URL of Koji hub (default: %default)') + parser.add_option('--baseurl', '-b', default=BASEURL, + help='Base URL for downloading RPMs (default: %default)') - base_path = koji.pathinfo.taskrelpath(task['id']) - output = session.listTaskOutput(task['id']) - if output == []: - print "This build is empty, no files to download" - sys.exit(1) - for filename in output: - if filename.endswith('.rpm') and opts.arches: - arch = filename.rsplit('.', 3)[2] - download = arch in opts.arches - else: - download = True - if download: - urlgrabber.grabber.urlgrab(BASEURL + base_path + '/' + filename, - progress_obj=prog_meter) + opts, args = parser.parse_args() + if not args: + parser.error('At least one task ID must be specified') + for task_id in args: + if not task_id.isdigit(): + parser.error('%s is not an integer task ID' % task_id) + + session = koji.ClientSession(opts.huburl) + download_scratch_rpms([int(tid) for tid in args], opts.arches, session, opts.baseurl)