+
+ SysprepParam = namedtuple('SysprepParam',
+ 'name description length validator')
+
+ def __init__(self, image, **kargs):
+ self.image = image
+
+ self.root = image.root
+ self.g = image.g
+ self.out = image.out
+
+ self.sysprep_params = \
+ kargs['sysprep_params'] if 'sysprep_params' in kargs else {}
+
+ self.meta = {}
+
+ def collect_metadata(self):
+ """Collect metadata about the OS"""
+ try:
+ if not self.mount(readonly=True):
+ raise FatalError("Unable to mount the media read-only")
+
+ self.out.output('Collecting image metadata ...', False)
+ self._do_collect_metadata()
+ self.out.success('done')
+ finally:
+ self.umount()
+
+ self.out.output()
+
+ def needed_sysprep_params(self):
+ """Returns a list of needed sysprep parameters. Each element in the
+ list is a SysprepParam object.
+ """
+ return []
+
+ def list_syspreps(self):
+ """Returns a list of sysprep objects"""
+ objs = [getattr(self, name) for name in dir(self)
+ if not name.startswith('_')]
+
+ return [x for x in objs if self._is_sysprep(x) and x.executed is False]
+
+ def sysprep_info(self, obj):
+ """Returns information about a sysprep object"""
+ assert self._is_sysprep(obj), "Object is not a sysprep"
+
+ return (obj.__name__.replace('_', '-'), textwrap.dedent(obj.__doc__))
+
+ 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__)
+
+ method_name = name.replace('-', '_')
+ method = None
+ try:
+ method = getattr(self, method_name)
+ except AttributeError:
+ raise FatalError(error_msg)
+
+ if not self._is_sysprep(method):
+ raise FatalError(error_msg)
+
+ return method
+
+ def enable_sysprep(self, obj):
+ """Enable a system preparation operation"""
+ setattr(obj.im_func, 'enabled', True)
+
+ 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 preparation operations."""
+
+ syspreps = self.list_syspreps()
+ enabled = filter(lambda x: x.enabled, syspreps)
+ disabled = filter(lambda x: not x.enabled, syspreps)
+
+ wrapper = textwrap.TextWrapper()
+ wrapper.subsequent_indent = '\t'
+ wrapper.initial_indent = '\t'
+ wrapper.width = 72
+
+ self.out.output("Enabled system preparation operations:")
+ if len(enabled) == 0:
+ self.out.output("(none)")
+ else:
+ for sysprep in enabled:
+ name = sysprep.__name__.replace('_', '-')
+ descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
+ self.out.output(' %s:\n%s\n' % (name, descr))
+
+ self.out.output("Disabled system preparation operations:")
+ if len(disabled) == 0:
+ self.out.output("(none)")
+ else:
+ for sysprep in disabled:
+ name = sysprep.__name__.replace('_', '-')
+ descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
+ self.out.output(' %s:\n%s\n' % (name, descr))
+
+ def print_sysprep_params(self):
+ """Print the system preparation parameter the user may use"""
+
+ self.out.output("Needed system preparation parameters:")
+
+ params = self.needed_sysprep_params()
+
+ if len(params) == 0:
+ self.out.output("(none)")
+ return
+
+ for param in params:
+ self.out.output("\t%s (%s): %s" %
+ (param.description, param.name,
+ self.sysprep_params[param.name] if param.name in
+ self.sysprep_params else "(none)"))
+
+ def do_sysprep(self):
+ """Prepare system for image creation."""
+
+ try:
+ if not self.mount(readonly=False):
+ raise FatalError("Unable to mount the media read-write")
+
+ self.out.output('Preparing system for image creation:')
+
+ tasks = self.list_syspreps()
+ enabled = filter(lambda x: x.enabled, tasks)
+
+ size = len(enabled)
+ cnt = 0
+ for task in enabled:
+ cnt += 1
+ self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False)
+ task()
+ setattr(task.im_func, 'executed', True)
+ finally:
+ self.umount()
+
+ self.out.output()
+
+ def mount(self, readonly=False):
+ """Mount image."""
+
+ if getattr(self, "mounted", False):
+ return True
+
+ mount_type = 'read-only' if readonly else 'read-write'
+ self.out.output("Mounting the media %s ..." % mount_type, False)
+
+ if not self._do_mount(readonly):
+ return False
+
+ self.mounted = True
+ self.out.success('done')
+ return True
+
+ def umount(self):
+ """Umount all mounted filesystems."""
+
+ self.out.output("Umounting the media ...", False)
+ self.g.umount_all()
+ self.mounted = False
+ self.out.success('done')
+
+ def _is_sysprep(self, obj):
+ """Checks if an object is a sysprep"""
+ return getattr(obj, 'sysprep', False) and callable(obj)