Statistics
| Branch: | Tag: | Revision:

root / image_creator / os_type / __init__.py @ 9297c398

History | View | Annotate | Download (2.6 kB)

1 aa2062ba Nikos Skalkotos
#!/usr/bin/env python
2 aa2062ba Nikos Skalkotos
3 8c574358 Nikos Skalkotos
import re
4 8c574358 Nikos Skalkotos
5 8c574358 Nikos Skalkotos
6 0d5a999d Nikos Skalkotos
def add_prefix(target):
7 0d5a999d Nikos Skalkotos
    def wrapper(self, *args):
8 0d5a999d Nikos Skalkotos
        prefix = args[0]
9 0d5a999d Nikos Skalkotos
        return map(lambda x: prefix + x, target(self, *args))
10 0d5a999d Nikos Skalkotos
    return wrapper
11 0d5a999d Nikos Skalkotos
12 8c574358 Nikos Skalkotos
13 aa2062ba Nikos Skalkotos
class OSBase(object):
14 3b2f6619 Nikos Skalkotos
    """Basic operating system class"""
15 aa2062ba Nikos Skalkotos
    def __init__(self, rootdev, ghandler):
16 aa2062ba Nikos Skalkotos
        self.root = rootdev
17 aa2062ba Nikos Skalkotos
        self.g = ghandler
18 aa2062ba Nikos Skalkotos
19 0d5a999d Nikos Skalkotos
    @add_prefix
20 8c574358 Nikos Skalkotos
    def ls(self, directory):
21 3b2f6619 Nikos Skalkotos
        """List the name of all files under a directory"""
22 8c574358 Nikos Skalkotos
        return self.g.ls(directory)
23 0d5a999d Nikos Skalkotos
24 0d5a999d Nikos Skalkotos
    @add_prefix
25 8c574358 Nikos Skalkotos
    def find(self, directory):
26 3b2f6619 Nikos Skalkotos
        """List the name of all files recursively under a directory"""
27 8c574358 Nikos Skalkotos
        return self.g.find(directory)
28 8c574358 Nikos Skalkotos
29 8c574358 Nikos Skalkotos
    def foreach_file(self, directory, action, **kargs):
30 3b2f6619 Nikos Skalkotos
        """Perform an action recursively on all files under a directory.
31 8c574358 Nikos Skalkotos

32 3b2f6619 Nikos Skalkotos
        The following options are allowed:
33 3b2f6619 Nikos Skalkotos

34 3b2f6619 Nikos Skalkotos
        * maxdepth: If defined the action will not be performed on
35 3b2f6619 Nikos Skalkotos
          files that are below this level of directories under the
36 3b2f6619 Nikos Skalkotos
          directory parameter.
37 3b2f6619 Nikos Skalkotos

38 3b2f6619 Nikos Skalkotos
        * ftype: The action will only be performed on files of this
39 3b2f6619 Nikos Skalkotos
          type. For a list of all allowed filetypes, see here:
40 3b2f6619 Nikos Skalkotos
          http://libguestfs.org/guestfs.3.html#guestfs_readdir
41 3b2f6619 Nikos Skalkotos

42 3b2f6619 Nikos Skalkotos
        * exclude: Exclude all files that follow this pattern.
43 3b2f6619 Nikos Skalkotos
        """
44 8c574358 Nikos Skalkotos
        maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth']
45 8c574358 Nikos Skalkotos
        if maxdepth == 0:
46 8c574358 Nikos Skalkotos
            return
47 8c574358 Nikos Skalkotos
48 8c574358 Nikos Skalkotos
        # maxdepth -= 1
49 8c574358 Nikos Skalkotos
        maxdepth = None if maxdepth is None else maxdepth - 1
50 8c574358 Nikos Skalkotos
        kargs['maxdepth'] = maxdepth
51 8c574358 Nikos Skalkotos
52 8c574358 Nikos Skalkotos
        exclude = None if 'exclude' not in kargs else kargs['exclude']
53 8c574358 Nikos Skalkotos
        ftype = None if 'ftype' not in kargs else kargs['ftype']
54 8c574358 Nikos Skalkotos
        has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
55 8c574358 Nikos Skalkotos
56 8c574358 Nikos Skalkotos
        for f in self.g.readdir(directory):
57 8c574358 Nikos Skalkotos
            if f['name'] in ('.', '..'):
58 8c574358 Nikos Skalkotos
                continue
59 8c574358 Nikos Skalkotos
60 8c574358 Nikos Skalkotos
            full_path = "%s/%s" % (directory, f['name'])
61 8c574358 Nikos Skalkotos
62 8c574358 Nikos Skalkotos
            if exclude and re.match(exclude, full_path):
63 8c574358 Nikos Skalkotos
                continue
64 8c574358 Nikos Skalkotos
65 8c574358 Nikos Skalkotos
            if has_ftype(f, 'd'):
66 8c574358 Nikos Skalkotos
                self.foreach_file(full_path, action, **kargs)
67 8c574358 Nikos Skalkotos
68 8c574358 Nikos Skalkotos
            if has_ftype(f, ftype):
69 8c574358 Nikos Skalkotos
                action(full_path)
70 0d5a999d Nikos Skalkotos
71 aa2062ba Nikos Skalkotos
    def get_metadata(self):
72 a1a5ca4b Nikos Skalkotos
        """Returns some descriptive metadata about the OS."""
73 aa2062ba Nikos Skalkotos
        meta = {}
74 c408053f Nikos Skalkotos
        meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
75 c408053f Nikos Skalkotos
        meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
76 c408053f Nikos Skalkotos
        meta['OS'] = self.g.inspect_get_distro(self.root)
77 c408053f Nikos Skalkotos
        meta['description'] = self.g.inspect_get_product_name(self.root)
78 aa2062ba Nikos Skalkotos
79 aa2062ba Nikos Skalkotos
        return meta
80 486ea6e0 Nikos Skalkotos
81 8c574358 Nikos Skalkotos
    def data_cleanup(self):
82 a1a5ca4b Nikos Skalkotos
        """Cleanup sensitive data out of the OS image."""
83 aa2062ba Nikos Skalkotos
        raise NotImplementedError
84 aa2062ba Nikos Skalkotos
85 aa2062ba Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :