[et-mgmt-tools] [PATCH 3 of 5] Store the image filename in the Image class

David Lutterkort dlutter at redhat.com
Thu Dec 13 02:24:01 UTC 2007


# HG changeset patch
# User David Lutterkort <dlutter at redhat.com>
# Date 1197512462 28800
# Node ID eb8ba74062f4d4f1fcf13578860d049d3e6e4611
# Parent  2f713d54e92e7699c43ac828ab4bab326275edd2
Store the image filename in the Image class

diff -r 2f713d54e92e -r eb8ba74062f4 virt-image
--- a/virt-image	Wed Dec 12 18:21:02 2007 -0800
+++ b/virt-image	Wed Dec 12 18:21:02 2007 -0800
@@ -147,17 +147,6 @@ def parse_args():
     
     return options
 
-def parse_image_xml(fname):
-    if fname is None:
-        print >> sys.stderr, _("Must provide the location of an image XML file with --image")
-        sys.exit(1)
-    if not os.access(fname, os.R_OK):
-        print >> sys.stderr, _("Can not read %s") % fname
-    file = open(fname, "r")
-    xml = file.read()
-    file.close()
-    return virtinst.ImageParser.parse(xml, os.path.dirname(fname))
-
 def main():
     options = parse_args()
 
@@ -166,7 +155,7 @@ def main():
     conn = cli.getConnection(options.connect)
     type = None
 
-    image = parse_image_xml(options.image)
+    image = virtinst.ImageParser.parse_file(options.image)
     capabilities = virtinst.CapabilitiesParser.parse(conn.getCapabilities())
 
     if options.boot is not None:
diff -r 2f713d54e92e -r eb8ba74062f4 virtinst/ImageParser.py
--- a/virtinst/ImageParser.py	Wed Dec 12 18:21:02 2007 -0800
+++ b/virtinst/ImageParser.py	Wed Dec 12 18:21:02 2007 -0800
@@ -30,10 +30,17 @@ class ParserException(Exception):
 
 class Image:
     """The toplevel object representing a VM image"""
-    def __init__(self, node = None, base = "."):
+    def __init__(self, node = None, base = None, filename = None):
         self.storage = {}
         self.domain = None
-        self.base = base
+        self.filename = os.path.abspath(filename)
+        if base is None:
+            if filename is not None:
+                self.base = os.path.dirname(filename)
+            else:
+                self.base ="."
+        else:
+            self.base = base
         self.name = None
         self.label = None
         self.descr = None
@@ -217,7 +224,7 @@ def xpathString(node, path, default = No
         result = default
     return result
 
-def parse(xml, base):
+def parse(xml, filename):
     """Parse the XML description of a VM image into a data structure. Returns
     an object of class Image. BASE should be the directory where the disk
     image files for this image can be found"""
@@ -245,10 +252,15 @@ def parse(xml, base):
         if root.name != "image":
             raise ParserException(_("Root element is not 'image'"))
 
-        image = Image(root)
-        image.base = base
+        image = Image(root, filename = filename)
     finally:
         doc.freeDoc()
 
     return image
 
+def parse_file(filename):
+    file = open(filename, "r")
+    xml = file.read()
+    file.close()
+    return parse(xml, filename = filename)
+




More information about the et-mgmt-tools mailing list