Add check_guestfs_version function
authorNikos Skalkotos <skalkoto@grnet.gr>
Tue, 25 Jun 2013 13:27:53 +0000 (16:27 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Tue, 25 Jun 2013 13:27:53 +0000 (16:27 +0300)
This function is used to check if a specified libguestfs version is
smaller, greater or equal to the installed one

image_creator/image.py
image_creator/util.py

index b27c1eb..86992b6 100644 (file)
@@ -33,7 +33,7 @@
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from image_creator.util import FatalError
+from image_creator.util import FatalError, check_guestfs_version
 from image_creator.gpt import GPTPartitionTable
 from image_creator.os_type import os_cls
 
@@ -64,14 +64,11 @@ class Image(object):
         # file descriptors. This can cause problems especially if the parent
         # process has opened pipes. Since the recovery process is an optional
         # feature of libguestfs, it's better to disable it.
-        self.g.set_recovery_proc(0)
-        version = self.g.version()
-        if version['major'] > 1 or \
-            (version['major'] == 1 and (version['minor'] >= 18 or
-                                        (version['minor'] == 17 and
-                                         version['release'] >= 14))):
-            self.g.set_recovery_proc(1)
+        if check_guestfs_version(self.g, 1, 17, 14) >= 0:
             self.out.output("Enabling recovery proc")
+            self.g.set_recovery_proc(1)
+        else:
+            self.g.set_recovery_proc(0)
 
         #self.g.set_trace(1)
         #self.g.set_verbose(1)
index fb0d0fa..a60d865 100644 (file)
@@ -115,4 +115,23 @@ class MD5:
 
         return checksum
 
+    def check_guestfs_version(ghandler, major, minor, release):
+        """Checks if the version of the used libguestfs is smaller, equal or
+        greater than the one specified by the major, minor and release triplet
+
+        Returns:
+            < 0 if the installed version is smaller than the specified one
+            = 0 if they are equal
+            > 0 if the installed one is greater than the specified one
+        """
+
+        ver = ghandler.version()
+
+        for (a, b) in (ver['major'], major), (ver['minor'], minor), \
+                (ver['release'], release):
+            if a != b:
+                return a - b
+
+        return 0
+
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :