1
0
mirror of https://gitlab.com/libvirt/libvirt-python.git synced 2025-07-29 19:41:52 +03:00

examples: Invoke print("...") instead of print "..."

The 'print' method must be called as a function in python3,
ie with brackets.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange
2013-12-03 15:34:56 +00:00
parent bb3301ba78
commit 3f4e32c6a1
8 changed files with 82 additions and 85 deletions

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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()

View File

@ -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")

View File

@ -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

View File

@ -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

View File

@ -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)