Commit 4487b377 authored by Boris Glimcher's avatar Boris Glimcher Committed by Tomasz Zawadzki
Browse files

script/rpc: split parser creation from main function



this allows to re-use or even auto-generate parser object
and then main logic remains unchanged.

This also allows avoid nested functions.

More pythonic way or writing a package

Change-Id: Ia5a42b9f853d684df9d05e45cadf84fb2699c84b
Signed-off-by: default avatarBoris Glimcher <Boris.Glimcher@emc.com>
Reviewed-on: https://review.spdk.io/c/spdk/spdk/+/26115


Reviewed-by: default avatarKonrad Sztyber <ksztyber@nvidia.com>
Tested-by: default avatarSPDK Automated Test System <spdkbot@gmail.com>
Community-CI: Mellanox Build Bot
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
parent 3c3dbb47
Loading
Loading
Loading
Loading
+77 −63
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ def print_array(a):
    print(" ".join((quote(v) for v in a)))


def main():
def create_parser():
    parser = argparse.ArgumentParser(
        description='SPDK RPC command line interface', usage='%(prog)s [options]')
    parser.add_argument('-s', dest='server_addr',
@@ -3943,21 +3943,28 @@ Format: 'user:u1 secret:s1 muser:mu1 msecret:ms1,user:u2 secret:s2 muser:mu2 mse
    p.add_argument('-e', '--enable', help='Enable keyring_linux module', action='store_true')
    p.set_defaults(func=keyring_linux_set_options)

    return parser, subparsers


def check_called_name(name):
    if name in deprecated_aliases:
        print("{} is deprecated, use {} instead.".format(name, deprecated_aliases[name]), file=sys.stderr)


class dry_run_client:
    def call(self, method, params=None):
        print("Request:\n" + json.dumps({"method": method, "params": params}, indent=2))


def null_print(arg):
    pass


def call_rpc_func(args):
    args.func(args)
    check_called_name(args.called_rpc_name)


def execute_script(parser, client, timeout, fd):
    executed_rpc = ""
    for rpc_call in map(str.rstrip, fd):
@@ -3979,7 +3986,8 @@ Format: 'user:u1 secret:s1 muser:mu1 msecret:ms1,user:u2 secret:s2 muser:mu2 mse
            print(ex.message)
            exit(1)

    def load_plugin(args):

def load_plugin(args, subparsers, plugins):
    # Create temporary parser, pull out the plugin parameter, load the module, and then run the real argument parser
    plugin_parser = argparse.ArgumentParser(add_help=False)
    plugin_parser.add_argument('--plugin', dest='rpc_plugin', help='Module name of plugin with additional RPC commands')
@@ -4002,6 +4010,7 @@ Format: 'user:u1 secret:s1 muser:mu1 msecret:ms1,user:u2 secret:s2 muser:mu2 mse
        except ModuleNotFoundError:
            print("Module %s not found" % rpc_module)


def replace_arg_underscores(args):
    # All option names are defined with dashes only - for example: --tgt-name
    # But if user used underscores, convert them to dashes (--tgt_name => --tgt-name)
@@ -4013,8 +4022,13 @@ Format: 'user:u1 secret:s1 muser:mu1 msecret:ms1,user:u2 secret:s2 muser:mu2 mse
            opt, *vals = arg.split('=')
            args[i] = '='.join([opt.replace('_', '-'), *vals])


def main():

    parser, subparsers = create_parser()

    plugins = []
    load_plugin(None)
    load_plugin(None, subparsers, plugins)

    replace_arg_underscores(sys.argv)

@@ -4034,7 +4048,7 @@ Format: 'user:u1 secret:s1 muser:mu1 msecret:ms1,user:u2 secret:s2 muser:mu2 mse
            cmd = shlex.split(input)
            replace_arg_underscores(cmd)
            try:
                load_plugin(cmd)
                load_plugin(cmd, subparsers, plugins)
                tmp_args = parser.parse_args(cmd)
            except SystemExit as ex:
                print("**STATUS=1", flush=True)