add bin/ryu

this new command takes a sub-command name as its first positional argument
and passes the rest of arguments to the corresponding sub-module.
for example, 'run' sub-command is an equivalent to bin/ryu-manager.

this commit does not replace the existing commands like ryu-manager.
this merely adds new ways to invoke the functionalities.

Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
YAMAMOTO Takashi 2014-03-26 15:20:08 +09:00 committed by FUJITA Tomonori
parent 7d1fcd7581
commit ce2f39a720
3 changed files with 96 additions and 0 deletions

20
bin/ryu Executable file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env python
# Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2014 YAMAMOTO Takashi <yamamoto at valinux co jp>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ryu.cmd.ryu_base import main
main()

75
ryu/cmd/ryu_base.py Normal file
View File

@ -0,0 +1,75 @@
# Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2014 YAMAMOTO Takashi <yamamoto at valinux co jp>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ryu.contrib
from ryu import cfg
from ryu import utils
from ryu import version
import argparse
import os.path
import sys
subcommands = {
'run': 'ryu.cmd.manager',
'of-config-cli': 'ryu.cmd.of_config_cli',
'rpc-cli': 'ryu.cmd.rpc_cli',
}
class RemainderOpt(cfg.MultiStrOpt):
def _get_argparse_kwargs(self, group, **kwargs):
kwargs = cfg.MultiStrOpt._get_argparse_kwargs(self, group, **kwargs)
kwargs['nargs'] = argparse.REMAINDER
return kwargs
base_conf = cfg.ConfigOpts()
base_conf.register_cli_opt(cfg.StrOpt('subcommand', positional=True,
required=True,
help='[%s]' % '|'.join(
subcommands.keys())))
base_conf.register_cli_opt(RemainderOpt('subcommand_args', default=[],
positional=True,
help='subcommand specific arguments'))
class SubCommand(object):
def __init__(self, name, entry):
self.name = name
self.entry = entry
def run(self, args):
prog = '%s %s' % (os.path.basename(sys.argv[0]), self.name,)
self.entry(args=args, prog=prog)
def main():
try:
base_conf(project='ryu', version='ryu %s' % version)
except cfg.RequiredOptError, e:
base_conf.print_help()
raise SystemExit(1)
subcmd_name = base_conf.subcommand
try:
subcmd_mod_name = subcommands[subcmd_name]
except KeyError:
base_conf.print_help()
raise SystemExit('Unknown subcommand %s' % subcmd_name)
subcmd_mod = utils.import_module(subcmd_mod_name)
subcmd = SubCommand(name=subcmd_name, entry=subcmd_mod.main)
subcmd.run(base_conf.subcommand_args)

View File

@ -48,4 +48,5 @@ setup-hooks =
[entry_points]
console_scripts =
ryu-manager = ryu.cmd.manager:main
ryu = ryu.cmd.ryu_base:main
rpc-cli = ryu.cmd.rpc_cli:main