[libvirt] [PATCH python 01/14] examples: Invoke print("...") instead of print "..."

Doug Goldstein cardoe at gentoo.org
Mon Dec 9 15:48:59 UTC 2013


On Mon, Dec 9, 2013 at 9:15 AM, Daniel P. Berrange <berrange at redhat.com> wrote:
> From: "Daniel P. Berrange" <berrange at redhat.com>
>
> The 'print' method must be called as a function in python3,
> ie with brackets.
>
> Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
> ---
>  examples/consolecallback.py |  6 ++--
>  examples/dominfo.py         | 14 +++++-----
>  examples/domrestore.py      | 17 ++++++------
>  examples/domsave.py         | 15 +++++-----
>  examples/domstart.py        | 19 ++++++-------
>  examples/esxlist.py         | 14 +++++-----
>  examples/event-test.py      | 68 ++++++++++++++++++++++-----------------------
>  examples/topology.py        | 14 +++++-----
>  8 files changed, 82 insertions(+), 85 deletions(-)
>
> diff --git a/examples/consolecallback.py b/examples/consolecallback.py
> index d8e33a9..c539a92 100644
> --- a/examples/consolecallback.py
> +++ b/examples/consolecallback.py
> @@ -62,14 +62,14 @@ def lifecycle_callback (connection, domain, event, detail, console):
>
>  # main
>  if len(sys.argv) != 3:
> -    print "Usage:", sys.argv[0], "URI UUID"
> -    print "for example:", sys.argv[0], "'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'"
> +    print("Usage:", sys.argv[0], "URI UUID")
> +    print("for example:", sys.argv[0], "'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'")
>      sys.exit(1)
>
>  uri = sys.argv[1]
>  uuid = sys.argv[2]
>
> -print "Escape character is ^]"
> +print("Escape character is ^]")
>  logging.basicConfig(filename='msg.log', level=logging.DEBUG)
>  logging.info("URI: %s", uri)
>  logging.info("UUID: %s", uuid)
> diff --git a/examples/dominfo.py b/examples/dominfo.py
> index bfa3ca3..d3049cd 100755
> --- a/examples/dominfo.py
> +++ b/examples/dominfo.py
> @@ -8,15 +8,15 @@ import libxml2
>  import pdb
>
>  def usage():
> -   print 'Usage: %s DOMAIN' % sys.argv[0]
> -   print '       Print information about the domain DOMAIN'
> +   print('Usage: %s DOMAIN' % sys.argv[0])
> +   print('       Print information about the domain DOMAIN')
>
>  def print_section(title):
> -    print "\n%s" % title
> -    print "=" * 60
> +    print("\n%s" % title)
> +    print("=" * 60)
>
>  def print_entry(key, value):
> -    print "%-10s %-10s" % (key, value)
> +    print("%-10s %-10s" % (key, value))
>
>  def print_xml(key, ctx, path):
>      res = ctx.xpathEval(path)
> @@ -36,14 +36,14 @@ name = sys.argv[1]
>  # Connect to libvirt
>  conn = libvirt.openReadOnly(None)
>  if conn is None:
> -    print 'Failed to open connection to the hypervisor'
> +    print('Failed to open connection to the hypervisor')
>      sys.exit(1)
>
>  try:
>      dom = conn.lookupByName(name)
>      # Annoyiingly, libvirt prints its own error message here
>  except libvirt.libvirtError:
> -    print "Domain %s is not running" % name
> +    print("Domain %s is not running" % name)
>      sys.exit(0)
>
>  info = dom.info()
> diff --git a/examples/domrestore.py b/examples/domrestore.py
> index fffc90f..06fdfbc 100755
> --- a/examples/domrestore.py
> +++ b/examples/domrestore.py
> @@ -8,10 +8,10 @@ import libxml2
>  import pdb
>
>  def usage():
> -   print 'Usage: %s DIR' % sys.argv[0]
> -   print '       Restore all the domains contained in DIR'
> -   print '       It is assumed that all files in DIR are'
> -   print '       images of domU\'s previously created with save'
> +   print('Usage: %s DIR' % sys.argv[0])
> +   print('       Restore all the domains contained in DIR')
> +   print('       It is assumed that all files in DIR are')
> +   print('       images of domU\'s previously created with save')
>
>  if len(sys.argv) != 2:
>      usage()
> @@ -22,15 +22,14 @@ imgs = os.listdir(dir)
>
>  conn = libvirt.open(None)
>  if conn is None:
> -    print 'Failed to open connection to the hypervisor'
> +    print('Failed to open connection to the hypervisor')
>      sys.exit(1)
>
>  for img in imgs:
>      file = os.path.join(dir, img)
> -    print "Restoring %s ... " % img,
> -    sys.stdout.flush()
> +    print("Restoring %s ... " % img)
>      ret = conn.restore(file)
>      if ret == 0:
> -        print "done"
> +        print("done")
>      else:
> -        print "error %d" % ret
> +        print("error %d" % ret)
> diff --git a/examples/domsave.py b/examples/domsave.py
> index bac4536..727217c 100755
> --- a/examples/domsave.py
> +++ b/examples/domsave.py
> @@ -8,9 +8,9 @@ import libxml2
>  import pdb
>
>  def usage():
> -   print 'Usage: %s DIR' % sys.argv[0]
> -   print '       Save all currently running domU\'s into DIR'
> -   print '       DIR must exist and be writable by this process'
> +   print('Usage: %s DIR' % sys.argv[0])
> +   print('       Save all currently running domU\'s into DIR')
> +   print('       DIR must exist and be writable by this process')
>
>  if len(sys.argv) != 2:
>      usage()
> @@ -20,7 +20,7 @@ dir = sys.argv[1]
>
>  conn = libvirt.open(None)
>  if conn is None:
> -    print 'Failed to open connection to the hypervisor'
> +    print('Failed to open connection to the hypervisor')
>      sys.exit(1)
>
>  doms = conn.listDomainsID()
> @@ -28,13 +28,12 @@ for id in doms:
>      if id == 0:
>          continue
>      dom = conn.lookupByID(id)
> -    print "Saving %s[%d] ... " % (dom.name(), id),
> -    sys.stdout.flush()
> +    print("Saving %s[%d] ... " % (dom.name(), id))
>      path = os.path.join(dir, dom.name())
>      ret = dom.save(path)
>      if ret == 0:
> -        print "done"
> +        print("done")
>      else:
> -        print "error %d" % ret
> +        print("error %d" % ret)
>
>  #pdb.set_trace()
> diff --git a/examples/domstart.py b/examples/domstart.py
> index b14fad1..ce1b60c 100755
> --- a/examples/domstart.py
> +++ b/examples/domstart.py
> @@ -20,11 +20,11 @@ def read_domain(fname):
>      return (name, xmldesc)
>
>  def usage():
> -   print 'Usage: %s domain.xml' % sys.argv[0]
> -   print '       Check that the domain described by DOMAIN.XML is running'
> -   print '       If the domain is not running, create it'
> -   print '       DOMAIN.XML must be a XML description of the domain'
> -   print '       in libvirt\'s XML format'
> +   print('Usage: %s domain.xml' % sys.argv[0])
> +   print('       Check that the domain described by DOMAIN.XML is running')
> +   print('       If the domain is not running, create it')
> +   print('       DOMAIN.XML must be a XML description of the domain')
> +   print('       in libvirt\'s XML format')
>
>  if len(sys.argv) != 2:
>      usage()
> @@ -34,17 +34,16 @@ if len(sys.argv) != 2:
>
>  conn = libvirt.open(None)
>  if conn is None:
> -    print 'Failed to open connection to the hypervisor'
> +    print('Failed to open connection to the hypervisor')
>      sys.exit(1)
>
>  try:
>      dom = conn.lookupByName(name)
>  except libvirt.libvirtError:
> -    print "Starting domain %s ... " % name,
> -    sys.stdout.flush()
> +    print("Starting domain %s ... " % name)
>      dom = conn.createLinux(xmldesc, 0)
>      if dom is None:
> -        print "failed"
> +        print("failed")
>          sys.exit(1)
>      else:
> -        print "done"
> +        print("done")
> diff --git a/examples/esxlist.py b/examples/esxlist.py
> index c55424f..0d47b00 100755
> --- a/examples/esxlist.py
> +++ b/examples/esxlist.py
> @@ -10,8 +10,8 @@ import getpass
>
>
>  def usage():
> -    print "Usage: %s HOSTNAME" % sys.argv[0]
> -    print "       List active domains of HOSTNAME and print some info"
> +    print("Usage: %s HOSTNAME" % sys.argv[0])
> +    print("       List active domains of HOSTNAME and print some info")
>
>
>  # This is the callback method passed to libvirt.openAuth() (see below).
> @@ -51,12 +51,12 @@ def request_credentials(credentials, user_data):
>
>
>  def print_section(title):
> -    print "\n%s" % title
> -    print "=" * 60
> +    print("\n%s" % title)
> +    print("=" * 60)
>
>
>  def print_entry(key, value):
> -    print "%-10s %-10s" % (key, value)
> +    print("%-10s %-10s" % (key, value))
>
>
>  def print_xml(key, ctx, path):
> @@ -100,7 +100,7 @@ auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT],
>  conn = libvirt.openAuth(uri, auth, 0)
>
>  if conn is None:
> -    print "Failed to open connection to %s" % hostname
> +    print("Failed to open connection to %s" % hostname)
>      sys.exit(1)
>
>  state_names = { libvirt.VIR_DOMAIN_RUNNING  : "running",
> @@ -136,7 +136,7 @@ for id in conn.listDomainsID():
>          ctx.setContextNode(d)
>
>          if not first:
> -            print "------------------------------------------------------------"
> +            print("------------------------------------------------------------")
>          else:
>              first = False
>
> diff --git a/examples/event-test.py b/examples/event-test.py
> index 84f5259..cf1a8b8 100644
> --- a/examples/event-test.py
> +++ b/examples/event-test.py
> @@ -30,7 +30,7 @@ do_debug = False
>  def debug(msg):
>      global do_debug
>      if do_debug:
> -        print msg
> +        print(msg)
>
>  #
>  # This general purpose event loop will support waiting for file handle
> @@ -456,50 +456,50 @@ def detailToString(event, detail):
>      return eventStrings[event][detail]
>
>  def myDomainEventCallback1 (conn, dom, event, detail, opaque):
> -    print "myDomainEventCallback1 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(),
> +    print("myDomainEventCallback1 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(),
>                                                                   eventToString(event),
> -                                                                 detailToString(event, detail))
> +                                                                 detailToString(event, detail)))
>
>  def myDomainEventCallback2 (conn, dom, event, detail, opaque):
> -    print "myDomainEventCallback2 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(),
> +    print("myDomainEventCallback2 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(),
>                                                                   eventToString(event),
> -                                                                 detailToString(event, detail))
> +                                                                 detailToString(event, detail)))
>
>  def myDomainEventRebootCallback(conn, dom, opaque):
> -    print "myDomainEventRebootCallback: Domain %s(%s)" % (dom.name(), dom.ID())
> +    print("myDomainEventRebootCallback: Domain %s(%s)" % (dom.name(), dom.ID()))
>
>  def myDomainEventRTCChangeCallback(conn, dom, utcoffset, opaque):
> -    print "myDomainEventRTCChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), utcoffset)
> +    print("myDomainEventRTCChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), utcoffset))
>
>  def myDomainEventWatchdogCallback(conn, dom, action, opaque):
> -    print "myDomainEventWatchdogCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), action)
> +    print("myDomainEventWatchdogCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), action))
>
>  def myDomainEventIOErrorCallback(conn, dom, srcpath, devalias, action, opaque):
> -    print "myDomainEventIOErrorCallback: Domain %s(%s) %s %s %d" % (dom.name(), dom.ID(), srcpath, devalias, action)
> +    print("myDomainEventIOErrorCallback: Domain %s(%s) %s %s %d" % (dom.name(), dom.ID(), srcpath, devalias, action))
>
>  def myDomainEventGraphicsCallback(conn, dom, phase, localAddr, remoteAddr, authScheme, subject, opaque):
> -    print "myDomainEventGraphicsCallback: Domain %s(%s) %d %s" % (dom.name(), dom.ID(), phase, authScheme)
> +    print("myDomainEventGraphicsCallback: Domain %s(%s) %d %s" % (dom.name(), dom.ID(), phase, authScheme))
>
>  def myDomainEventDiskChangeCallback(conn, dom, oldSrcPath, newSrcPath, devAlias, reason, opaque):
> -    print "myDomainEventDiskChangeCallback: Domain %s(%s) disk change oldSrcPath: %s newSrcPath: %s devAlias: %s reason: %s" % (
> -            dom.name(), dom.ID(), oldSrcPath, newSrcPath, devAlias, reason)
> +    print("myDomainEventDiskChangeCallback: Domain %s(%s) disk change oldSrcPath: %s newSrcPath: %s devAlias: %s reason: %s" % (
> +            dom.name(), dom.ID(), oldSrcPath, newSrcPath, devAlias, reason))
>  def myDomainEventTrayChangeCallback(conn, dom, devAlias, reason, opaque):
> -    print "myDomainEventTrayChangeCallback: Domain %s(%s) tray change devAlias: %s reason: %s" % (
> -            dom.name(), dom.ID(), devAlias, reason)
> +    print("myDomainEventTrayChangeCallback: Domain %s(%s) tray change devAlias: %s reason: %s" % (
> +            dom.name(), dom.ID(), devAlias, reason))
>  def myDomainEventPMWakeupCallback(conn, dom, reason, opaque):
> -    print "myDomainEventPMWakeupCallback: Domain %s(%s) system pmwakeup" % (
> -            dom.name(), dom.ID())
> +    print("myDomainEventPMWakeupCallback: Domain %s(%s) system pmwakeup" % (
> +            dom.name(), dom.ID()))
>  def myDomainEventPMSuspendCallback(conn, dom, reason, opaque):
> -    print "myDomainEventPMSuspendCallback: Domain %s(%s) system pmsuspend" % (
> -            dom.name(), dom.ID())
> +    print("myDomainEventPMSuspendCallback: Domain %s(%s) system pmsuspend" % (
> +            dom.name(), dom.ID()))
>  def myDomainEventBalloonChangeCallback(conn, dom, actual, opaque):
> -    print "myDomainEventBalloonChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), actual)
> +    print("myDomainEventBalloonChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), actual))
>  def myDomainEventPMSuspendDiskCallback(conn, dom, reason, opaque):
> -    print "myDomainEventPMSuspendDiskCallback: Domain %s(%s) system pmsuspend_disk" % (
> -            dom.name(), dom.ID())
> +    print("myDomainEventPMSuspendDiskCallback: Domain %s(%s) system pmsuspend_disk" % (
> +            dom.name(), dom.ID()))
>  def myDomainEventDeviceRemovedCallback(conn, dom, dev, opaque):
> -    print "myDomainEventDeviceRemovedCallback: Domain %s(%s) device removed: %s" % (
> -            dom.name(), dom.ID(), dev)
> +    print("myDomainEventDeviceRemovedCallback: Domain %s(%s) device removed: %s" % (
> +            dom.name(), dom.ID(), dev))
>
>  run = True
>
> @@ -507,27 +507,27 @@ def myConnectionCloseCallback(conn, reason, opaque):
>      reasonStrings = (
>          "Error", "End-of-file", "Keepalive", "Client",
>          )
> -    print "myConnectionCloseCallback: %s: %s" % (conn.getURI(), reasonStrings[reason])
> +    print("myConnectionCloseCallback: %s: %s" % (conn.getURI(), reasonStrings[reason]))
>      run = False
>
> -def usage(out=sys.stderr):
> -    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 usage():
> +    print("usage: "+os.path.basename(sys.argv[0])+" [-hdl] [uri]")
> +    print("   uri will default to qemu:///system")
> +    print("   --help, -h   Print(this help message")
> +    print("   --debug, -d  Print(debug output")
> +    print("   --loop, -l   Toggle event-loop-implementation")
>
>  def main():
>      try:
>          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"
> +        print(str(err)) # will print something like "option -a not recognized"
>          usage()
>          sys.exit(2)
>      for o, a in opts:
>          if o in ("-h", "--help"):
> -            usage(sys.stdout)
> +            usage()
>              sys.exit()
>          if o in ("-d", "--debug"):
>              global do_debug
> @@ -541,7 +541,7 @@ def main():
>      else:
>          uri = "qemu:///system"
>
> -    print "Using uri:" + uri
> +    print("Using uri:" + uri)
>
>      # Run a background thread with the event loop
>      if use_pure_python_event_loop:
> @@ -554,7 +554,7 @@ def main():
>      # Close connection on exit (to test cleanup paths)
>      old_exitfunc = getattr(sys, 'exitfunc', None)
>      def exit():
> -        print "Closing " + str(vc)
> +        print("Closing " + vc.getURI())
>          vc.close()
>          if (old_exitfunc): old_exitfunc()
>      sys.exitfunc = exit
> diff --git a/examples/topology.py b/examples/topology.py
> index 62effe3..191669c 100755
> --- a/examples/topology.py
> +++ b/examples/topology.py
> @@ -13,13 +13,13 @@ from xml.dom import minidom
>  try:
>      conn = libvirt.openReadOnly(None)
>  except libvirt.libvirtError:
> -    print 'Failed to connect to the hypervisor'
> +    print('Failed to connect to the hypervisor')
>      sys.exit(1)
>
>  try:
>      capsXML = conn.getCapabilities()
>  except libvirt.libvirtError:
> -    print 'Failed to request capabilities'
> +    print('Failed to request capabilities')
>      sys.exit(1)
>
>  caps = minidom.parseString(capsXML)
> @@ -38,8 +38,8 @@ siblingsIds = [ proc.getAttribute('siblings')
>                  for proc in cells.getElementsByTagName('cpu')
>                  if proc.getAttribute('siblings') not in siblingsIds ]
>
> -print "Host topology"
> -print "NUMA nodes:", cells.getAttribute('num')
> -print "   Sockets:", len(set(socketIds))
> -print "     Cores:", len(set(siblingsIds))
> -print "   Threads:", total_cpus
> +print("Host topology")
> +print("NUMA nodes:", cells.getAttribute('num'))
> +print("   Sockets:", len(set(socketIds)))
> +print("     Cores:", len(set(siblingsIds)))
> +print("   Threads:", total_cpus)
> --
> 1.8.3.1
>
> --
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list

ACK.

-- 
Doug Goldstein




More information about the libvir-list mailing list