root / image_creator / os_type / __init__.py @ ae48a082
History | View | Annotate | Download (4.2 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import re |
35 |
|
36 |
|
37 |
def add_prefix(target): |
38 |
def wrapper(self, *args): |
39 |
prefix = args[0]
|
40 |
return map(lambda x: prefix + x, target(self, *args)) |
41 |
return wrapper
|
42 |
|
43 |
|
44 |
class OSBase(object): |
45 |
"""Basic operating system class"""
|
46 |
def __init__(self, rootdev, ghandler): |
47 |
self.root = rootdev
|
48 |
self.g = ghandler
|
49 |
|
50 |
@add_prefix
|
51 |
def ls(self, directory): |
52 |
"""List the name of all files under a directory"""
|
53 |
return self.g.ls(directory) |
54 |
|
55 |
@add_prefix
|
56 |
def find(self, directory): |
57 |
"""List the name of all files recursively under a directory"""
|
58 |
return self.g.find(directory) |
59 |
|
60 |
def foreach_file(self, directory, action, **kargs): |
61 |
"""Perform an action recursively on all files under a directory.
|
62 |
|
63 |
The following options are allowed:
|
64 |
|
65 |
* maxdepth: If defined the action will not be performed on
|
66 |
files that are below this level of directories under the
|
67 |
directory parameter.
|
68 |
|
69 |
* ftype: The action will only be performed on files of this
|
70 |
type. For a list of all allowed filetypes, see here:
|
71 |
http://libguestfs.org/guestfs.3.html#guestfs_readdir
|
72 |
|
73 |
* exclude: Exclude all files that follow this pattern.
|
74 |
"""
|
75 |
maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth'] |
76 |
if maxdepth == 0: |
77 |
return
|
78 |
|
79 |
# maxdepth -= 1
|
80 |
maxdepth = None if maxdepth is None else maxdepth - 1 |
81 |
kargs['maxdepth'] = maxdepth
|
82 |
|
83 |
exclude = None if 'exclude' not in kargs else kargs['exclude'] |
84 |
ftype = None if 'ftype' not in kargs else kargs['ftype'] |
85 |
has_ftype = lambda x, y: y is None and True or x['ftyp'] == y |
86 |
|
87 |
for f in self.g.readdir(directory): |
88 |
if f['name'] in ('.', '..'): |
89 |
continue
|
90 |
|
91 |
full_path = "%s/%s" % (directory, f['name']) |
92 |
|
93 |
if exclude and re.match(exclude, full_path): |
94 |
continue
|
95 |
|
96 |
if has_ftype(f, 'd'): |
97 |
self.foreach_file(full_path, action, **kargs)
|
98 |
|
99 |
if has_ftype(f, ftype):
|
100 |
action(full_path) |
101 |
|
102 |
def get_metadata(self): |
103 |
"""Returns some descriptive metadata about the OS."""
|
104 |
meta = {} |
105 |
meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root) |
106 |
meta['OSFAMILY'] = self.g.inspect_get_type(self.root) |
107 |
meta['OS'] = self.g.inspect_get_distro(self.root) |
108 |
meta['description'] = self.g.inspect_get_product_name(self.root) |
109 |
|
110 |
return meta
|
111 |
|
112 |
def data_cleanup(self): |
113 |
"""Cleanup sensitive data out of the OS image."""
|
114 |
raise NotImplementedError |
115 |
|
116 |
def sysprep(self): |
117 |
"""Prepere system for image creation."""
|
118 |
raise NotImplementedError |
119 |
|
120 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|