Statistics
| Branch: | Tag: | Revision:

root / image_creator / os_type / __init__.py @ 3b2f6619

History | View | Annotate | Download (2.6 kB)

1
#!/usr/bin/env python
2

    
3
import re
4

    
5

    
6
def add_prefix(target):
7
    def wrapper(self, *args):
8
        prefix = args[0]
9
        return map(lambda x: prefix + x, target(self, *args))
10
    return wrapper
11

    
12

    
13
class OSBase(object):
14
    """Basic operating system class"""
15
    def __init__(self, rootdev, ghandler):
16
        self.root = rootdev
17
        self.g = ghandler
18

    
19
    @add_prefix
20
    def ls(self, directory):
21
        """List the name of all files under a directory"""
22
        return self.g.ls(directory)
23

    
24
    @add_prefix
25
    def find(self, directory):
26
        """List the name of all files recursively under a directory"""
27
        return self.g.find(directory)
28

    
29
    def foreach_file(self, directory, action, **kargs):
30
        """Perform an action recursively on all files under a directory.
31

32
        The following options are allowed:
33

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

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

42
        * exclude: Exclude all files that follow this pattern.
43
        """
44
        maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth']
45
        if maxdepth == 0:
46
            return
47

    
48
        # maxdepth -= 1
49
        maxdepth = None if maxdepth is None else maxdepth - 1
50
        kargs['maxdepth'] = maxdepth
51

    
52
        exclude = None if 'exclude' not in kargs else kargs['exclude']
53
        ftype = None if 'ftype' not in kargs else kargs['ftype']
54
        has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
55

    
56
        for f in self.g.readdir(directory):
57
            if f['name'] in ('.', '..'):
58
                continue
59

    
60
            full_path = "%s/%s" % (directory, f['name'])
61

    
62
            if exclude and re.match(exclude, full_path):
63
                continue
64

    
65
            if has_ftype(f, 'd'):
66
                self.foreach_file(full_path, action, **kargs)
67

    
68
            if has_ftype(f, ftype):
69
                action(full_path)
70

    
71
    def get_metadata(self):
72
        """Returnes some descriptive metadata of the OS."""
73
        meta = {}
74
        meta["OSFAMILY"] = self.g.inspect_get_type(self.root)
75
        meta["OS"] = self.g.inspect_get_distro(self.root)
76
        meta["description"] = self.g.inspect_get_product_name(self.root)
77

    
78
        return meta
79

    
80
    def data_cleanup(self):
81
        """Cleanup sesitive data out of the OS image."""
82
        raise NotImplementedError
83

    
84
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :