-
- usage = """%prog [options] --add [--cdrom] cdrom_id description mirror_id uri_suffix
- %prog [options] --add --mirror mirror_id uri_prefix
-
- %prog [options] --update [short_name1 [short_name2 ...]]
- %prog [options] --reap"""
-
- parser = op.OptionParser(usage=usage)
- parser.set_defaults(verbosity=0,
- item='cdrom')
-
- parser.add_option('-a', '--add', action='store_const',
- dest='action', const='add',
- help='Add a new item to the database')
-
- parser.add_option('-u', '--update', action='store_const',
- dest='action', const='update',
- help='Update all cdrom images in the database with the latest version')
- parser.add_option('-r', '--reap', action='store_const',
- dest='action', const='reap',
- help='Reap stale cdrom images that are no longer in use')
-
- a_group = op.OptionGroup(parser, 'Adding new items')
- a_group.add_option('-c', '--cdrom', action='store_const',
- dest='item', const='cdrom',
- help='Add a new cdrom to the database')
- a_group.add_option('-m', '--mirror', action='store_const',
- dest='item', const='mirror',
- help='Add a new mirror to the database')
- parser.add_option_group(a_group)
-
- v_group = op.OptionGroup(parser, "Verbosity levels")
- v_group.add_option("-q", "--quiet", action='store_const',
- dest='verbosity', const=0,
- help='Show no output from commands this script runs (default)')
- v_group.add_option("-v", "--verbose", action='store_const',
- dest='verbosity', const=1,
- help='Show only errors from commands this script runs')
- v_group.add_option("--noisy", action='store_const',
- dest='verbosity', const=2,
- help='Show all output from commands this script runs')
- parser.add_option_group(v_group)
-
- (options, args) = parser.parse_args()
- verbosity = options.verbosity
- if options.action is None:
- print parser.format_help()
- elif options.action == 'add':
- if options.item == 'cdrom':
- attrs = dict(zip(('cdrom_id', 'description', 'mirror_id', 'uri_suffix'),
- args))
- cdrom = database.CDROM(**attrs)
- database.session.save(cdrom)
- database.session.flush()
-
- load_image(cdrom)
-
- elif options.item == 'mirror':
- attrs = dict(zip(('mirror_id', 'uri_prefix'),
- args))
- mirror = database.Mirror(**attrs)
- database.session.save(mirror)
- database.session.flush()
- elif options.action == 'update':
- if len(args) > 0:
- images = [database.CDROM.query().get(arg) for arg in args]
- else:
- images = database.CDROM.query().all()
- for cdrom in images:
- if cdrom is not None:
- load_image(cdrom)
- elif options.action == 'reap':
- reap_images()
+ database.session.begin()
+
+ parser = argparse.ArgumentParser(description='Perform actions on the CD-ROM images in the database')
+ subparsers = parser.add_subparsers(help='Action to perform')
+
+ add_parser = subparsers.add_parser('add', help='Add new image to database')
+ add_parser.set_defaults(func=lambda args: add_parser.print_help())
+ add_subparsers = add_parser.add_subparsers()
+
+ add_cdrom_parser = add_subparsers.add_parser('cdrom')
+ add_cdrom_parser.add_argument('cdrom_id')
+ add_cdrom_parser.add_argument('description')
+ add_cdrom_parser.add_argument('mirror_id')
+ add_cdrom_parser.add_argument('uri_suffix')
+ add_cdrom_parser.set_defaults(func=action_add_cdrom)
+
+ add_mirror_parser = add_subparsers.add_parser('mirror')
+ add_mirror_parser.add_argument('mirror_id')
+ add_mirror_parser.add_argument('uri_prefix')
+ add_mirror_parser.set_defaults(func=action_add_mirror)
+
+ update_parser = subparsers.add_parser('update', help='Update images in database')
+ update_parser.add_argument('names', nargs='*', metavar='name', help='Shortnames of images to update')
+ update_parser.set_defaults(func=action_update)
+
+ reap_parser = subparsers.add_parser('reap', help='Reap old images in database')
+ reap_parser.set_defaults(func=action_reap)
+
+ args = parser.parse_args()
+ if 'func' in args:
+ args.func(args)
+ else:
+ parser.print_help()
+