Monitor executed syspreps in os_type/__init__.py
[snf-image-creator] / image_creator / os_type / __init__.py
index 5601a87..cea08a3 100644 (file)
@@ -38,15 +38,16 @@ import re
 
 
 def os_cls(distro, osfamily):
+    """Given the distro name and the osfamily, return the appropriate class"""
     module = None
     classname = None
     try:
-        module = __import__("image_creator.os_type.%s"
-            % distro, fromlist=['image_creator.os_type'])
+        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'])
+        module = __import__("image_creator.os_type.%s" % osfamily,
+                            fromlist=['image_creator.os_type'])
         classname = osfamily.capitalize()
 
     return getattr(module, classname)
@@ -60,9 +61,11 @@ def add_prefix(target):
 
 
 def sysprep(enabled=True):
+    """Decorator for system preparation tasks"""
     def wrapper(func):
         func.sysprep = True
         func.enabled = enabled
+        func.executed = False
         return func
     return wrapper
 
@@ -80,6 +83,8 @@ class OSBase(object):
         self.meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
         self.meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
         self.meta['OS'] = self.g.inspect_get_distro(self.root)
+        if self.meta['OS'] == "unknown":
+            self.meta['OS'] = self.meta['OSFAMILY']
         self.meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
 
     def _is_sysprep(self, obj):
@@ -87,20 +92,20 @@ class OSBase(object):
 
     def list_syspreps(self):
 
-        objs = [getattr(self, name) for name in dir(self) \
-            if not name.startswith('_')]
+        objs = [getattr(self, name) for name in dir(self)
+                if not name.startswith('_')]
 
-        return [x for x in objs if self._is_sysprep(x)]
+        return [x for x in objs if self._is_sysprep(x) and x.executed is False]
 
     def sysprep_info(self, obj):
         assert self._is_sysprep(obj), "Object is not a sysprep"
 
         return (obj.__name__.replace('_', '-'), textwrap.dedent(obj.__doc__))
 
-    def _sysprep_change_status(self, name, status):
-
+    def get_sysprep_by_name(self, name):
+        """Returns the sysprep object with the given name"""
         error_msg = "Syprep operation %s does not exist for %s" % \
-                (name, self.__class__.__name__)
+                    (name, self.__class__.__name__)
 
         method_name = name.replace('-', '_')
         method = None
@@ -112,18 +117,18 @@ class OSBase(object):
         if not self._is_sysprep(method):
             raise FatalError(error_msg)
 
-        setattr(method.im_func, 'enabled', status)
+        return method
 
-    def enable_sysprep(self, name):
-        """Enable a system preperation operation"""
-        self._sysprep_change_status(name, True)
+    def enable_sysprep(self, obj):
+        """Enable a system preparation operation"""
+        setattr(obj.im_func, 'enabled', True)
 
-    def disable_sysprep(self, name):
-        """Disable a system preperation operation"""
-        self._sysprep_change_status(name, False)
+    def disable_sysprep(self, obj):
+        """Disable a system preparation operation"""
+        setattr(obj.im_func, 'enabled', False)
 
     def print_syspreps(self):
-        """Print enabled and disabled system preperation operations."""
+        """Print enabled and disabled system preparation operations."""
 
         syspreps = self.list_syspreps()
         enabled = filter(lambda x: x.enabled, syspreps)
@@ -134,7 +139,7 @@ class OSBase(object):
         wrapper.initial_indent = '\t'
         wrapper.width = 72
 
-        self.out.output("Enabled system preperation operations:")
+        self.out.output("Enabled system preparation operations:")
         if len(enabled) == 0:
             self.out.output("(none)")
         else:
@@ -143,7 +148,7 @@ class OSBase(object):
                 descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
                 self.out.output('    %s:\n%s\n' % (name, descr))
 
-        self.out.output("Disabled system preperation operations:")
+        self.out.output("Disabled system preparation operations:")
         if len(disabled) == 0:
             self.out.output("(none)")
         else:
@@ -218,6 +223,7 @@ class OSBase(object):
             cnt += 1
             self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False)
             task()
+            setattr(task.im_func, 'executed', True)
         self.out.output()
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :