[virt-tools-list] [virt-manager PATCH 2/6] inspection: simplify mount points sort/mount

Pino Toscano ptoscano at redhat.com
Wed Apr 17 16:49:49 UTC 2019


Rely on the Python 3 sorting facilities to sort the mount points using
a key based on the length of the mount point, doing the same effect as
the old compare function.

As side change required by this, enable python_return_dict on the
GuestFS handle, so we get proper hashes instead of lists.  This requires
libguestfs 1.22, which is 6 years old by now (and other virt-manager
requires are way more recent than that).
---
 virtManager/inspection.py | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/virtManager/inspection.py b/virtManager/inspection.py
index 084571bb..6021e130 100644
--- a/virtManager/inspection.py
+++ b/virtManager/inspection.py
@@ -198,7 +198,7 @@ class vmmInspection(vmmGObject):
 
         import guestfs  # pylint: disable=import-error
 
-        g = guestfs.GuestFS(close_on_exit=False)
+        g = guestfs.GuestFS(close_on_exit=False, python_return_dict=True)
         prettyvm = conn.get_uri() + ":" + vm.get_name()
         try:
             g.add_libvirt_dom(vm.get_backend(), readonly=1)
@@ -239,23 +239,16 @@ class vmmInspection(vmmGObject):
 
         # Sort keys by length, shortest first, so that we end up
         # mounting the filesystems in the correct order.
-        mps = list(g.inspect_get_mountpoints(root))
-        def compare(a, b):
-            if len(a[0]) > len(b[0]):
-                return 1
-            elif len(a[0]) == len(b[0]):
-                return 0
-            else:
-                return -1
-
-        mps.sort(key=functools.cmp_to_key(compare))
-        for mp_dev in mps:
+        mps = g.inspect_get_mountpoints(root)
+
+        mps = sorted(mps.items(), key=lambda k: len(k[0]))
+        for mp, dev in mps:
             try:
-                g.mount_ro(mp_dev[1], mp_dev[0])
+                g.mount_ro(dev, mp)
             except Exception:
                 logging.exception("%s: exception mounting %s on %s "
                                   "(ignored)",
-                                  prettyvm, mp_dev[1], mp_dev[0])
+                                  prettyvm, dev, mp)
 
         filesystems_mounted = True
 
-- 
2.20.1




More information about the virt-tools-list mailing list