1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import os
20 import re
21 import shutil
22
23 import VMBuilder
24 import VMBuilder.util as util
25 from VMBuilder.exception import VMBuilderException
26
28 for plugin in find_plugins():
29 exec "import %s" % plugin
30
32 retval = []
33 for plugin_dir in __path__:
34 for p in os.listdir(plugin_dir):
35 path = '%s/%s' % (plugin_dir, p)
36 if os.path.isdir(path) and os.path.isfile('%s/__init__.py' % path):
37 retval.append("VMBuilder.plugins.%s" % p)
38 return retval
39
41 priority = 10
42
44 self.context = context
45 self._setting_groups = []
46 self.register_options()
47
50
53
55 """
56 Override this method with checks for anything that might cause the VM creation to fail
57
58 raise an exception if you can see already that this won't work
59 """
60 pass
61
62 - def post_install(self):
63 """
64 This is called just after the distro is installed, before it gets copied to the fs images.
65 """
66 pass
67
68 - def install_file(self, path, contents=None, source=None, mode=None):
69 fullpath = '%s%s' % (self.context.chroot_dir, path)
70 if not os.path.isdir(os.path.dirname(fullpath)):
71 os.makedirs(os.path.dirname(fullpath))
72 if source and not contents:
73 shutil.copy(source, fullpath)
74 else:
75 fp = open(fullpath, 'w')
76 fp.write(contents)
77 fp.close()
78 if mode:
79 os.chmod(fullpath, mode)
80 return fullpath
81
84
86 return util.run_cmd('chroot', self.chroot_dir, *args, **kwargs)
87
90
91
93 - def __init__(self, plugin, context, name):
94
95 self.plugin = plugin
96
97 self.context = context
98
99 self.name = name
100
101 self._settings = []
102
104
105
106 if 'type' in kwargs:
107 type = kwargs['type']
108 del kwargs['type']
109 else:
110 type = 'str'
111
112 if type == 'str':
113 setting = self.plugin.StringSetting(self, *args, **kwargs)
114 elif type == 'bool':
115 setting = self.plugin.BooleanSetting(self, *args, **kwargs)
116 elif type == 'list':
117 setting = self.plugin.ListSetting(self, *args, **kwargs)
118 elif type == 'int':
119 setting = self.plugin.IntSetting(self, *args, **kwargs)
120 else:
121 raise VMBuilderException("Unknown setting type: '%s' (Plugin: '%s', Setting group: '%s', Setting: '%s')" %
122 (type,
123 self.plugin.__module__,
124 self.name,
125 args[0]))
126 self._settings.append(setting)
127
129 default = None
130
131 - def __init__(self, setting_group, name, metavar=None, help=None, extra_args=None, valid_options=None, action=None, **kwargs):
158
160 """
161 If a value has previously been set, return it.
162 If not, return the default value.
163 """
164
165 if self.value_set:
166 return self.value
167 else:
168 return self.default
169
171 """
172 Checks the value's validity.
173 """
174 if self.valid_options is not None:
175 if value not in self.valid_options:
176 raise VMBuilderException('%r is not a valid option for %s. Valid options are: %s' % (value, self.name, ' '.join(self.valid_options)))
177 else:
178 return self.check_value(value)
179
181 return self.valid_options
182
184 """
185 Set the list of valid options for this setting.
186 """
187 if not type(valid_options) == list and valid_options is not None:
188 raise VMBuilderException('set_valid_options only accepts lists or None')
189 if valid_options:
190 for option in valid_options:
191 self.check_value(option)
192 self.valid_options = valid_options
193
195 """
196 Return the default value.
197 """
198 return self.default
199
201 """
202 Set a new default value.
203 """
204 value = self.do_check_value(value)
205 self.default = value
206
208 """
209 Set new value.
210
211 Contrary to L{set_value}, L{set_value_fuzzy} will attempt
212 to turn L{value} into the target type. E.g. turning '10'
213 into 10, "main,universe,multiverse" into ['main',
214 'universe', 'multiverse']
215 """
216 return self.set_value(value)
217
219 """
220 Set a new value.
221 """
222 value = self.do_check_value(value)
223 self.value = value
224 self.value_set = True
225
230
232 if len(value) == 1 and type(value[0]) == str:
233 value = value[0]
234 if type(value) == str:
235 if value == '':
236 return self.set_value([])
237 for sep in [':', ',']:
238 if sep in value:
239 split_regex = re.compile("\s*%s\s*" % sep)
240 return self.set_value(split_regex.split(value))
241 value = [value]
242 self.set_value(value)
243 return self.set_value(value)
244
246 if not type(value) == list:
247 raise VMBuilderException('%r is type %s, expected list.' % (value, type(value)))
248 return value
249
252 if type(value) != int:
253 try:
254 value = int(value)
255 except ValueError:
256 raise VMBuilderException('Could not interpret %r as an int.' % (value,))
257 return self.set_value(value)
258
260 if not type(value) == int:
261 raise VMBuilderException('%r is type %s, expected int.' % (value, type(value)))
262 return value
263
266 if type(value) == str:
267 if value.lower() in ['no', 'false', 'off', '0']:
268 value = False
269 elif value.lower() in ['yes', 'true', 'on', '1']:
270 value = True
271 else:
272 raise VMBuilderException('Could not interpret %r as a boolean value.' % (value,))
273 return self.set_value(value)
274
276 if not type(value) == bool:
277 raise VMBuilderException('%r is type %s, expected bool.' % (value, type(value)))
278 return value
279
282 if not type(value) == str:
283 raise VMBuilderException('%r is type %s, expected str.' % (value, type(value)))
284 return value
285
290
292 return name in self.context._config
293
298
304
309
314
319
324
329