This patch also updates it's callers.
# adding an interface to racoon.conf.
-import getopt
+import argparse
import glob
import logging
import logging.handlers
schema.tables = new_tables
-def usage():
- print "usage: %s [OPTIONS] DATABASE" % sys.argv[0]
- print "where DATABASE is a socket on which ovsdb-server is listening."
- ovs.daemon.usage()
- print """\
-Other options:
- --root-prefix=DIR Use DIR as alternate root directory (for testing).
- -h, --help Display this help message."""
- sys.exit(0)
-
-
def update_ipsec(ipsec, interfaces, new_interfaces):
for name, vals in interfaces.iteritems():
if name not in new_interfaces:
return None
-def main(argv):
- try:
- options, args = getopt.gnu_getopt(
- argv[1:], 'h', ['help', 'root-prefix='] + ovs.daemon.LONG_OPTIONS)
- except getopt.GetoptError, geo:
- sys.stderr.write("%s: %s\n" % (ovs.util.PROGRAM_NAME, geo.msg))
- sys.exit(1)
-
- for key, value in options:
- if key in ['-h', '--help']:
- usage()
- elif key == "--root-prefix":
- global root_prefix
- root_prefix = value
- elif not ovs.daemon.parse_opt(key, value):
- sys.stderr.write("%s: unhandled option %s\n"
- % (ovs.util.PROGRAM_NAME, key))
- sys.exit(1)
+def main():
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument("database", metavar="DATABASE",
+ help="A socket on which ovsdb-server is listening.")
+ parser.add_argument("--root-prefix", metavar="DIR",
+ help="Use DIR as alternate root directory"
+ " (for testing).")
- if len(args) != 1:
- sys.stderr.write("%s: exactly one nonoption argument is required "
- "(use --help for help)\n" % ovs.util.PROGRAM_NAME)
- sys.exit(1)
+ ovs.daemon.add_args(parser)
+ args = parser.parse_args()
+ ovs.daemon.handle_args(args)
- remote = args[0]
+ global root_prefix
+ root_prefix = args.root_prefix
+ remote = args.database
schema_file = "%s/vswitch.ovsschema" % ovs.dirs.PKGDATADIR
schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schema_file))
prune_schema(schema)
if __name__ == '__main__':
try:
- main(sys.argv)
+ main()
except SystemExit:
# Let system.exit() calls complete normally
raise
_fatal("%s: pidfile check failed (%s), aborting"
% (_pidfile, os.strerror(pid)))
-# XXX Python's getopt does not support options with optional arguments, so we
-# have to separate --pidfile (with no argument) from --pidfile-name (with an
-# argument). Need to write our own getopt I guess.
-LONG_OPTIONS = ["detach", "no-chdir", "pidfile", "pidfile-name=",
- "overwrite-pidfile", "monitor"]
+def add_args(parser):
+ """Populates 'parser', an ArgumentParser allocated using the argparse
+ module, with the command line arguments required by the daemon module."""
-def parse_opt(option, arg):
- if option == '--detach':
+ pidfile = make_pidfile_name(None)
+
+ group = parser.add_argument_group(title="Daemon Options")
+ group.add_argument("--detach", action="store_true",
+ help="Run in background as a daemon.")
+ group.add_argument("--no-chdir", action="store_true",
+ help="Do not chdir to '/'.")
+ group.add_argument("--monitor", action="store_true",
+ help="Monitor %s process." % ovs.util.PROGRAM_NAME)
+ group.add_argument("--pidfile", nargs="?", default=pidfile,
+ help="Create pidfile (default %s)." % pidfile)
+ group.add_argument("--overwrite-pidfile", action="store_true",
+ help="With --pidfile, start even if already running.")
+
+
+def handle_args(args):
+ """Handles daemon module settings in 'args'. 'args' is an object
+ containing values parsed by the parse_args() method of ArgumentParser. The
+ parent ArgumentParser should have been prepared by add_args() before
+ calling parse_args()."""
+
+ if args.detach:
set_detach()
- elif option == '--no-chdir':
+
+ if args.no_chdir:
set_no_chdir()
- elif option == '--pidfile':
- set_pidfile(None)
- elif option == '--pidfile-name':
- set_pidfile(arg)
- elif option == '--overwrite-pidfile':
+
+ if args.pidfile:
+ set_pidfile(args.pidfile)
+
+ if args.overwrite_pidfile:
ignore_existing_pidfile()
- elif option == '--monitor':
+
+ if args.monitor:
set_monitor()
- else:
- return False
- return True
AT_CAPTURE_FILE([expected])
# Start the daemon and wait for the pidfile to get created
# and that its contents are the correct pid.
-AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid& echo $! > expected], [0])
+AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile=`pwd`/pid& echo $! > expected], [0])
OVS_WAIT_UNTIL([test -s pid], [kill `cat expected`])
AT_CHECK(
[pid=`cat pid` && expected=`cat expected` && test "$pid" = "$expected"],
AT_CAPTURE_FILE([parentpid])
AT_CAPTURE_FILE([newpid])
# Start the daemon and wait for the pidfile to get created.
-AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --monitor& echo $! > parent], [0])
+AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile=`pwd`/pid --monitor& echo $! > parent], [0])
OVS_WAIT_UNTIL([test -s pid], [kill `cat parent`])
# Check that the pidfile names a running process,
# and that the parent process of that process is our child process.
AT_CAPTURE_FILE([parentpid])
AT_CAPTURE_FILE([newpid])
# Start the daemon and wait for the pidfile to get created.
-AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --monitor& echo $! > parent], [0])
+AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile=`pwd`/pid --monitor& echo $! > parent], [0])
OVS_WAIT_UNTIL([test -s pid], [kill `cat parent`])
# Check that the pidfile names a running process,
# and that the parent process of that process is our child process.
# Start the daemon and make sure that the pidfile exists immediately.
# We don't wait for the pidfile to get created because the daemon is
# supposed to do so before the parent exits.
-AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --detach], [0])
+AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile=`pwd`/pid --detach], [0])
AT_CHECK([test -s pid])
AT_CHECK([kill -0 `cat pid`])
# Kill the daemon and make sure that the pidfile gets deleted.
# Start the daemon and make sure that the pidfile exists immediately.
# We don't wait for the pidfile to get created because the daemon is
# supposed to do so before the parent exits.
-AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/daemon --detach --monitor], [0])
+AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile=`pwd`/daemon --detach --monitor], [0])
AT_CHECK([test -s daemon])
# Check that the pidfile names a running process,
# and that the parent process of that process is a running process,
AT_SETUP([daemon --detach startup errors - Python])
AT_SKIP_IF([test $HAVE_PYTHON = no])
AT_CAPTURE_FILE([pid])
-AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --detach --bail], [1], [], [stderr])
+AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile=`pwd`/pid --detach --bail], [1], [], [stderr])
AT_CHECK([grep 'test-daemon.py: exiting after daemonize_start() as requested' stderr],
[0], [ignore], [])
AT_CHECK([test ! -s pid])
AT_SETUP([daemon --detach --monitor startup errors - Python])
AT_SKIP_IF([test $HAVE_PYTHON = no])
AT_CAPTURE_FILE([pid])
-AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --detach --monitor --bail], [1], [], [stderr])
+AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile=`pwd`/pid --detach --monitor --bail], [1], [], [stderr])
AT_CHECK([grep 'test-daemon.py: exiting after daemonize_start() as requested' stderr],
[0], [ignore], [])
AT_CHECK([test ! -s pid])
AT_CAPTURE_FILE([pid])
AT_CAPTURE_FILE([status])
AT_CAPTURE_FILE([stderr])
-AT_CHECK([(yes 2>stderr; echo $? > status) | $PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --detach], [0], [], [])
+AT_CHECK([(yes 2>stderr; echo $? > status) | $PYTHON $srcdir/test-daemon.py --pidfile=`pwd`/pid --detach], [0], [], [])
AT_CHECK([kill `cat pid`])
AT_CHECK([test -s status])
if grep '[[bB]]roken pipe' stderr >/dev/null 2>&1; then
AT_CAPTURE_FILE([status])
AT_CAPTURE_FILE([stderr])
OVSDB_INIT([db])
-AT_CHECK([(yes 2>stderr; echo $? > status) | $PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --detach], [0], [], [])
+AT_CHECK([(yes 2>stderr; echo $? > status) | $PYTHON $srcdir/test-daemon.py --pidfile=`pwd`/pid --detach], [0], [], [])
AT_CHECK([kill `cat pid`])
AT_CHECK([test -s status])
if grep '[[bB]]roken pipe' stderr >/dev/null 2>&1; then
AT_SETUP([JSON-RPC request and successful reply - Python])
AT_SKIP_IF([test $HAVE_PYTHON = no])
-AT_CHECK([$PYTHON $srcdir/test-jsonrpc.py --detach --pidfile-name=$PWD/pid listen punix:socket])
+AT_CHECK([$PYTHON $srcdir/test-jsonrpc.py --detach --pidfile=`pwd`/pid listen punix:socket])
AT_CHECK([test -s pid])
AT_CHECK([kill -0 `cat pid`])
AT_CHECK(
AT_SETUP([JSON-RPC request and error reply - Python])
AT_SKIP_IF([test $HAVE_PYTHON = no])
-AT_CHECK([$PYTHON $srcdir/test-jsonrpc.py --detach --pidfile-name=$PWD/pid listen punix:socket])
+AT_CHECK([$PYTHON $srcdir/test-jsonrpc.py --detach --pidfile=`pwd`/pid listen punix:socket])
AT_CHECK([test -s pid])
AT_CHECK([kill -0 `cat pid`])
AT_CHECK(
AT_SETUP([JSON-RPC notification - Python])
AT_SKIP_IF([test $HAVE_PYTHON = no])
-AT_CHECK([$PYTHON $srcdir/test-jsonrpc.py --detach --pidfile-name=$PWD/pid listen punix:socket])
+AT_CHECK([$PYTHON $srcdir/test-jsonrpc.py --detach --pidfile=`pwd`/pid listen punix:socket])
AT_CHECK([test -s pid])
# When a daemon dies it deletes its pidfile, so make a copy.
AT_CHECK([cp pid pid2])
###
AT_CHECK(
[$PYTHON $top_srcdir/debian/ovs-monitor-ipsec "--root-prefix=`pwd`" \
- "--pidfile-name=`pwd`/ovs-monitor-ipsec.pid" \
+ "--pidfile=`pwd`/ovs-monitor-ipsec.pid" \
unix:socket 2>log 3>actions &])
AT_CAPTURE_FILE([log])
AT_CAPTURE_FILE([actions])
OVS_VSCTL_SETUP
# Start ovs-xapi-sync.
-AT_CHECK([$PYTHON ./ovs-xapi-sync "--pidfile-name=`pwd`/ovs-xapi-sync.pid" \
+AT_CHECK([$PYTHON ./ovs-xapi-sync "--pidfile=`pwd`/ovs-xapi-sync.pid" \
"--root-prefix=`pwd`" unix:socket >log 2>&1 &])
AT_CAPTURE_FILE([log])
# See the License for the specific language governing permissions and
# limitations under the License.
-import getopt
+import argparse
import logging
import signal
import sys
raise Exception("Signal handler called with %d" % signum)
-def main(argv):
+def main():
logging.basicConfig(level=logging.DEBUG)
signal.signal(signal.SIGHUP, handler)
- try:
- options, args = getopt.gnu_getopt(
- argv[1:], 'b', ["bail", "help"] + ovs.daemon.LONG_OPTIONS)
- except getopt.GetoptError, geo:
- sys.stderr.write("%s: %s\n" % (ovs.util.PROGRAM_NAME, geo.msg))
- sys.exit(1)
+ parser = argparse.ArgumentParser(
+ description="Open vSwitch daemonization test program for Python.")
+ parser.add_argument("-b", "--bail", action="store_true",
+ help="Exit with an error after daemonize_start().")
- bail = False
- for key, value in options:
- if key == '--help':
- usage()
- elif key in ['-b', '--bail']:
- bail = True
- elif not ovs.daemon.parse_opt(key, value):
- sys.stderr.write("%s: unhandled option %s\n"
- % (ovs.util.PROGRAM_NAME, key))
- sys.exit(1)
+ ovs.daemon.add_args(parser)
+ args = parser.parse_args()
+ ovs.daemon.handle_args(args)
ovs.daemon.daemonize_start()
- if bail:
+ if args.bail:
sys.stderr.write("%s: exiting after daemonize_start() as requested\n"
% ovs.util.PROGRAM_NAME)
sys.exit(1)
time.sleep(1)
-def usage():
- sys.stdout.write("""\
-%s: Open vSwitch daemonization test program for Python
-usage: %s [OPTIONS]
-""" % ovs.util.PROGRAM_NAME)
- ovs.daemon.usage()
- sys.stdout.write("""
-Other options:
- -h, --help display this help message
- -b, --bail exit with an error after daemonize_start()
-""")
- sys.exit(0)
-
if __name__ == '__main__':
try:
- main(sys.argv)
+ main()
except SystemExit:
# Let system.exit() calls complete normally
raise
# See the License for the specific language governing permissions and
# limitations under the License.
+import argparse
import errno
-import getopt
import os
import sys
def main(argv):
- try:
- options, args = getopt.gnu_getopt(
- argv[1:], 'h', ["help"] + ovs.daemon.LONG_OPTIONS)
- except getopt.GetoptError, geo:
- sys.stderr.write("%s: %s\n" % (ovs.util.PROGRAM_NAME, geo.msg))
- sys.exit(1)
- for key, value in options:
- if key in ['-h', '--help']:
- usage()
- elif not ovs.daemon.parse_opt(key, value):
- sys.stderr.write("%s: unhandled option %s\n"
- % (ovs.util.PROGRAM_NAME, key))
- sys.exit(1)
+ parser = argparse.ArgumentParser(
+ description="JSON-RPC test utility for Python.",
+ formatter_class=argparse.RawDescriptionHelpFormatter)
commands = {"listen": (do_listen, 1),
"request": (do_request, 3),
"notify": (do_notify, 3),
- "help": (usage, (0,))}
-
- command_name = args[0]
- args = args[1:]
+ "help": (parser.print_help, (0,))}
+
+ group_description = """\
+listen LOCAL listen for connections on LOCAL
+request REMOTE METHOD PARAMS send request, print reply
+notify REMOTE METHOD PARAMS send notification and exit
+""" + ovs.stream.usage("JSON-RPC")
+
+ group = parser.add_argument_group(title="Commands",
+ description=group_description)
+ group.add_argument('command', metavar="COMMAND", nargs=1,
+ choices=commands, help="Command to use.")
+ group.add_argument('command_args', metavar="ARG", nargs='*',
+ help="Arguments to COMMAND.")
+
+ ovs.daemon.add_args(parser)
+ args = parser.parse_args()
+ ovs.daemon.handle_args(args)
+
+ command_name = args.command[0]
+ args = args.command_args
if not command_name in commands:
sys.stderr.write("%s: unknown command \"%s\" "
"(use --help for help)\n" % (argv[0], command_name))
func(*args)
-def usage():
- sys.stdout.write("""\
-%s: JSON-RPC test utility for Python
-usage: %s [OPTIONS] COMMAND [ARG...]
- listen LOCAL listen for connections on LOCAL
- request REMOTE METHOD PARAMS send request, print reply
- notify REMOTE METHOD PARAMS send notification and exit
-""" % (ovs.util.PROGRAM_NAME, ovs.util.PROGRAM_NAME))
- sys.stdout.write(ovs.stream.usage("JSON-RPC") + "\n")
- ovs.daemon.usage()
- sys.stdout.write("""
-Other options:
- -h, --help display this help message
-""")
- sys.exit(0)
-
-
if __name__ == '__main__':
main(sys.argv)
# - Set the "iface-id" key in the Interface table.
# - Set the fail-mode on internal bridges.
-import getopt
+import argparse
import logging
import logging.handlers
import os
schema.tables = new_tables
-def usage():
- print "usage: %s [OPTIONS] DATABASE" % sys.argv[0]
- print "where DATABASE is a socket on which ovsdb-server is listening."
- ovs.daemon.usage()
- print """\
-Other options:
- --root-prefix=DIR Use DIR as alternate root directory (for testing).
- -h, --help display this help message"""
- sys.exit(0)
-
-
def handler(signum, _):
global force_run
if (signum == signal.SIGHUP):
force_run = True
-def main(argv):
+def main():
global force_run
s_log.addHandler(logging.StreamHandler())
s_log.warn("failed to open logfile (%s)" % e)
s_log.setLevel(logging.INFO)
- try:
- options, args = getopt.gnu_getopt(
- argv[1:], 'h', ['help', 'root-prefix='] + ovs.daemon.LONG_OPTIONS)
- except getopt.GetoptError, geo:
- sys.stderr.write("%s: %s\n" % (ovs.util.PROGRAM_NAME, geo.msg))
- sys.exit(1)
-
- for key, value in options:
- if key in ['-h', '--help']:
- usage()
- elif key == '--root-prefix':
- global root_prefix
- root_prefix = value
- elif not ovs.daemon.parse_opt(key, value):
- sys.stderr.write("%s: unhandled option %s\n"
- % (ovs.util.PROGRAM_NAME, key))
- sys.exit(1)
-
- if len(args) != 1:
- sys.stderr.write("%s: exactly one nonoption argument is required "
- "(use --help for help)\n" % ovs.util.PROGRAM_NAME)
- sys.exit(1)
-
- remote = args[0]
+ parser = argparse.ArgumentParser()
+ parser.add_argument("database", metavar="DATABASE",
+ help="A socket on which ovsdb-server is listening.")
+ parser.add_argument("--root-prefix", metavar="DIR",
+ help="Use DIR as alternate root directory"
+ " (for testing).")
+
+ ovs.daemon.add_args(parser)
+ args = parser.parse_args()
+ ovs.daemon.handle_args(args)
+
+ global root_prefix
+ root_prefix = args.root_prefix
+
+ remote = args.database
schema_file = "%s/vswitch.ovsschema" % ovs.dirs.PKGDATADIR
schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schema_file))
prune_schema(schema)
if __name__ == '__main__':
try:
- main(sys.argv)
+ main()
except SystemExit:
# Let system.exit() calls complete normally
raise