Move get_os_class from image_creator to os_type
authorNikos Skalkotos <skalkoto@grnet.gr>
Sun, 29 Apr 2012 12:21:47 +0000 (15:21 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Sun, 29 Apr 2012 12:21:47 +0000 (15:21 +0300)
This is needed because otherwise setup.py fails on some systems. Also
do some more cleanup.

image_creator/__init__.py
image_creator/disk.py
image_creator/gpt.py
image_creator/main.py
image_creator/os_type/__init__.py

index afdda63..6f90bfa 100644 (file)
 
 __version__ = '0.1'
 
-
-import image_creator.os_type
-
-
-def get_os_class(distro, osfamily):
-    module = None
-    classname = None
-    try:
-        module = __import__("image_creator.os_type.%s"
-            % distro, fromlist=['image_creator.os_type'])
-        classname = distro.capitalize()
-    except ImportError:
-        module = __import__("image_creator.os_type.%s"
-            % osfamily, fromlist=['image_creator.os_type'])
-        classname = osfamily.capitalize()
-
-    return getattr(module, classname)
-
-
-
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
index 4103e10..0a4f222 100644 (file)
@@ -120,8 +120,7 @@ class Disk(object):
         os.close(cowfd)
         self._add_cleanup(os.unlink, cow)
         # Create 1G cow sparse file
-        dd('if=/dev/null', 'of=%s' % cow, 'bs=1k', \
-                                        'seek=%d' % (1024 * 1024))
+        dd('if=/dev/null', 'of=%s' % cow, 'bs=1k', 'seek=%d' % (1024 * 1024))
         cowdev = self._losetup(cow)
 
         snapshot = uuid.uuid4().hex
@@ -273,7 +272,7 @@ class DiskDevice(object):
 
         if self.parttype == 'msdos' and last_partition['part_num'] > 4:
             raise FatalError("This disk contains logical partitions. "
-                "Only primary partitions are supported.")
+                                    "Only primary partitions are supported.")
 
         part_dev = "%s%d" % (self.guestfs_device, last_partition['part_num'])
         fs_type = self.g.vfs_type(part_dev)
@@ -299,8 +298,7 @@ class DiskDevice(object):
         self.g.part_add(self.guestfs_device, 'p', start, end)
 
         self.size = (end + 1) * sector_size
-        success("new image size is %dMB" %
-                            ((self.size + 2 ** 20 - 1) // 2 ** 20))
+        success("new size is %dMB" % ((self.size + 2 ** 20 - 1) // 2 ** 20))
 
         if self.parttype == 'gpt':
             ptable = GPTPartitionTable(self.real_device)
@@ -318,15 +316,15 @@ class DiskDevice(object):
         progress_size = (self.size + 2 ** 20 - 1) // 2 ** 20  # in MB
         progressbar = progress("Dumping image file: ", 'mb')
         progressbar.max = progress_size
-        with open(self.real_device, 'r') as source:
-            with open(outfile, "w") as dest:
+
+        with open(self.real_device, 'r') as src:
+            with open(outfile, "w") as dst:
                 left = self.size
                 offset = 0
                 progressbar.next()
                 while left > 0:
                     length = min(left, blocksize)
-                    sent = sendfile(dest.fileno(), source.fileno(), offset,
-                                                                        length)
+                    sent = sendfile(dst.fileno(), src.fileno(), offset, length)
                     offset += sent
                     left -= sent
                     progressbar.goto((self.size - left) // 2 ** 20)
index 1a7ed04..f6f1cc2 100644 (file)
@@ -232,7 +232,7 @@ class GPTPartitionTable(object):
     def __init__(self, disk):
         self.disk = disk
         with open(disk, "rb") as d:
-            #MBR (Logical block address 0)
+            # MBR (Logical block address 0)
             lba0 = d.read(BLOCKSIZE)
             self.mbr = MBR(lba0)
 
index c6a1c9a..5cbcb51 100644 (file)
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from image_creator import get_os_class
 from image_creator import __version__ as version
 from image_creator import util
 from image_creator.disk import Disk
 from image_creator.util import get_command, error, success, output, \
                                                     FatalError, progress, md5
+from image_creator.os_type import get_os_class
 from image_creator.kamaki_wrapper import Kamaki
 import sys
 import os
 import optparse
 import StringIO
 
-dd = get_command('dd')
-
 
 def check_writable_dir(option, opt_str, value, parser):
     dirname = os.path.dirname(value)
@@ -145,8 +143,8 @@ def image_creator():
 
     if options.outfile is None and not options.upload \
                                             and not options.print_sysprep:
-        raise FatalError("At least one of `-o', `-u' or" \
-                            "`--print-sysprep' must be set")
+        raise FatalError("At least one of `-o', `-u' or `--print-sysprep' " \
+                                                                "must be set")
 
     title = 'snf-image-creator %s' % version
     output(title)
@@ -199,7 +197,7 @@ def image_creator():
         checksum = md5(snapshot, size)
 
         metastring = '\n'.join(
-            ['%s=%s' % (key, value) for (key, value) in metadata.items()])
+                ['%s=%s' % (key, value) for (key, value) in metadata.items()])
         metastring += '\n'
 
         if options.outfile is not None:
@@ -207,13 +205,13 @@ def image_creator():
 
             output('Dumping metadata file...', False)
             with open('%s.%s' % (options.outfile, 'meta'), 'w') as f:
-                    f.write(metastring)
+                f.write(metastring)
             success('done')
 
             output('Dumping md5sum file...', False)
             with open('%s.%s' % (options.outfile, 'md5sum'), 'w') as f:
-                f.write('%s %s\n' % (
-                                checksum, os.path.basename(options.outfile)))
+                f.write('%s %s\n' % (checksum, \
+                                            os.path.basename(options.outfile)))
             success('done')
 
         # Destroy the device. We only need the snapshot from now on
index fc06ff9..b19d573 100644 (file)
@@ -37,6 +37,21 @@ import textwrap
 import re
 
 
+def get_os_class(distro, osfamily):
+    module = None
+    classname = None
+    try:
+        module = __import__("image_creator.os_type.%s"
+            % distro, fromlist=['image_creator.os_type'])
+        classname = distro.capitalize()
+    except ImportError:
+        module = __import__("image_creator.os_type.%s"
+            % osfamily, fromlist=['image_creator.os_type'])
+        classname = osfamily.capitalize()
+
+    return getattr(module, classname)
+
+
 def add_prefix(target):
     def wrapper(self, *args):
         prefix = args[0]