Skip to contentSkip to conversation

scripts: render_block_graph: Fix with new python and improve argument parsing

PATCH In review Peter Krempa 1 participant 5 messages Feb 3 – Feb 12

lore
Share & tools
lore
#0 lore raw

v3 cover scripts: render_block_graph: Fix with new python and improve argument parsing

New python doesn't like '\l' escapes from the 'dot' language.

While at it improve usability of the script by employing proper argument parsing.

v3:
- rebase + resend

v2:
- use mutually exclusive group instead of hardcoding it (Pavel)

Peter Krempa (3):
scripts: render_block_graph: Fix invalid escape sequence warning with

    python 3.12

scripts: render_block_graph: Implement proper argument parser
scripts: render_block_graph: Avoid backtrace on error from virsh

 scripts/render_block_graph.py | 80 +++++++++++++++++++++++++----------
 1 file changed, 57 insertions(+), 23 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v3 1/3 scripts: render_block_graph: Fix invalid escape sequence warning with python 3.12

From: Peter Krempa <[email protected]>

Trying to run 'render_block_graph' produces following warnings on machine with python 3.12:

 $ ./scripts/render_block_graph.py cd-throttle ble.png
 ./scripts/render_block_graph.py:57: SyntaxWarning: invalid escape sequence '\l'
   '  w - Write\l'
 ./scripts/render_block_graph.py:58: SyntaxWarning: invalid escape sequence '\l'
   '  r - consistent-Read\l'
 ./scripts/render_block_graph.py:59: SyntaxWarning: invalid escape sequence '\l'
   '  u - write - Unchanged\l'
 ./scripts/render_block_graph.py:60: SyntaxWarning: invalid escape sequence '\l'

[...]

The graphviz '\l' escape sequence is used to left-justify the line and new python started enforcing of strings to contain only known escape sequences. Convert the strings containing the dot language to raw.

---
 scripts/render_block_graph.py | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
scripts/render_block_graph.py +12 -12
index 3e1a2e3fa7..14b2d02ec2 100755
@@ -53,16 +53,16 @@ def render_block_graph(qmp, filename, format='png'):
 
     graph = Digraph(comment='Block Nodes Graph')
     graph.format = format
-    graph.node('permission symbols:\l'
-               '  w - Write\l'
-               '  r - consistent-Read\l'
-               '  u - write - Unchanged\l'
-               '  g - Graph-mod\l'
-               '  s - reSize\l'
-               'edge label scheme:\l'
-               '  <child type>\l'
-               '  <perm>\l'
-               '  <shared_perm>\l', shape='none')
+    graph.node(r'permission symbols:\l'
+               r'  w - Write\l'
+               r'  r - consistent-Read\l'
+               r'  u - write - Unchanged\l'
+               r'  g - Graph-mod\l'
+               r'  s - reSize\l'
+               r'edge label scheme:\l'
+               r'  <child type>\l'
+               r'  <perm>\l'
+               r'  <shared_perm>\l', shape='none')
 
     for n in block_graph['nodes']:
         if n['type'] == 'block-driver':
@@ -83,8 +83,8 @@ def render_block_graph(qmp, filename, format='png'):
         graph.node(str(n['id']), label, shape=shape)
 
     for e in block_graph['edges']:
-        label = '%s\l%s\l%s\l' % (e['name'], perm(e['perm']),
-                                  perm(e['shared-perm']))
+        label = r'%s\l%s\l%s\l' % (e['name'], perm(e['perm']),
+                                   perm(e['shared-perm']))
         graph.edge(str(e['parent']), str(e['child']), label=label)
 
     graph.render(filename)
Signature · 1 line
2.52.0
#2 lore raw

PATCH v3 2/3 scripts: render_block_graph: Implement proper argument parser

In reply to Peter Krempa

From: Peter Krempa <[email protected]>

As no argument parsing is employed the script is hard to use and when running without arguments it blurbs:

 $ ./scripts/render_block_graph.py
 Traceback (most recent call last):
   File "/home/pipo/git/qemu.git/./scripts/render_block_graph.py", line 135, in <module>
     obj = sys.argv[1]
          ~~~~~~~~^^^
 IndexError: list index out of range

instead of an actionable error. The user then usually needs to read the script to understand arguments.

Implement proper argument parsing via 'argparse'. The following arguments will be supported:

  $ ./scripts/render_block_graph.py --help
  usage: render_block_graph.py [-h] (--socket SOCKET | --vm VM) [--uri URI] [--output OUTPUT]

Tool that renders the qemu block graph into a image.

options:

    -h, --help       show this help message and exit
    --socket SOCKET  socket mode - path to qemu monitor socket
    --vm VM          libvirt mode - name of libvirt VM
    --uri URI        libvirt URI to connect to
    --output OUTPUT  path to output image (.png suffix added); in libvirt mode default is the name of the VM

Usage then requires one of '--vm' or '--socket'. In libvirt mode the output file is by default populated from the VM name and the '--uri' parameter allows overriding the libvirt connection uri.

---
 scripts/render_block_graph.py | 51 ++++++++++++++++++++++++++++-------
 1 file changed, 41 insertions(+), 10 deletions(-)
scripts/render_block_graph.py +41 -10
index 14b2d02ec2..6f668dec51 100755
@@ -23,6 +23,7 @@
 import subprocess
 import json
 from graphviz import Digraph
+import argparse
 
 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'python'))
 from qemu.qmp import QMPError
@@ -91,13 +92,19 @@ def render_block_graph(qmp, filename, format='png'):
 
 
 class LibvirtGuest():
-    def __init__(self, name):
+    def __init__(self, name, uri=None):
         self.name = name
+        self.uri = uri
 
     def cmd(self, cmd):
         # only supports qmp commands without parameters
         m = {'execute': cmd}
-        ar = ['virsh', 'qemu-monitor-command', self.name, json.dumps(m)]
+        ar = ['virsh']
+
+        if self.uri:
+            ar += ['-c', self.uri]
+
+        ar += ['qemu-monitor-command', self.name, json.dumps(m)]
 
         reply = json.loads(subprocess.check_output(ar))
 
@@ -108,15 +115,39 @@ def cmd(self, cmd):
 
 
 if __name__ == '__main__':
-    obj = sys.argv[1]
-    out = sys.argv[2]
+    parser = argparse.ArgumentParser(
+            description='Tool that renders the qemu block graph into a image.')
+
+    modegroup = parser.add_mutually_exclusive_group(required=True)
+
+    modegroup.add_argument('--socket',
+                           help='socket mode - path to qemu monitor socket')
+    modegroup.add_argument('--vm', help='libvirt mode - name of libvirt VM')
+
+    parser.add_argument('--uri', help='libvirt URI to connect to')
+
+    parser.add_argument('--output',
+                        help='path to output image (.png suffix added); '
+                             'in libvirt mode default is the name of the VM')
 
-    if os.path.exists(obj):
-        # assume unix socket
-        qmp = QEMUMonitorProtocol(obj)
+    args = parser.parse_args()
+
+    if args.socket:
+        if not args.output:
+            print("--output required in socket mode", file=sys.stderr)
+            parser.print_usage()
+            sys.exit(1)
+
+        qmp = QEMUMonitorProtocol(args.socket)
         qmp.connect()
-    else:
-        # assume libvirt guest name
-        qmp = LibvirtGuest(obj)
+        out = args.output
+
+    if args.vm:
+        qmp = LibvirtGuest(args.vm, args.uri)
+
+        if args.output:
+            out = args.output
+        else:
+            out = args.vm
 
     render_block_graph(qmp, out)
Signature · 1 line
2.52.0
#3 lore raw

PATCH v3 3/3 scripts: render_block_graph: Avoid backtrace on error from virsh

In reply to Peter Krempa

From: Peter Krempa <[email protected]>

An error from virsh spews also backtrace:

  $ ./scripts/render_block_graph.py --vm doesnotexist
  error: failed to get domain 'doesnotexist'
  Traceback (most recent call last):
    File "/home/pipo/git/qemu.git/./scripts/render_block_graph.py", line 152, in <module>
      render_block_graph(qmp, out)
      ~~~~~~~~~~~~~~~~~~^^^^^^^^^^
    File "/home/pipo/git/qemu.git/./scripts/render_block_graph.py", line 47, in render_block_graph

[snipped]

instead of just the important bits:

  $ ./scripts/render_block_graph.py --vm doesnotexist
  error: failed to get domain 'doesnotexist'

Catch the exception and exit. 'virsh' already printed the error.

---
 scripts/render_block_graph.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/render_block_graph.py +4 -1
index 6f668dec51..3bfc751caa 100755
@@ -106,7 +106,10 @@ def cmd(self, cmd):
 
         ar += ['qemu-monitor-command', self.name, json.dumps(m)]
 
-        reply = json.loads(subprocess.check_output(ar))
+        try:
+            reply = json.loads(subprocess.check_output(ar))
+        except subprocess.CalledProcessError:
+            sys.exit(1)
 
         if 'error' in reply:
             raise QMPError(reply)
Signature · 1 line
2.52.0
#4 lore raw
In reply to Peter Krempa
Quoted 11 lines · Peter Krempa

New python doesn't like '\l' escapes from the 'dot' language.

While at it improve usability of the script by employing proper argument parsing.

v3:
- rebase + resend

v2:
- use mutually exclusive group instead of hardcoding it (Pavel)

Quoted 8 lines

Peter Krempa (3):
scripts: render_block_graph: Fix invalid escape sequence warning with

    python 3.12

scripts: render_block_graph: Implement proper argument parser
scripts: render_block_graph: Avoid backtrace on error from virsh

 scripts/render_block_graph.py | 80 +++++++++++++++++++++++++----------
 1 file changed, 57 insertions(+), 23 deletions(-)

Ping?