update_classic_status: switch to argparse

I'm about to add a few command line options to this script, so switch
over to argparse before that which is more modern and a bit more
flexible.

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
Paul Eggleton 2019-09-04 11:19:48 +12:00
parent 3daa2349ae
commit 63446ed8de

View File

@ -8,11 +8,11 @@
# Licensed under the MIT license, see COPYING.MIT for details # Licensed under the MIT license, see COPYING.MIT for details
import sys import sys
import os.path import os
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))) sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..')))
import optparse import argparse
import re import re
import utils import utils
import logging import logging
@ -25,59 +25,55 @@ class DryRunRollbackException(Exception):
def main(): def main():
parser = optparse.OptionParser( parser = argparse.ArgumentParser(description='Comparison recipe cover status update tool')
usage = """
%prog [options]""")
parser.add_option("-b", "--branch", parser.add_argument('-b', '--branch',
help = "Specify branch to import into", default='oe-classic',
action="store", dest="branch", default='oe-classic') help='Specify branch to import into')
parser.add_option("-l", "--layer", parser.add_argument('-l', '--layer',
help = "Specify layer to import into", default='oe-classic',
action="store", dest="layer", default='oe-classic') help='Specify layer to import into')
parser.add_option("-u", "--update", parser.add_argument('-u', '--update',
help = "Specify update record to link to", help='Specify update record to link to')
action="store", dest="update") parser.add_argument('-n', '--dry-run',
parser.add_option("-n", "--dry-run", action='store_true',
help = "Don't write any data back to the database", help='Don\'t write any data back to the database')
action="store_true", dest="dryrun") parser.add_argument('-s', '--skip',
parser.add_option("-s", "--skip", help='Skip specified packages (comma-separated list, no spaces)')
help = "Skip specified packages (comma-separated list, no spaces)", parser.add_argument('-d', '--debug',
action="store", dest="skip") action='store_const', const=logging.DEBUG, dest='loglevel', default=logging.INFO,
parser.add_option("-d", "--debug", help='Enable debug output')
help = "Enable debug output", parser.add_argument('-q', '--quiet',
action="store_const", const=logging.DEBUG, dest="loglevel", default=logging.INFO) action='store_const', const=logging.ERROR, dest='loglevel',
parser.add_option("-q", "--quiet", help='Hide all output except error messages')
help = "Hide all output except error messages",
action="store_const", const=logging.ERROR, dest="loglevel")
options, args = parser.parse_args(sys.argv) args = parser.parse_args()
utils.setup_django() utils.setup_django()
from layerindex.models import LayerItem, LayerBranch, Recipe, ClassicRecipe, Update, ComparisonRecipeUpdate from layerindex.models import LayerItem, LayerBranch, Recipe, ClassicRecipe, Update, ComparisonRecipeUpdate
from django.db import transaction from django.db import transaction
logger.setLevel(options.loglevel) logger.setLevel(args.loglevel)
layer = LayerItem.objects.filter(name=options.layer).first() layer = LayerItem.objects.filter(name=args.layer).first()
if not layer: if not layer:
logger.error('Specified layer %s does not exist in database' % options.layer) logger.error('Specified layer %s does not exist in database' % args.layer)
sys.exit(1) sys.exit(1)
layerbranch = layer.get_layerbranch(options.branch) layerbranch = layer.get_layerbranch(args.branch)
if not layerbranch: if not layerbranch:
logger.error("Specified branch %s does not exist in database" % options.branch) logger.error("Specified branch %s does not exist in database" % args.branch)
sys.exit(1) sys.exit(1)
updateobj = None updateobj = None
if options.update: if args.update:
updateobj = Update.objects.filter(id=int(options.update)).first() updateobj = Update.objects.filter(id=int(args.update)).first()
if not updateobj: if not updateobj:
logger.error("Specified update id %s does not exist in database" % options.update) logger.error("Specified update id %s does not exist in database" % args.update)
sys.exit(1) sys.exit(1)
if options.skip: if args.skip:
skiplist = options.skip.split(',') skiplist = args.skip.split(',')
else: else:
skiplist = [] skiplist = []
try: try:
@ -240,7 +236,7 @@ def main():
rupdate.link_updated = True rupdate.link_updated = True
rupdate.save() rupdate.save()
if options.dryrun: if args.dry_run:
raise DryRunRollbackException() raise DryRunRollbackException()
except DryRunRollbackException: except DryRunRollbackException:
pass pass