Cleanup RawImageCreator a bit
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 3 Oct 2012 15:13:23 +0000 (18:13 +0300)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Mon, 8 Oct 2012 08:51:16 +0000 (11:51 +0300)
This patch cleans-up the RawImageCreator code a little bit. We construct
the full name for the files we generate many times in various places, which
looks untidy. This commit introduces 2 helper functions: _full_name() and
_full_path() which return full name for a file and full path to a file,
correspondingly. The code becomes more readable with this change.

But also, this is a preparation to one of the following patches where we'll
need to construct full path for the bmap file. We do not want to add yet
another place where we construct this manually, so we better have a helper
function.

Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
Change-Id: I9ac52c95e4b16bcd227df467439ac2ae87b1c145

mic/imager/raw.py

index 9ac3e6e..4085bec 100644 (file)
@@ -174,6 +174,14 @@ class RawImageCreator(BaseImageCreator):
 
         return self._diskinfo
 
+    def _full_name(self, name, extention):
+        """ Construct full file name for a file we generate. """
+        return "%s-%s.%s" % (self.name, name, extention)
+
+    def _full_path(self, path, name, extention):
+        """ Construct full file path to a file we generate. """
+        return os.path.join(path, self._full_name(name, extention))
+
     #
     # Actual implemention
     #
@@ -184,15 +192,11 @@ class RawImageCreator(BaseImageCreator):
 
         #create disk
         for item in self.get_diskinfo():
-            msger.debug("Adding disk %s as %s/%s-%s.raw with size %s bytes" %
-                        (item['name'], self.__imgdir, self.name, item['name'],
-                         item['size']))
-
-            disk = fs_related.SparseLoopbackDisk("%s/%s-%s.raw" % (
-                                                                self.__imgdir,
-                                                                self.name,
-                                                                item['name']),
-                                                 item['size'])
+            full_path = self._full_path(self.__imgdir, item['name'], "raw")
+            msger.debug("Adding disk %s as %s with size %s bytes" \
+                        % (item['name'], full_path, item['size']))
+
+            disk = fs_related.SparseLoopbackDisk(full_path, item['size'])
             self.__disks[item['name']] = disk
 
         self.__instloop = PartitionedMount(self.__disks, self._instroot)
@@ -426,8 +430,9 @@ class RawImageCreator(BaseImageCreator):
 
         i = 0
         for name in self.__disks.keys():
-            xml += "      <drive disk='%s-%s.%s' target='hd%s'/>\n" \
-                   % (self.name,name, self.__disk_format,chr(ord('a')+i))
+            full_name = self._full_name(name, self.__disk_format)
+            xml += "      <drive disk='%s' target='hd%s'/>\n" \
+                       % (full_name, chr(ord('a') + i))
             i = i + 1
 
         xml += "    </boot>\n"
@@ -443,21 +448,17 @@ class RawImageCreator(BaseImageCreator):
 
         if self.checksum is True:
             for name in self.__disks.keys():
-                diskpath = "%s/%s-%s.%s" \
-                           % (self._outdir,self.name,name, self.__disk_format)
+                diskpath = self._full_path(self._outdir, name, \
+                                           self.__disk_format)
                 disk_size = os.path.getsize(diskpath)
                 meter_ct = 0
                 meter = progress.TextMeter()
-                meter.start(size=disk_size,
-                            text="Generating disk signature for %s-%s.%s" \
-                                 % (self.name,
-                                    name,
-                                    self.__disk_format))
-                xml += "    <disk file='%s-%s.%s' use='system' format='%s'>\n"\
-                       % (self.name,
-                          name,
-                          self.__disk_format,
-                          self.__disk_format)
+                full_name = self._full_name(name, self.__disk_format)
+                meter.start(size = disk_size, \
+                            text = "Generating disk signature for %s" \
+                                   % full_name)
+                xml += "    <disk file='%s' use='system' format='%s'>\n" \
+                       % (full_name, self.__disk_format)
 
                 try:
                     import hashlib
@@ -490,11 +491,9 @@ class RawImageCreator(BaseImageCreator):
                 xml += "    </disk>\n"
         else:
             for name in self.__disks.keys():
-                xml += "    <disk file='%s-%s.%s' use='system' format='%s'/>\n"\
-                       %(self.name,
-                         name,
-                         self.__disk_format,
-                         self.__disk_format)
+                full_name = self._full_name(name, self.__disk_format)
+                xml += "    <disk file='%s' use='system' format='%s'/>\n" \
+                       % (full_name, self.__disk_format)
 
         xml += "  </storage>\n"
         xml += "</image>\n"