Add mail and /var/tmp cleanup for unix systems
[snf-image-creator] / image_creator / os_type / __init__.py
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         """Returns some descriptive metadata about the OS."""
73         meta = {}
74         meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
75         meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
76         meta['OS'] = self.g.inspect_get_distro(self.root)
77         meta['description'] = self.g.inspect_get_product_name(self.root)
78
79         return meta
80
81     def data_cleanup(self):
82         """Cleanup sensitive data out of the OS image."""
83         raise NotImplementedError
84
85 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :