# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
-from image_creator.util import output, FatalError
+from image_creator.util import FatalError
import textwrap
import re
-def get_os_class(distro, osfamily):
+def os_cls(distro, osfamily):
module = None
classname = None
try:
class OSBase(object):
"""Basic operating system class"""
- def __init__(self, rootdev, ghandler):
+ def __init__(self, rootdev, ghandler, output):
self.root = rootdev
self.g = ghandler
+ 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)
objs = [getattr(self, name) for name in dir(self) \
if not name.startswith('_')]
- enabled = [x for x in objs if self._is_sysprep(x) and x.enabled]
- disabled = [x for x in objs if self._is_sysprep(x) and not x.enabled]
+ return [x for x in objs if self._is_sysprep(x)]
- return enabled, disabled
+ def sysprep_info(self, obj):
+ assert self._is_sysprep(obj), "Object is not a sysprep"
- def _sysprep_change_status(self, name, status):
+ 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__)
+ (name, self.__class__.__name__)
method_name = name.replace('-', '_')
method = None
if not self._is_sysprep(method):
raise FatalError(error_msg)
- setattr(method.im_func, 'enabled', status)
+ return method
- def enable_sysprep(self, name):
+ def enable_sysprep(self, obj):
"""Enable a system preperation operation"""
- self._sysprep_change_status(name, True)
+ setattr(obj.im_func, 'enabled', True)
- def disable_sysprep(self, name):
+ def disable_sysprep(self, obj):
"""Disable a system preperation operation"""
- self._sysprep_change_status(name, False)
+ setattr(obj.im_func, 'enabled', False)
def print_syspreps(self):
"""Print enabled and disabled system preperation operations."""
- enabled, disabled = self.list_syspreps()
+ 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
- output("Enabled system preperation operations:")
+ self.out.output("Enabled system preperation operations:")
if len(enabled) == 0:
- output("(none)")
+ self.out.output("(none)")
else:
for sysprep in enabled:
name = sysprep.__name__.replace('_', '-')
descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
- output(' %s:\n%s\n' % (name, descr))
+ self.out.output(' %s:\n%s\n' % (name, descr))
- output("Disabled system preperation operations:")
+ self.out.output("Disabled system preperation operations:")
if len(disabled) == 0:
- output("(none)")
+ self.out.output("(none)")
else:
for sysprep in disabled:
name = sysprep.__name__.replace('_', '-')
descr = wrapper.fill(textwrap.dedent(sysprep.__doc__))
- output(' %s:\n%s\n' % (name, descr))
+ self.out.output(' %s:\n%s\n' % (name, descr))
@add_prefix
def ls(self, directory):
if has_ftype(f, ftype):
action(full_path)
- def get_metadata(self):
- """Returns some descriptive metadata about the OS."""
- meta = {}
- meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
- meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
- meta['OS'] = self.g.inspect_get_distro(self.root)
- meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
-
- return meta
-
def do_sysprep(self):
"""Prepere system for image creation."""
- output('Preparing system for image creation:')
+ self.out.output('Preparing system for image creation:')
+
+ tasks = self.list_syspreps()
+ enabled = filter(lambda x: x.enabled, tasks)
- tasks, _ = self.list_syspreps()
- size = len(tasks)
+ size = len(enabled)
cnt = 0
- for task in tasks:
+ for task in enabled:
cnt += 1
- output(('(%d/%d)' % (cnt, size)).ljust(7), False)
+ self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False)
task()
- output()
+ self.out.output()
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :