import_layers: switch to argparse

No functionality changes, just a code upgrade.

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
Paul Eggleton 2019-08-06 10:50:13 +12:00
parent 712ed71c63
commit 577e62e62f

View File

@ -10,7 +10,7 @@
import sys import sys
import os import os
import optparse import argparse
import re import re
import glob import glob
import logging import logging
@ -41,36 +41,27 @@ def datetime_hook(jsdict):
def main(): def main():
valid_layer_name = re.compile('[-\w]+$') parser = argparse.ArgumentParser(description="Layer index import utility. Imports layer information from another layer index instance using the REST API. WARNING: this will overwrite data in your database, use with caution!")
parser.add_argument('url', help='Layer index URL to fetch from')
parser.add_argument('-n', '--dry-run', action='store_true', help="Don't write any data back to the database")
parser.add_argument('-d', '--debug', action='store_true', help='Enable debug output')
parser.add_argument('-q', '--quiet', action='store_true', help='Hide all output except error messages')
parser = optparse.OptionParser( args = parser.parse_args()
usage = """
%prog [options] <url>""")
parser.add_option("-n", "--dry-run", if args.debug:
help = "Don't write any data back to the database", loglevel = logging.DEBUG
action="store_true", dest="dryrun") elif args.quiet:
parser.add_option("-d", "--debug", loglevel = logging.WARNING
help = "Enable debug output", else:
action="store_const", const=logging.DEBUG, dest="loglevel", default=logging.INFO) loglevel = logging.INFO
parser.add_option("-q", "--quiet",
help = "Hide all output except error messages",
action="store_const", const=logging.ERROR, dest="loglevel")
options, args = parser.parse_args(sys.argv)
if len(args) < 2:
print("Please specify URL of the layer index")
sys.exit(1)
layerindex_url = args[1]
utils.setup_django() utils.setup_django()
import settings import settings
from layerindex.models import Branch, LayerItem, LayerBranch, LayerDependency, LayerMaintainer, LayerNote, Recipe, Source, Patch, PackageConfig, StaticBuildDep, DynamicBuildDep, RecipeFileDependency, Machine, Distro, BBClass, BBAppend, IncFile from layerindex.models import Branch, LayerItem, LayerBranch, LayerDependency, LayerMaintainer, LayerNote, Recipe, Source, Patch, PackageConfig, StaticBuildDep, DynamicBuildDep, RecipeFileDependency, Machine, Distro, BBClass, BBAppend, IncFile
from django.db import transaction from django.db import transaction
logger.setLevel(options.loglevel) logger.setLevel(loglevel)
fetchdir = settings.LAYER_FETCH_DIR fetchdir = settings.LAYER_FETCH_DIR
if not fetchdir: if not fetchdir:
@ -80,6 +71,7 @@ def main():
if not os.path.exists(fetchdir): if not os.path.exists(fetchdir):
os.makedirs(fetchdir) os.makedirs(fetchdir)
layerindex_url = args.url
if not layerindex_url.endswith('/'): if not layerindex_url.endswith('/'):
layerindex_url += '/' layerindex_url += '/'
if not '/layerindex/api/' in layerindex_url: if not '/layerindex/api/' in layerindex_url:
@ -431,7 +423,7 @@ def main():
layer_idmap, layer_idmap,
'layer') 'layer')
if options.dryrun: if args.dry_run:
raise DryRunRollbackException() raise DryRunRollbackException()
except DryRunRollbackException: except DryRunRollbackException:
pass pass