[libvirt] [PATCH 0/3] example: event-test.py

Some minor improvements to the event-test Python example. Philipp Hahn (3): example: Fix argument handling example: Redirect --help output to stdout/stderr example: Support debug output and loop switch examples/domain-events/events-python/event-test.py | 25 +++++++++++++------ 1 files changed, 17 insertions(+), 8 deletions(-)

sys.argv contains the original command line arguments, while args only contains the arguments not handled by getopt(). Currently this is no problem since --help is the only command line option passable, which terminates the process, so the code is never reached. Any option added in the future will reveal the bug. Signed-off-by: Philipp Hahn <hahn@univention.de> --- examples/domain-events/events-python/event-test.py | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/domain-events/events-python/event-test.py b/examples/domain-events/events-python/event-test.py index 76fda2b..9018366 100644 --- a/examples/domain-events/events-python/event-test.py +++ b/examples/domain-events/events-python/event-test.py @@ -486,8 +486,8 @@ def main(): usage() sys.exit() - if len(sys.argv) > 1: - uri = sys.argv[1] + if len(args) >= 1: + uri = args[0] else: uri = "qemu:///system" -- 1.7.1

On 10/12/2011 08:54 AM, Philipp Hahn wrote:
sys.argv contains the original command line arguments, while args only contains the arguments not handled by getopt(). Currently this is no problem since --help is the only command line option passable, which terminates the process, so the code is never reached. Any option added in the future will reveal the bug.
Signed-off-by: Philipp Hahn<hahn@univention.de> --- examples/domain-events/events-python/event-test.py | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
ACK and pushed. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

When --help is requested, print usage() to stdout. When an illegal option is passed, print usage to stderr. Signed-off-by: Philipp Hahn <hahn@univention.de> --- examples/domain-events/events-python/event-test.py | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/domain-events/events-python/event-test.py b/examples/domain-events/events-python/event-test.py index 9018366..5272e56 100644 --- a/examples/domain-events/events-python/event-test.py +++ b/examples/domain-events/events-python/event-test.py @@ -469,9 +469,9 @@ def myDomainEventIOErrorCallback(conn, dom, srcpath, devalias, action, opaque): def myDomainEventGraphicsCallback(conn, dom, phase, localAddr, remoteAddr, authScheme, subject, opaque): print "myDomainEventGraphicsCallback: Domain %s(%s) %d %s" % (dom.name(), dom.ID(), phase, authScheme) -def usage(): - print "usage: "+os.path.basename(sys.argv[0])+" [uri]" - print " uri will default to qemu:///system" +def usage(out=sys.stderr): + print >>out, "usage: "+os.path.basename(sys.argv[0])+" [uri]" + print >>out, " uri will default to qemu:///system" def main(): try: @@ -483,7 +483,7 @@ def main(): sys.exit(2) for o, a in opts: if o in ("-h", "--help"): - usage() + usage(sys.stdout) sys.exit() if len(args) >= 1: -- 1.7.1

On 10/12/2011 09:11 AM, Philipp Hahn wrote:
When --help is requested, print usage() to stdout. When an illegal option is passed, print usage to stderr.
Signed-off-by: Philipp Hahn<hahn@univention.de> --- examples/domain-events/events-python/event-test.py | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
ACK and pushed. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

Add support for enabling debug output via command line option. Allow to toggle the loop implementation between pure-Python and native-C. Signed-off-by: Philipp Hahn <hahn@univention.de> --- examples/domain-events/events-python/event-test.py | 13 +++++++++++-- 1 files changed, 11 insertions(+), 2 deletions(-) diff --git a/examples/domain-events/events-python/event-test.py b/examples/domain-events/events-python/event-test.py index 5272e56..4df9b98 100644 --- a/examples/domain-events/events-python/event-test.py +++ b/examples/domain-events/events-python/event-test.py @@ -470,12 +470,15 @@ def myDomainEventGraphicsCallback(conn, dom, phase, localAddr, remoteAddr, authS print "myDomainEventGraphicsCallback: Domain %s(%s) %d %s" % (dom.name(), dom.ID(), phase, authScheme) def usage(out=sys.stderr): - print >>out, "usage: "+os.path.basename(sys.argv[0])+" [uri]" + print >>out, "usage: "+os.path.basename(sys.argv[0])+" [-hdl] [uri]" print >>out, " uri will default to qemu:///system" + print >>out, " --help, -h Print this help message" + print >>out, " --debug, -d Print debug output" + print >>out, " --loop, -l Toggle event-loop-implementation" def main(): try: - opts, args = getopt.getopt(sys.argv[1:], "h", ["help"] ) + opts, args = getopt.getopt(sys.argv[1:], "hdl", ["help", "debug", "loop"]) except getopt.GetoptError, err: # print help information and exit: print str(err) # will print something like "option -a not recognized" @@ -485,6 +488,12 @@ def main(): if o in ("-h", "--help"): usage(sys.stdout) sys.exit() + if o in ("-d", "--debug"): + global do_debug + do_debug = True + if o in ("-l", "--loop"): + global use_pure_python_event_loop + use_pure_python_event_loop ^= True if len(args) >= 1: uri = args[0] -- 1.7.1

On 10/12/2011 09:13 AM, Philipp Hahn wrote:
Add support for enabling debug output via command line option. Allow to toggle the loop implementation between pure-Python and native-C.
Signed-off-by: Philipp Hahn<hahn@univention.de> --- examples/domain-events/events-python/event-test.py | 13 +++++++++++-- 1 files changed, 11 insertions(+), 2 deletions(-)
Nice - makes the example program a bit more useful as a standalone test. ACK and pushed. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org
participants (2)
-
Eric Blake
-
Philipp Hahn