Detect Kubuntu images
[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     def __init__(self, rootdev, ghandler):
15         self.root = rootdev
16         self.g = ghandler
17
18     @add_prefix
19     def ls(self, directory):
20         return self.g.ls(directory)
21
22     @add_prefix
23     def find(self, directory):
24         return self.g.find(directory)
25
26     def foreach_file(self, directory, action, **kargs):
27
28         maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth']
29         if maxdepth == 0:
30             return
31
32         # maxdepth -= 1
33         maxdepth = None if maxdepth is None else maxdepth - 1
34         kargs['maxdepth'] = maxdepth
35
36         exclude = None if 'exclude' not in kargs else kargs['exclude']
37         ftype = None if 'ftype' not in kargs else kargs['ftype']
38         has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
39
40         for f in self.g.readdir(directory):
41             if f['name'] in ('.', '..'):
42                 continue
43
44             full_path = "%s/%s" % (directory, f['name'])
45
46             if exclude and re.match(exclude, full_path):
47                 continue
48
49             if has_ftype(f, 'd'):
50                 self.foreach_file(full_path, action, **kargs)
51
52             if has_ftype(f, ftype):
53                 action(full_path)
54
55     def get_metadata(self):
56         meta = {}
57         meta["OSFAMILY"] = self.g.inspect_get_type(self.root)
58         meta["OS"] = self.g.inspect_get_distro(self.root)
59         meta["description"] = self.g.inspect_get_product_name(self.root)
60
61         return meta
62
63     def data_cleanup(self):
64         raise NotImplementedError
65
66 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :