Merge "Reduce code duplication"

This commit is contained in:
Jenkins
2016-08-12 09:47:55 +00:00
committed by Gerrit Code Review

View File

@@ -181,7 +181,8 @@ def _parse_add_values(argvish):
return parsed_devs return parsed_devs
def _set_weight_values(devs, weight, opts): def check_devs(devs, input_question, opts, abort_msg):
if not devs: if not devs:
print('Search value matched 0 devices.\n' print('Search value matched 0 devices.\n'
'The on-disk ring builder is unchanged.') 'The on-disk ring builder is unchanged.')
@@ -191,12 +192,18 @@ def _set_weight_values(devs, weight, opts):
print('Matched more than one device:') print('Matched more than one device:')
for dev in devs: for dev in devs:
print(' %s' % format_device(dev)) print(' %s' % format_device(dev))
if not opts.yes and \ if not opts.yes and input(input_question) != 'y':
input('Are you sure you want to update the weight for ' print(abort_msg)
'these %s devices? (y/N) ' % len(devs)) != 'y':
print('Aborting device modifications')
exit(EXIT_ERROR) exit(EXIT_ERROR)
def _set_weight_values(devs, weight, opts):
input_question = 'Are you sure you want to update the weight for these ' \
'%s devices? (y/N) ' % len(devs)
abort_msg = 'Aborting device modifications'
check_devs(devs, input_question, opts, abort_msg)
for dev in devs: for dev in devs:
builder.set_dev_weight(dev['id'], weight) builder.set_dev_weight(dev['id'], weight)
print('%s weight set to %s' % (format_device(dev), print('%s weight set to %s' % (format_device(dev),
@@ -240,20 +247,10 @@ def _parse_set_weight_values(argvish):
def _set_info_values(devs, change, opts): def _set_info_values(devs, change, opts):
if not devs: input_question = 'Are you sure you want to update the info for these ' \
print("Search value matched 0 devices.\n" '%s devices? (y/N) ' % len(devs)
"The on-disk ring builder is unchanged.") abort_msg = 'Aborting device modifications'
exit(EXIT_ERROR) check_devs(devs, input_question, opts, abort_msg)
if len(devs) > 1:
print('Matched more than one device:')
for dev in devs:
print(' %s' % format_device(dev))
if not opts.yes and \
input('Are you sure you want to update the info for '
'these %s devices? (y/N) ' % len(devs)) != 'y':
print('Aborting device modifications')
exit(EXIT_ERROR)
for dev in devs: for dev in devs:
orig_dev_string = format_device(dev) orig_dev_string = format_device(dev)
@@ -791,20 +788,10 @@ swift-ring-builder <builder_file> remove
devs, opts = _parse_remove_values(argv[3:]) devs, opts = _parse_remove_values(argv[3:])
if not devs: input_question = 'Are you sure you want to remove these ' \
print('Search value matched 0 devices.\n' '%s devices? (y/N) ' % len(devs)
'The on-disk ring builder is unchanged.') abort_msg = 'Aborting device removals'
exit(EXIT_ERROR) check_devs(devs, input_question, opts, abort_msg)
if len(devs) > 1:
print('Matched more than one device:')
for dev in devs:
print(' %s' % format_device(dev))
if not opts.yes and \
input('Are you sure you want to remove these %s '
'devices? (y/N) ' % len(devs)) != 'y':
print('Aborting device removals')
exit(EXIT_ERROR)
for dev in devs: for dev in devs:
try: try: