+ self.out = output
+
+ # Collect metadata about the OS
+ self.meta = {}
+ 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)
+ self.meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
+
+ def _is_sysprep(self, obj):
+ return getattr(obj, 'sysprep', False) and callable(obj)
+
+ def list_syspreps(self):
+
+ objs = [getattr(self, name) for name in dir(self) \
+ if not name.startswith('_')]
+
+ return [x for x in objs if self._is_sysprep(x)]
+
+ def sysprep_info(self, obj):
+ 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 preperation operation"""
+ setattr(obj.im_func, 'enabled', True)
+
+ def disable_sysprep(self, obj):
+ """Disable a system preperation operation"""
+ setattr(obj.im_func, 'enabled', False)
+
+ def print_syspreps(self):
+ """Print enabled and disabled system preperation 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 preperation 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 preperation 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))
+
+ @add_prefix
+ def ls(self, directory):
+ """List the name of all files under a directory"""
+ return self.g.ls(directory)
+
+ @add_prefix
+ def find(self, directory):
+ """List the name of all files recursively under a directory"""
+ return self.g.find(directory)
+
+ def foreach_file(self, directory, action, **kargs):
+ """Perform an action recursively on all files under a directory.
+
+ The following options are allowed:
+
+ * maxdepth: If defined the action will not be performed on
+ files that are below this level of directories under the
+ directory parameter.
+
+ * ftype: The action will only be performed on files of this
+ type. For a list of all allowed filetypes, see here:
+ http://libguestfs.org/guestfs.3.html#guestfs_readdir
+
+ * exclude: Exclude all files that follow this pattern.
+ """
+ maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth']
+ if maxdepth == 0:
+ return
+
+ # maxdepth -= 1
+ maxdepth = None if maxdepth is None else maxdepth - 1
+ kargs['maxdepth'] = maxdepth
+
+ exclude = None if 'exclude' not in kargs else kargs['exclude']
+ ftype = None if 'ftype' not in kargs else kargs['ftype']
+ has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
+
+ for f in self.g.readdir(directory):
+ if f['name'] in ('.', '..'):
+ continue
+
+ full_path = "%s/%s" % (directory, f['name'])
+
+ if exclude and re.match(exclude, full_path):
+ continue
+
+ if has_ftype(f, 'd'):
+ self.foreach_file(full_path, action, **kargs)
+
+ if has_ftype(f, ftype):
+ action(full_path)
+
+ def do_sysprep(self):
+ """Prepere system for image creation."""
+
+ self.out.output('Preparing system for image creation:')
+
+ tasks = self.list_syspreps()
+ enabled = filter(lambda x: x.enabled, tasks)