Use oslo.config for CLI arguments
It seems weird to import oslo.config then use argparse to parse CLI arguments. Let's leverage our common libraries. I couldn't find a way to accept "-vvv" in oslo.config neatly, so I've changed 'verbose' to an IntOpt and updated autohelp-wrapper. I did look at adding this to oslo.config but exceeded the time I wanted to spend. Change-Id: I4e02257571f3349d492dfdd56a5746d1fa2ae9dd
This commit is contained in:
parent
68ca7ae76f
commit
db78ac44da
|
@ -121,9 +121,9 @@ while getopts :b:g:e:v:cfq opt; do
|
|||
QUIET=1
|
||||
;;
|
||||
v)
|
||||
AUTOOPT="-v"
|
||||
AUTOOPT="-v 1"
|
||||
if [ $OPTARG = 2 ]; then
|
||||
AUTOOPT="-vv"
|
||||
AUTOOPT="-v 2"
|
||||
fi
|
||||
;;
|
||||
\?)
|
||||
|
|
|
@ -17,19 +17,18 @@
|
|||
# For an example of usage, run this program with the -h switch.
|
||||
#
|
||||
|
||||
# Must import this before argparse
|
||||
from oslo_config import cfg
|
||||
|
||||
import argparse
|
||||
import importlib
|
||||
import os
|
||||
import pickle
|
||||
import re
|
||||
import sys
|
||||
|
||||
import jinja2
|
||||
from oslo_config import cfg
|
||||
import stevedore
|
||||
|
||||
import jinja2
|
||||
|
||||
try:
|
||||
from sqlalchemy import exc
|
||||
except Exception:
|
||||
|
@ -41,6 +40,29 @@ try:
|
|||
except ImportError:
|
||||
pass
|
||||
|
||||
_conf_description = 'Manage flag files, to aid in updating documentation.'
|
||||
_opts = [
|
||||
cfg.StrOpt('subcommand',
|
||||
required=True,
|
||||
choices=['update', 'rst', 'dump'],
|
||||
help='Action (update, rst, dump).'),
|
||||
cfg.StrOpt('package',
|
||||
required=True,
|
||||
help='Name of the top-level package.'),
|
||||
cfg.IntOpt('verbose', short='v',
|
||||
default=0),
|
||||
cfg.MultiStrOpt('input', short='i',
|
||||
required=True,
|
||||
dest='repos',
|
||||
help='Path to a python package in which options should '
|
||||
'be discovered. Can be used multiple times.',
|
||||
),
|
||||
cfg.StrOpt('output', short='o',
|
||||
dest='target',
|
||||
help='Directory or file in which data will be saved.\n'
|
||||
'Defaults to ../../doc/common/tables/ for "rst".\n'
|
||||
'Defaults to stdout for "dump"'),
|
||||
]
|
||||
|
||||
EXTENSIONS = ['oslo.cache',
|
||||
'oslo.concurrency',
|
||||
|
@ -565,41 +587,14 @@ def update_flagmappings(package_name, options, verbose=0):
|
|||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Manage flag files, to aid in updating documentation.',
|
||||
usage='%(prog)s <cmd> <package> [options]')
|
||||
parser.add_argument('subcommand',
|
||||
help='Action (update, rst, dump).',
|
||||
choices=['update', 'rst', 'dump'])
|
||||
parser.add_argument('package',
|
||||
help='Name of the top-level package.')
|
||||
parser.add_argument('-v', '--verbose',
|
||||
action='count',
|
||||
default=0,
|
||||
dest='verbose',
|
||||
required=False,)
|
||||
parser.add_argument('-i', '--input',
|
||||
dest='repos',
|
||||
help='Path to a python package in which options '
|
||||
'should be discoverd. Can be used multiple '
|
||||
'times.',
|
||||
required=False,
|
||||
type=str,
|
||||
action='append')
|
||||
parser.add_argument('-o', '--output',
|
||||
dest='target',
|
||||
help='Directory or file in which data will be saved.\n'
|
||||
'Defaults to ../../doc/common/tables/ '
|
||||
'for "rst".\n'
|
||||
'Defaults to stdout for "dump"',
|
||||
required=False,
|
||||
type=str,)
|
||||
args = parser.parse_args()
|
||||
conf = cfg.ConfigOpts()
|
||||
conf.register_cli_opts(_opts)
|
||||
conf(sys.argv[1:], description=_conf_description)
|
||||
|
||||
if args.repos is None:
|
||||
args.repos = ['./sources/%s/%s' % args.package]
|
||||
if conf.repos is None:
|
||||
conf.repos = ['./sources/%s/%s' % conf.package]
|
||||
|
||||
for repository in args.repos:
|
||||
for repository in conf.repos:
|
||||
package_name = os.path.basename(repository)
|
||||
base_path = os.path.dirname(repository)
|
||||
|
||||
|
@ -607,28 +602,28 @@ def main():
|
|||
try:
|
||||
__import__(package_name)
|
||||
except ImportError as e:
|
||||
if args.verbose >= 1:
|
||||
if conf.verbose >= 1:
|
||||
print(str(e))
|
||||
print("Failed to import: %s (%s)" % (package_name, e))
|
||||
|
||||
import_modules(base_path, package_name, verbose=args.verbose)
|
||||
import_modules(base_path, package_name, verbose=conf.verbose)
|
||||
sys.path.pop(0)
|
||||
|
||||
overrides = _get_overrides(package_name)
|
||||
options = OptionsCache(overrides, verbose=args.verbose)
|
||||
options.maybe_load_extensions(args.repos)
|
||||
options = OptionsCache(overrides, verbose=conf.verbose)
|
||||
options.maybe_load_extensions(conf.repos)
|
||||
|
||||
if args.verbose > 0:
|
||||
if conf.verbose > 0:
|
||||
print("%s options imported from package %s." % (len(options),
|
||||
str(package_name)))
|
||||
|
||||
if args.subcommand == 'update':
|
||||
update_flagmappings(args.package, options, verbose=args.verbose)
|
||||
if conf.subcommand == 'update':
|
||||
update_flagmappings(conf.package, options, verbose=conf.verbose)
|
||||
|
||||
elif args.subcommand == 'rst':
|
||||
write_files(args.package, options, args.target, args.subcommand)
|
||||
elif conf.subcommand == 'rst':
|
||||
write_files(conf.package, options, conf.target, conf.subcommand)
|
||||
|
||||
elif args.subcommand == 'dump':
|
||||
elif conf.subcommand == 'dump':
|
||||
options.dump()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue