Shelltestrunner tests for hcheck
[ganeti-local] / lib / ovf.py
index 7c5305b..6ce13e1 100644 (file)
@@ -45,6 +45,11 @@ try:
 except ImportError:
   import elementtree.ElementTree as ET
 
+try:
+  ParseError = ET.ParseError # pylint: disable=E1103
+except AttributeError:
+  ParseError = None
+
 from ganeti import constants
 from ganeti import errors
 from ganeti import utils
@@ -99,16 +104,16 @@ VS_TYPE = {
 
 # AllocationUnits values and conversion
 ALLOCATION_UNITS = {
-  'b': ["bytes", "b"],
-  'kb': ["kilobytes", "kb", "byte * 2^10", "kibibytes", "kib"],
-  'mb': ["megabytes", "mb", "byte * 2^20", "mebibytes", "mib"],
-  'gb': ["gigabytes", "gb", "byte * 2^30", "gibibytes", "gib"],
+  "b": ["bytes", "b"],
+  "kb": ["kilobytes", "kb", "byte * 2^10", "kibibytes", "kib"],
+  "mb": ["megabytes", "mb", "byte * 2^20", "mebibytes", "mib"],
+  "gb": ["gigabytes", "gb", "byte * 2^30", "gibibytes", "gib"],
 }
 CONVERT_UNITS_TO_MB = {
-  'b': lambda x: x / (1024 * 1024),
-  'kb': lambda x: x / 1024,
-  'mb': lambda x: x,
-  'gb': lambda x: x * 1024,
+  "b": lambda x: x / (1024 * 1024),
+  "kb": lambda x: x / 1024,
+  "mb": lambda x: x,
+  "gb": lambda x: x * 1024,
 }
 
 # Names of the config fields
@@ -139,6 +144,17 @@ DISK_FORMAT = {
 }
 
 
+def CheckQemuImg():
+  """ Make sure that qemu-img is present before performing operations.
+
+  @raise errors.OpPrereqError: when qemu-img was not found in the system
+
+  """
+  if not constants.QEMUIMG_PATH:
+    raise errors.OpPrereqError("qemu-img not found at build time, unable"
+                               " to continue")
+
+
 def LinkFile(old_path, prefix=None, suffix=None, directory=None):
   """Create link with a given prefix and suffix.
 
@@ -210,7 +226,7 @@ class OVFReader(object):
     self.tree = ET.ElementTree()
     try:
       self.tree.parse(input_path)
-    except xml.parsers.expat.ExpatError, err:
+    except (ParseError, xml.parsers.expat.ExpatError), err:
       raise errors.OpPrereqError("Error while reading %s file: %s" %
                                  (OVF_EXT, err))
 
@@ -911,6 +927,7 @@ class Converter(object):
     @raise errors.OpPrereqError: convertion of the disk failed
 
     """
+    CheckQemuImg()
     disk_file = os.path.basename(disk_path)
     (disk_name, disk_extension) = os.path.splitext(disk_file)
     if disk_extension != disk_format:
@@ -921,7 +938,7 @@ class Converter(object):
       prefix=disk_name, dir=self.output_dir)
     self.temp_file_manager.Add(new_disk_path)
     args = [
-      "qemu-img",
+      constants.QEMUIMG_PATH,
       "convert",
       "-O",
       disk_format,
@@ -948,7 +965,8 @@ class Converter(object):
     @raise errors.OpPrereqError: format information cannot be retrieved
 
     """
-    args = ["qemu-img", "info", disk_path]
+    CheckQemuImg()
+    args = [constants.QEMUIMG_PATH, "info", disk_path]
     run_result = utils.RunCmd(args, cwd=os.getcwd())
     if run_result.failed:
       raise errors.OpPrereqError("Gathering info about the disk using qemu-img"
@@ -1312,6 +1330,7 @@ class OVFImporter(Converter):
       information or size information is invalid or creation failed
 
     """
+    CheckQemuImg()
     assert self.options.disks
     results = {}
     for (disk_id, disk_desc) in self.options.disks:
@@ -1324,7 +1343,7 @@ class OVFImporter(Converter):
                                      (disk_id, disk_desc["size"]))
         new_path = utils.PathJoin(self.output_dir, str(disk_id))
         args = [
-          "qemu-img",
+          constants.QEMUIMG_PATH,
           "create",
           "-f",
           "raw",