Skip to contentSkip to conversation

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

PATCH Superseded Peter Krempa 2 participants 5 messages Mar 18, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#1 lore raw

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

In reply to Peter Krempa

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.48.1
#2 lore raw

PATCH 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  direct 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 will be 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 | 53 ++++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 10 deletions(-)
scripts/render_block_graph.py +43 -10
index 14b2d02ec2..7a6738410c 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,41 @@ 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.')
+
+    parser.add_argument('--socket',
+                        help='direct mode - path to qemu monitor socket')
+
+    parser.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')
+
+    args = parser.parse_args()
 
-    if os.path.exists(obj):
-        # assume unix socket
-        qmp = QEMUMonitorProtocol(obj)
+    if (args.socket and args.vm) or (not args.socket and not args.vm):
+        print("One of --socket or --vm is required.", file=sys.stderr)
+        parser.print_help()
+        sys.exit(1)
+
+    if args.socket:
+        qmp = QEMUMonitorProtocol(args.socket)
         qmp.connect()
-    else:
-        # assume libvirt guest name
-        qmp = LibvirtGuest(obj)
+
+    if args.vm:
+        qmp = LibvirtGuest(args.vm, args.uri)
+
+        if args.output:
+            out = args.output
+        else:
+            out = args.vm
+
+    if not out:
+        print("--output required", file=sys.stderr)
+        parser.print_help()
+        sys.exit(1)
 
     render_block_graph(qmp, out)
Signature · 1 line
2.48.1
#4 lore raw

PATCH 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 7a6738410c..e2bb83b60e 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.48.1
#0 lore raw

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.

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 | 82 +++++++++++++++++++++++++----------
 1 file changed, 59 insertions(+), 23 deletions(-)
Signature · 1 line
2.48.1
#3 lore raw

Re: [PATCH 2/3] scripts: render_block_graph: Implement proper argument parser

In reply to Peter Krempa
Quoted 91 lines · 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  direct 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 will be 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 | 53 ++++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 10 deletions(-)
scripts/render_block_graph.py +21 -4
index 14b2d02ec2..7a6738410c 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,41 @@ 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.')
+
+    parser.add_argument('--socket',
+                        help='direct mode - path to qemu monitor socket')
+
+    parser.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')

scripts/render_block_graph.py def cmd(self, cmd): · line 136

Quoted 10 lines
scripts/render_block_graph.py +6 -3
+
+    args = parser.parse_args()
 
-    if os.path.exists(obj):
-        # assume unix socket
-        qmp = QEMUMonitorProtocol(obj)
+    if (args.socket and args.vm) or (not args.socket and not args.vm):
+        print("One of --socket or --vm is required.", file=sys.stderr)
+        parser.print_help()
+        sys.exit(1)

It's possible to do with argparse as well:

modegroup = parser.add_mutually_exclusive_group(required=True) modegroup.add_argument('--socket',
help='direct mode - path to qemu monitor socket')
modegroup.add_argument('--vm', help='libvirt mode - name of libvirt VM')

The only difference is that it will print usage parser.print_usage().

Quoted 7 lines
scripts/render_block_graph.py +3 -3
+
+    if args.socket:
+        qmp = QEMUMonitorProtocol(args.socket)
         qmp.connect()
-    else:
-        # assume libvirt guest name
-        qmp = LibvirtGuest(obj)

scripts/render_block_graph.py def cmd(self, cmd): · line 150

Quoted 10 lines
scripts/render_block_graph.py +10 -0
+
+    if args.vm:
+        qmp = LibvirtGuest(args.vm, args.uri)
+
+        if args.output:
+            out = args.output
+        else:
+            out = args.vm
+
+    if not out:

This needs to use args.output otherwise python will complain that it doesn't know out variable.

scripts/render_block_graph.py def cmd(self, cmd): · line 152

Quoted 2 lines
scripts/render_block_graph.py +2 -0
+        print("--output required", file=sys.stderr)
+        parser.print_help()

Probably use parser.print_usage() if you decide to use the add_mutually_exclusive_group().

Pavel

Quoted 7 lines
scripts/render_block_graph.py +1 -0
+        sys.exit(1)
 
     render_block_graph(qmp, out)
Signature · 1 line
2.48.1