summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Hahn <hahn@univention.de>2018-11-20 08:34:48 +0100
committerPhilipp Hahn <pmhahn+github@pmhahn.de>2020-08-05 07:43:02 +0000
commit74a78fa42c7dcf75343909923cc87184107cbd31 (patch)
tree1457e7f419ce53e563631191775a0eede465bb3a
parentb801ff31faf97a616e554ba266783d480e00b781 (diff)
downloadlibvirt-python-74a78fa42c7dcf75343909923cc87184107cbd31.tar.gz
examples: Fix white space
indent by 4 spaces one spaces around assignments Signed-off-by: Philipp Hahn <hahn@univention.de>
-rw-r--r--examples/consolecallback.py14
-rwxr-xr-xexamples/dhcpleases.py4
-rwxr-xr-xexamples/dominfo.py4
-rwxr-xr-xexamples/domipaddrs.py2
-rwxr-xr-xexamples/domstart.py1
-rwxr-xr-xexamples/esxlist.py16
-rwxr-xr-xexamples/guest-vcpus/guest-vcpu-daemon.py13
-rwxr-xr-xexamples/nodestats.py22
-rwxr-xr-xexamples/sparsestream.py8
-rwxr-xr-xexamples/topology.py18
10 files changed, 71 insertions, 31 deletions
diff --git a/examples/consolecallback.py b/examples/consolecallback.py
index 208e028..d7810b9 100644
--- a/examples/consolecallback.py
+++ b/examples/consolecallback.py
@@ -10,16 +10,18 @@ import atexit
from argparse import ArgumentParser
from typing import Optional # noqa F401
+
def reset_term() -> None:
termios.tcsetattr(0, termios.TCSADRAIN, attrs)
+
def error_handler(unused, error) -> None:
# The console stream errors on VM shutdown; we don't care
- if (error[0] == libvirt.VIR_ERR_RPC and
- error[1] == libvirt.VIR_FROM_STREAMS):
+ if error[0] == libvirt.VIR_ERR_RPC and error[1] == libvirt.VIR_FROM_STREAMS:
return
logging.warn(error)
+
class Console(object):
def __init__(self, uri: str, uuid: str) -> None:
self.uri = uri
@@ -34,9 +36,9 @@ class Console(object):
logging.info("%s initial state %d, reason %d",
self.uuid, self.state[0], self.state[1])
+
def check_console(console: Console) -> bool:
- if (console.state[0] == libvirt.VIR_DOMAIN_RUNNING or
- console.state[0] == libvirt.VIR_DOMAIN_PAUSED):
+ if (console.state[0] == libvirt.VIR_DOMAIN_RUNNING or console.state[0] == libvirt.VIR_DOMAIN_PAUSED):
if console.stream is None:
console.stream = console.connection.newStream(libvirt.VIR_STREAM_NONBLOCK)
console.domain.openConsole(None, console.stream, 0)
@@ -48,6 +50,7 @@ def check_console(console: Console) -> bool:
return console.run_console
+
def stdin_callback(watch: int, fd: int, events: int, console: Console) -> None:
readbuf = os.read(fd, 1024)
if readbuf.startswith(b""):
@@ -56,6 +59,7 @@ def stdin_callback(watch: int, fd: int, events: int, console: Console) -> None:
if console.stream:
console.stream.send(readbuf)
+
def stream_callback(stream: libvirt.virStream, events: int, console: Console) -> None:
try:
assert console.stream
@@ -64,11 +68,13 @@ def stream_callback(stream: libvirt.virStream, events: int, console: Console) ->
return
os.write(0, received_data)
+
def lifecycle_callback(connection: libvirt.virConnect, domain: libvirt.virDomain, event: int, detail: int, console: Console) -> None:
console.state = console.domain.state(0)
logging.info("%s transitioned to state %d, reason %d",
console.uuid, console.state[0], console.state[1])
+
# main
parser = ArgumentParser(epilog="Example: %(prog)s 'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'")
parser.add_argument("uri")
diff --git a/examples/dhcpleases.py b/examples/dhcpleases.py
index 13049c5..92395c5 100755
--- a/examples/dhcpleases.py
+++ b/examples/dhcpleases.py
@@ -30,6 +30,7 @@ if not leases:
print("Failed to get leases for %s" % net.name())
exit(0)
+
def toIPAddrType(addrType: int) -> str:
if addrType == libvirt.VIR_IP_ADDR_TYPE_IPV4:
return "ipv4"
@@ -37,13 +38,14 @@ def toIPAddrType(addrType: int) -> str:
return "ipv6"
return "Unknown"
+
print(" {0:20} {1:18} {2:9} {3:25} {4:15} {5}".format("Expiry Time",
"MAC address",
"Protocol",
"IP address",
"Hostname",
"Client ID or DUID"))
-print("-"*115)
+print("-" * 115)
for lease in leases:
print(" {0:20} {1:18} {2:9} {3:25} {4:15} {5}".format(
diff --git a/examples/dominfo.py b/examples/dominfo.py
index 0c45374..ce4898a 100755
--- a/examples/dominfo.py
+++ b/examples/dominfo.py
@@ -18,13 +18,15 @@ def print_section(title: str) -> None:
print("\n%s" % title)
print("=" * 60)
+
def print_entry(key: str, value: Any) -> None:
print("%-10s %-10s" % (key, value))
+
def print_xml(key: str, ctx, path: str) -> str:
res = ctx.xpathEval(path)
if res is None or len(res) == 0:
- value="Unknown"
+ value = "Unknown"
else:
value = res[0].content
print_entry(key, value)
diff --git a/examples/domipaddrs.py b/examples/domipaddrs.py
index e649b34..60f388c 100755
--- a/examples/domipaddrs.py
+++ b/examples/domipaddrs.py
@@ -24,7 +24,7 @@ def print_dom_ifaces(dom: libvirt.virDomain) -> None:
for (name, val) in ifaces.items():
if val['addrs']:
for addr in val['addrs']:
- print (" {0:10} {1:19} {2:12} {3}/{4}".format(
+ print(" {0:10} {1:19} {2:12} {3}/{4}".format(
name,
val['hwaddr'],
IPTYPE[addr['type']],
diff --git a/examples/domstart.py b/examples/domstart.py
index 3a33219..fdfe25f 100755
--- a/examples/domstart.py
+++ b/examples/domstart.py
@@ -9,6 +9,7 @@ import libxml2
from argparse import ArgumentParser
from typing import Tuple
+
# Parse the XML description of domU from FNAME
# and return a tuple (name, xmldesc) where NAME
# is the name of the domain, and xmldesc is the contetn of FNAME
diff --git a/examples/esxlist.py b/examples/esxlist.py
index ac88f77..b68d6c8 100755
--- a/examples/esxlist.py
+++ b/examples/esxlist.py
@@ -97,13 +97,15 @@ except libvirt.libvirtError:
print("Failed to open connection to %s" % args.hostname)
exit(1)
-state_names = { libvirt.VIR_DOMAIN_RUNNING : "running",
- libvirt.VIR_DOMAIN_BLOCKED : "idle",
- libvirt.VIR_DOMAIN_PAUSED : "paused",
- libvirt.VIR_DOMAIN_SHUTDOWN : "in shutdown",
- libvirt.VIR_DOMAIN_SHUTOFF : "shut off",
- libvirt.VIR_DOMAIN_CRASHED : "crashed",
- libvirt.VIR_DOMAIN_NOSTATE : "no state" }
+state_names = {
+ libvirt.VIR_DOMAIN_RUNNING: "running",
+ libvirt.VIR_DOMAIN_BLOCKED: "idle",
+ libvirt.VIR_DOMAIN_PAUSED: "paused",
+ libvirt.VIR_DOMAIN_SHUTDOWN: "in shutdown",
+ libvirt.VIR_DOMAIN_SHUTOFF: "shut off",
+ libvirt.VIR_DOMAIN_CRASHED: "crashed",
+ libvirt.VIR_DOMAIN_NOSTATE: "no state",
+}
for id in conn.listDomainsID():
domain = conn.lookupByID(id)
diff --git a/examples/guest-vcpus/guest-vcpu-daemon.py b/examples/guest-vcpus/guest-vcpu-daemon.py
index d9e0776..33ec2a4 100755
--- a/examples/guest-vcpus/guest-vcpu-daemon.py
+++ b/examples/guest-vcpus/guest-vcpu-daemon.py
@@ -15,6 +15,7 @@ uri = "qemu:///system"
customXMLuri = "guest-cpu.python.libvirt.org"
connectRetryTimeout = 5
+
class workerData:
def __init__(self):
self.doms = list()
@@ -61,21 +62,24 @@ def virEventLoopNativeRun():
while True:
libvirt.virEventRunDefaultImpl()
+
def handleAgentLifecycleEvent(conn, dom, state, reason, opaque):
if state == libvirt.VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_STATE_CONNECTED:
opaque.addDomainNotify(dom)
+
def handleConnectClose(conn, reason, opaque):
print('Disconnected from ' + uri)
opaque.closeConnectNotify()
+
def handleLibvirtLibraryError(opaque, error):
pass
+
def processAgentConnect(dom):
try:
- cpus = dom.metadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT, customXMLuri,
- libvirt.VIR_DOMAIN_AFFECT_LIVE)
+ cpus = dom.metadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT, customXMLuri, libvirt.VIR_DOMAIN_AFFECT_LIVE)
doc = minidom.parseString(cpus)
ncpus = int(doc.getElementsByTagName('ncpus')[0].getAttribute('count'))
except Exception:
@@ -87,6 +91,7 @@ def processAgentConnect(dom):
except Exception:
print("failed to set vcpu count for domain " + dom.name())
+
def work():
data = workerData()
@@ -118,6 +123,7 @@ def work():
data.waitNotify()
+
def main():
libvirt.virEventRegisterDefaultImpl()
libvirt.registerErrorHandler(handleLibvirtLibraryError, None)
@@ -131,7 +137,8 @@ def main():
eventLoop.start()
while True:
- time.sleep(1)
+ time.sleep(1)
+
if __name__ == "__main__":
parser = ArgumentParser(description=__doc__)
diff --git a/examples/nodestats.py b/examples/nodestats.py
index 10520b2..b38c56c 100755
--- a/examples/nodestats.py
+++ b/examples/nodestats.py
@@ -9,6 +9,7 @@ from xml.dom import minidom
import libxml2
from typing import Any, Dict # noqa F401
+
def xpath_eval(ctxt, path: str) -> str:
res = ctxt.xpathEval(path)
if res is None or len(res) == 0:
@@ -17,6 +18,7 @@ def xpath_eval(ctxt, path: str) -> str:
value = res[0].content
return value
+
try:
conn = libvirt.openReadOnly(None)
except libvirt.libvirtError:
@@ -32,17 +34,23 @@ except libvirt.libvirtError:
caps = minidom.parseString(capsXML)
cells = caps.getElementsByTagName("cells")[0]
-nodesIDs = [ int(proc.getAttribute("id"))
- for proc in cells.getElementsByTagName("cell") ]
+nodesIDs = [
+ int(proc.getAttribute("id"))
+ for proc in cells.getElementsByTagName("cell")
+]
-nodesMem = [ conn.getMemoryStats(int(proc))
- for proc in nodesIDs]
+nodesMem = [
+ conn.getMemoryStats(int(proc))
+ for proc in nodesIDs
+]
doms = conn.listAllDomains(libvirt.VIR_CONNECT_LIST_DOMAINS_ACTIVE)
-domsStrict = [ proc
- for proc in doms
- if proc.numaParameters()["numa_mode"] == libvirt.VIR_DOMAIN_NUMATUNE_MEM_STRICT ]
+domsStrict = [
+ proc
+ for proc in doms
+ if proc.numaParameters()["numa_mode"] == libvirt.VIR_DOMAIN_NUMATUNE_MEM_STRICT
+]
domsStrictCfg = {} # type: Dict[libvirt.virDomain, Dict[str, Dict[str, Any]]]
for dom in domsStrict:
diff --git a/examples/sparsestream.py b/examples/sparsestream.py
index bd9961f..60edcb9 100755
--- a/examples/sparsestream.py
+++ b/examples/sparsestream.py
@@ -12,23 +12,28 @@ import libvirt
import os
from argparse import ArgumentParser
+
def bytesWriteHandler(stream: libvirt.virStream, buf: bytes, opaque: int) -> int:
fd = opaque
return os.write(fd, buf)
+
def bytesReadHandler(stream: libvirt.virStream, nbytes: int, opaque: int) -> bytes:
fd = opaque
return os.read(fd, nbytes)
+
def recvSkipHandler(stream: libvirt.virStream, length: int, opaque: int) -> None:
fd = opaque
cur = os.lseek(fd, length, os.SEEK_CUR)
return os.ftruncate(fd, cur)
+
def sendSkipHandler(stream: libvirt.virStream, length: int, opaque: int) -> int:
fd = opaque
return os.lseek(fd, length, os.SEEK_CUR)
+
def holeHandler(stream: libvirt.virStream, opaque: int):
fd = opaque
cur = os.lseek(fd, 0, os.SEEK_CUR)
@@ -79,6 +84,7 @@ def holeHandler(stream: libvirt.virStream, opaque: int):
os.lseek(fd, cur, os.SEEK_SET)
return [inData, sectionLen]
+
def download(vol: libvirt.virStorageVol, st: libvirt.virStream, filename: str) -> None:
offset = 0
length = 0
@@ -89,6 +95,7 @@ def download(vol: libvirt.virStorageVol, st: libvirt.virStream, filename: str) -
os.close(fd)
+
def upload(vol: libvirt.virStorageVol, st: libvirt.virStream, filename: str) -> None:
offset = 0
length = 0
@@ -99,6 +106,7 @@ def upload(vol: libvirt.virStorageVol, st: libvirt.virStream, filename: str) ->
os.close(fd)
+
# main
parser = ArgumentParser(description=__doc__)
parser.add_argument("uri")
diff --git a/examples/topology.py b/examples/topology.py
index 83fe0c0..c39ee9c 100755
--- a/examples/topology.py
+++ b/examples/topology.py
@@ -29,13 +29,17 @@ total_cpus = cells.getElementsByTagName('cpu').length
socketIds = []
siblingsIds = []
-socketIds = [ proc.getAttribute('socket_id')
- for proc in cells.getElementsByTagName('cpu')
- if proc.getAttribute('socket_id') not in socketIds ]
-
-siblingsIds = [ proc.getAttribute('siblings')
- for proc in cells.getElementsByTagName('cpu')
- if proc.getAttribute('siblings') not in siblingsIds ]
+socketIds = [
+ proc.getAttribute('socket_id')
+ for proc in cells.getElementsByTagName('cpu')
+ if proc.getAttribute('socket_id') not in socketIds
+]
+
+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'))