2
# ExtLinuxConf.py - Simple syslinux config parsing
4
# Copyright 2010 Citrix Systems Ltd.
6
# This software may be freely redistributed under the terms of the GNU
7
# general public license.
9
# You should have received a copy of the GNU General Public License
10
# along with this program; if not, write to the Free Software
11
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
class ExtLinuxImage(object):
19
def __init__(self, lines, path):
20
self.reset(lines, path)
27
" initrd: %s\n" %(self.title, self.root, self.kernel,
28
self.args, self.initrd))
29
def reset(self, lines, path):
30
self._initrd = self._kernel = self._readonly = None
36
map(self.set_from_line, lines)
38
def set_from_line(self, line, replace = None):
39
(com, arg) = GrubConf.grub_exact_split(line, 2)
42
# Special handling for mboot.c32
43
if com.lower() == "append" and self.kernel is not None:
44
(_,kernel) = self.kernel
45
if kernel.endswith("mboot.c32"):
49
modules = arg.split("---")
51
if len(modules) == 3: # Assume Xen + Kernel + Initrd
52
(_,kernel,initrd) = modules
53
elif len(modules) == 2: # Assume Kernel + Initrd
54
(kernel,initrd) = modules
57
setattr(self, "kernel", kernel.strip())
59
setattr(self, "initrd", initrd.strip())
61
# Bypass regular self.commands handling
64
if com is not None and self.commands.has_key(com):
65
if self.commands[com] is not None:
66
setattr(self, self.commands[com], re.sub('^"(.+)"$', r"\1", arg.strip()))
68
logging.info("Ignored image directive %s" %(com,))
70
logging.warning("Unknown image directive %s" %(com,))
72
# now put the line in the list of lines
74
self.lines.append(line)
76
self.lines.pop(replace)
77
self.lines.insert(replace, line)
79
def set_kernel(self, val):
80
if val.find(" ") == -1:
81
self._kernel = (None,val)
84
(kernel, args) = val.split(None, 1)
85
self._kernel = (None,kernel)
91
kernel = property(get_kernel, set_kernel)
92
args = property(get_args)
94
def set_initrd(self, val):
95
self._initrd = (None,val)
98
initrd = property(get_initrd, set_initrd)
100
def set_readonly(self, val):
102
def get_readonly(self):
103
return self._readonly
104
readonly = property(get_readonly, set_readonly)
106
# set up command handlers
112
class ExtLinuxConfigFile(object):
113
def __init__(self, fn = None):
122
def parse(self, buf = None):
124
if self.filename is None:
125
raise ValueError, "No config file defined to parse!"
127
f = open(self.filename, 'r')
128
lines = f.readlines()
131
lines = buf.split("\n")
133
path = os.path.dirname(self.filename)
141
if l.startswith('#'):
144
if l.lower().startswith("label"):
146
self.add_image(ExtLinuxImage(img, path))
154
(com, arg) = GrubConf.grub_exact_split(l, 2)
156
if self.commands.has_key(com):
157
if self.commands[com] is not None:
158
setattr(self, self.commands[com], arg.strip())
160
logging.info("Ignored directive %s" %(com,))
162
logging.warning("Unknown directive %s" %(com,))
165
self.add_image(ExtLinuxImage(img, path))
167
def hasPassword(self):
170
def hasPasswordAccess(self):
173
def add_image(self, image):
174
self.images.append(image)
176
def _get_default(self):
177
for i in range(len(self.images)):
178
if self.images[i].title == self._default:
181
def _set_default(self, val):
183
default = property(_get_default, _set_default)
185
commands = { "default": "default",
186
"timeout": "timeout",
194
if __name__ == "__main__":
196
raise RuntimeError, "Need a configuration file to read"
197
g = ExtLinuxConfigFile(sys.argv[1])