From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id A92D7A00E6 for ; Tue, 6 Aug 2019 09:29:21 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id A3506F04; Tue, 6 Aug 2019 09:29:21 +0200 (CEST) Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by dpdk.org (Postfix) with ESMTP id 02838F04 for ; Tue, 6 Aug 2019 09:29:19 +0200 (CEST) Received: by mail-pf1-f170.google.com with SMTP id u14so41035475pfn.2 for ; Tue, 06 Aug 2019 00:29:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=BqQTS3SQy6zo0UBIv0MSF3a495wMA/yeimjYj1CksJ4=; b=Wx5ThUW4FbH/uDxCGZS4YPAZU80wLA7lWc4vTsSyadCEG4XpOhtUzldLdddr8R3dpm wHSZfFQy84XJOXVlAYXyOU+SP9oARbtzNFObMxv7iho7ZztwC6+OtlM3cgQ9SkXD3Lqo PesBsnwUfFm41DIPXOoQjJhZAZ/yC3qXYjVve2MIVQk4YcndKU8hKTI8RIDbzsv7SE/z 3UKZcnIULEUbzYFwICzH+uN84FOjGvjN9kyccYeKlAX+ZUTYjMhzE83AdHdCifvfGIpy BqLquwfNH4yseGPOTXXvCQRDOWWfgZojylqw5Xn9XyhWbCwTHRtAFN8rHlwQUE/mm6e9 kD7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=BqQTS3SQy6zo0UBIv0MSF3a495wMA/yeimjYj1CksJ4=; b=DaXyx+fumbIbF54+HP/M0gHqEdWWRBRttMfAb3B6PVEM+ioBZMloZ2MmAVjIKTuPul OYeba9W5oeRY0rPzFWdRUV/SLNZf7iTkTu/RFXXfI0u1PkxZD7cHjQe4hx6TstspVt9o HFdjt0xBP+0xJSvCH5HxiK6Da4p6CLiStjzmRIxFV4pgmJLAMMo7PbY6jVFtalI0QRrl ku8Gd/yrp0kq3EZELDVtpR11BnRfI6/MBPQCeQu6Z/gfDod0Zdt7ZhCfQK04Q+MgGXnb 8xwjG8bhJ8SX7s2Si1W0h8DNI/kWlHk6WiQGguBN3LOuvf/WOHqbB22fdwbuVdYb8TCf F3ww== X-Gm-Message-State: APjAAAVrG1fu4uKMBuAxMwITRLLsvTSCVl5UJpJnHn6sHbBXQrKQabYW PU4CQIr6L+/lK1tGfz6zS0P+wMfp X-Google-Smtp-Source: APXvYqyYGlH9jdax/ke4tHUpKzIQbA5crmkAVyh7hrZf2oGQ8Tckn8kmmAK+bim3wpG9LJpF+OYV+g== X-Received: by 2002:a63:3112:: with SMTP id x18mr1769286pgx.385.1565076559088; Tue, 06 Aug 2019 00:29:19 -0700 (PDT) Received: from localhost.localdomain ([192.47.164.146]) by smtp.gmail.com with ESMTPSA id g2sm105154073pfb.95.2019.08.06.00.29.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Aug 2019 00:29:18 -0700 (PDT) From: yasufum.o@gmail.com To: spp@dpdk.org, ferruh.yigit@intel.com, yasufum.o@gmail.com Date: Tue, 6 Aug 2019 16:29:14 +0900 Message-Id: <20190806072914.11642-1-yasufum.o@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [spp] [PATCH] cli: add config option X-BeenThere: spp@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Soft Patch Panel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: spp-bounces@dpdk.org Sender: "spp" From: Yasufumi Ogawa SPP CLI has a default config file as `src/cli/config/defautl.yml`. It might be customized and rename other than default. This update is to add `--config` option for loading customized config instead of default one. $ python3 src/spp.py --config /path/to/your/config.yml Signed-off-by: Yasufumi Ogawa --- src/cli/shell.py | 79 +++++++++++++++++++++++++----------------------- src/cli/spp.py | 5 ++- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/cli/shell.py b/src/cli/shell.py index 2288e1d..989f071 100644 --- a/src/cli/shell.py +++ b/src/cli/shell.py @@ -24,39 +24,44 @@ import yaml class Shell(cmd.Cmd, object): """SPP command prompt.""" - # Load default config, can be changed via `config` command - # TODO(yasufum) enable to give config file from option - try: - config_file = "{}/config/default.yml".format( - os.path.dirname(__file__)) - cli_config = yaml.load(open(config_file), - Loader=yaml.FullLoader) - except IOError as e: - print('Error: no config file found!') - print(e) - exit() - - hist_file = os.path.expanduser('~/.spp_history') - PLUGIN_DIR = 'plugins' - - # Commands not included in history - HIST_EXCEPT = ['bye', 'exit', 'history', 'redo'] - - # Shell settings which are reserved vars of Cmd class. - # `intro` is to be shown as a welcome message. - intro = 'Welcome to the SPP CLI. Type `help` or `?` to list commands.\n' - prompt = cli_config['prompt']['val'] # command prompt - - # Recipe file to be recorded with `record` command - recorded_file = None - - # setup history file - if os.path.exists(hist_file): - readline.read_history_file(hist_file) - else: - readline.write_history_file(hist_file) - - def __init__(self, spp_cli_objs, use_cache=False): + def __init__(self, spp_cli_objs, config, use_cache=False): + + # Load default config, can be changed via `config` command + try: + if config is not None: + config_path = "{}/{}".format( + os.getcwd(), config) + else: + config_path = "{}/config/default.yml".format( + os.path.dirname(__file__)) + + self.cli_config = yaml.load(open(config_path), + Loader=yaml.FullLoader) + except IOError as e: + print('Error: No config file found!') + print(e) + exit() + + self.hist_file = os.path.expanduser('~/.spp_history') + self.plugin_dir = 'plugins' + + # Commands not included in history + self.hist_except = ['bye', 'exit', 'history', 'redo'] + + # Shell settings which are reserved vars of Cmd class. + # `intro` is to be shown as a welcome message. + self.intro = 'Welcome to the SPP CLI. Type `help` or `?` to list commands.\n' + self.prompt = self.cli_config['prompt']['val'] # command prompt + + # Recipe file to be recorded with `record` command + self.recorded_file = None + + # setup history file + if os.path.exists(self.hist_file): + readline.read_history_file(self.hist_file) + else: + readline.write_history_file(self.hist_file) + cmd.Cmd.__init__(self) self.spp_ctl_server = server.SppCtlServer(spp_cli_objs) self.spp_ctl_cli = spp_cli_objs[0] @@ -107,7 +112,7 @@ class Shell(cmd.Cmd, object): self.init_spp_procs() # TODO(yasufum) do not add to history if command is failed. - if line.strip().split(' ')[0] not in self.HIST_EXCEPT: + if line.strip().split(' ')[0] not in self.hist_except: readline.write_history_file(self.hist_file) return stop @@ -917,7 +922,7 @@ class Shell(cmd.Cmd, object): args = re.sub(r'\s+', ' ', args) list_args = args.split(' ') - libdir = self.PLUGIN_DIR + libdir = self.plugin_dir mod_name = list_args[0] method_name = 'do_%s' % mod_name exec('from .%s import %s' % (libdir, mod_name)) @@ -933,7 +938,7 @@ class Shell(cmd.Cmd, object): def complete_load_cmd(self, text, line, begidx, endidx): """Complete command plugins - Search under PLUGIN_DIR with compl_common() method. + Search under `plugin_dir` with compl_common() method. This method is intended to be used for searching current directory, but not in this case. If text is not '', compl_common() does not work correctly and do filtering @@ -942,7 +947,7 @@ class Shell(cmd.Cmd, object): curdir = os.path.dirname(__file__) res = common.compl_common( - '', '%s/%s' % (curdir, self.PLUGIN_DIR), 'py') + '', '%s/%s' % (curdir, self.plugin_dir), 'py') completions = [] for t in res: diff --git a/src/cli/spp.py b/src/cli/spp.py index 00ffd6f..8cf31eb 100644 --- a/src/cli/spp.py +++ b/src/cli/spp.py @@ -3,6 +3,7 @@ # Copyright(c) 2015-2016 Intel Corporation import argparse +import os import re from .shell import Shell from .shell_lib import common @@ -20,6 +21,8 @@ def main(argv): parser.add_argument('-b', '--bind-addr', action='append', default=['%s:%s' % (api_ipaddr, api_port)], help='bind address, default=127.0.0.1:7777') + parser.add_argument('--config', type=str, + help='Config file path') args = parser.parse_args() if len(args.bind_addr) > 1: @@ -48,7 +51,7 @@ def main(argv): spp_cli_objs.append(spp_ctl_cli) - shell = Shell(spp_cli_objs) + shell = Shell(spp_cli_objs, args.config) shell.cmdloop() shell = None -- 2.17.1