Skip to content

Commit a583724

Browse files
committed
attachments: enable request by a bug ID and more features
1 parent 7981897 commit a583724

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

bugz/cli.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -438,33 +438,55 @@ def attach(settings):
438438

439439

440440
def attachment(settings):
441-
""" Download or view an attachment given the id."""
442-
log_info('Getting attachment %s' % settings.attachid)
441+
""" Download or view an attachment(s) given the attachment or bug id."""
443442

444443
params = {}
445-
params['attachment_ids'] = [settings.attachid]
444+
if hasattr(settings, 'bug'):
445+
params['ids'] = [settings.id]
446+
log_info('Getting attachment(s) for bug %s' % settings.id)
447+
else:
448+
params['attachment_ids'] = [settings.id]
449+
log_info('Getting attachment %s' % settings.id)
446450

447451
check_auth(settings)
452+
results = settings.call_bz(settings.bz.Bug.attachments, params)
448453

449-
result = settings.call_bz(settings.bz.Bug.attachments, params)
450-
result = result['attachments'][settings.attachid]
451-
view = hasattr(settings, 'view')
454+
if hasattr(settings, 'bug'):
455+
results = results['bugs'][settings.id]
456+
else:
457+
results = [ results['attachments'][settings.id] ]
458+
459+
if hasattr(settings, 'patch_only'):
460+
results = list(filter(lambda x : x['is_patch'], results))
461+
462+
if hasattr(settings, 'skip_obsolete'):
463+
results = list(filter(lambda x : not x['is_obsolete'], results))
464+
465+
if not results:
466+
return
467+
468+
if hasattr(settings, 'most_recent'):
469+
results = [ results[-1] ]
452470

471+
view = hasattr(settings, 'view')
453472
action = {True: 'Viewing', False: 'Saving'}
454-
log_info('%s attachment: "%s"' %
455-
(action[view], result['file_name']))
456-
safe_filename = os.path.basename(re.sub(r'\.\.', '',
473+
474+
for result in results:
475+
log_info('%s%s attachment: "%s"' % (action[view],
476+
' obsolete' if result['is_obsolete'] else '',
477+
result['file_name']))
478+
safe_filename = os.path.basename(re.sub(r'\.\.', '',
457479
result['file_name']))
458480

459-
if view:
460-
print(result['data'].data.decode('utf-8'))
461-
else:
462-
if os.path.exists(result['file_name']):
463-
raise RuntimeError('Filename already exists')
481+
if view:
482+
print(result['data'].data.decode('utf-8'))
483+
else:
484+
if os.path.exists(result['file_name']):
485+
raise RuntimeError('Filename already exists')
464486

465-
fd = open(safe_filename, 'wb')
466-
fd.write(result['data'].data)
467-
fd.close()
487+
fd = open(safe_filename, 'wb')
488+
fd.write(result['data'].data)
489+
fd.close()
468490

469491

470492
def get(settings):

bugz/cli_argparser.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,25 @@ def make_arg_parser():
8080

8181
attachment_parser = subparsers.add_parser('attachment',
8282
argument_default=argparse.SUPPRESS,
83-
help='get an attachment '
83+
help='get an attachment(s) '
8484
'from Bugzilla')
85-
attachment_parser.add_argument('attachid',
86-
help='the ID of the attachment')
85+
attachment_parser.add_argument('id',
86+
help='the ID of the attachment or bug')
87+
attachment_parser.add_argument('-b', '--bug',
88+
action='store_true',
89+
help='the ID is a bug')
90+
attachment_parser.add_argument('-r', '--most-recent',
91+
action='store_true',
92+
help='get only most recent attachment')
93+
attachment_parser.add_argument('-p', '--patch-only',
94+
action='store_true',
95+
help='get only patch attachment(s)')
96+
attachment_parser.add_argument('-o', '--skip-obsolete',
97+
action='store_true',
98+
help='get only not obsolete attachment(s)')
8799
attachment_parser.add_argument('-v', '--view',
88100
action="store_true",
89-
help='print attachment rather than save')
101+
help='print attachment(s) rather than save')
90102
attachment_parser.set_defaults(func=bugz.cli.attachment)
91103

92104
connections_parser = subparsers.add_parser('connections',

0 commit comments

Comments
 (0)