1 | # |
---|
2 | #LiloConf.py |
---|
3 | # |
---|
4 | |
---|
5 | import sys, re, os |
---|
6 | import logging |
---|
7 | import GrubConf |
---|
8 | |
---|
9 | class LiloImage(object): |
---|
10 | def __init__(self, lines, path): |
---|
11 | self.reset(lines, path) |
---|
12 | |
---|
13 | def __repr__(self): |
---|
14 | return ("title: %s\n" |
---|
15 | " root: %s\n" |
---|
16 | " kernel: %s\n" |
---|
17 | " args: %s\n" |
---|
18 | " initrd: %s\n" %(self.title, self.root, self.kernel, |
---|
19 | self.args, self.initrd)) |
---|
20 | def reset(self, lines, path): |
---|
21 | self._root = self._initrd = self._kernel = self._args = None |
---|
22 | self.title = "" |
---|
23 | self.lines = [] |
---|
24 | self.path = path |
---|
25 | map(self.set_from_line, lines) |
---|
26 | self.root = "" # dummy |
---|
27 | |
---|
28 | def set_from_line(self, line, replace = None): |
---|
29 | (com, arg) = GrubConf.grub_exact_split(line, 2) |
---|
30 | |
---|
31 | if self.commands.has_key(com): |
---|
32 | if self.commands[com] is not None: |
---|
33 | exec("%s = r\'%s\'" %(self.commands[com], re.sub('^"(.+)"$', r"\1", arg.strip()))) |
---|
34 | else: |
---|
35 | logging.info("Ignored image directive %s" %(com,)) |
---|
36 | else: |
---|
37 | logging.warning("Unknown image directive %s" %(com,)) |
---|
38 | |
---|
39 | # now put the line in the list of lines |
---|
40 | if replace is None: |
---|
41 | self.lines.append(line) |
---|
42 | else: |
---|
43 | self.lines.pop(replace) |
---|
44 | self.lines.insert(replace, line) |
---|
45 | |
---|
46 | def set_kernel(self, val): |
---|
47 | self._kernel = (None, self.path + "/" + val) |
---|
48 | def get_kernel(self): |
---|
49 | return self._kernel |
---|
50 | kernel = property(get_kernel, set_kernel) |
---|
51 | |
---|
52 | def set_initrd(self, val): |
---|
53 | self._initrd = (None, self.path + "/" + val) |
---|
54 | def get_initrd(self): |
---|
55 | return self._initrd |
---|
56 | initrd = property(get_initrd, set_initrd) |
---|
57 | |
---|
58 | # set up command handlers |
---|
59 | commands = { "label": "self.title", |
---|
60 | "root": "self.root", |
---|
61 | "rootnoverify": "self.root", |
---|
62 | "image": "self.kernel", |
---|
63 | "initrd": "self.initrd", |
---|
64 | "append": "self.args", |
---|
65 | "read-only": None, |
---|
66 | "chainloader": None, |
---|
67 | "module": None} |
---|
68 | |
---|
69 | class LiloConfigFile(object): |
---|
70 | def __init__(self, fn = None): |
---|
71 | self.filename = fn |
---|
72 | self.images = [] |
---|
73 | self.timeout = -1 |
---|
74 | self._default = 0 |
---|
75 | |
---|
76 | if fn is not None: |
---|
77 | self.parse() |
---|
78 | |
---|
79 | def parse(self, buf = None): |
---|
80 | if buf is None: |
---|
81 | if self.filename is None: |
---|
82 | raise ValueError, "No config file defined to parse!" |
---|
83 | |
---|
84 | f = open(self.filename, 'r') |
---|
85 | lines = f.readlines() |
---|
86 | f.close() |
---|
87 | else: |
---|
88 | lines = buf.split("\n") |
---|
89 | |
---|
90 | path = os.path.dirname(self.filename) |
---|
91 | img = [] |
---|
92 | for l in lines: |
---|
93 | l = l.strip() |
---|
94 | # skip blank lines |
---|
95 | if len(l) == 0: |
---|
96 | continue |
---|
97 | # skip comments |
---|
98 | if l.startswith('#'): |
---|
99 | continue |
---|
100 | # new image |
---|
101 | if l.startswith("image"): |
---|
102 | if len(img) > 0: |
---|
103 | self.add_image(LiloImage(img, path)) |
---|
104 | img = [l] |
---|
105 | continue |
---|
106 | |
---|
107 | if len(img) > 0: |
---|
108 | img.append(l) |
---|
109 | continue |
---|
110 | |
---|
111 | (com, arg) = GrubConf.grub_exact_split(l, 2) |
---|
112 | if self.commands.has_key(com): |
---|
113 | if self.commands[com] is not None: |
---|
114 | exec("%s = r\"%s\"" %(self.commands[com], arg.strip())) |
---|
115 | else: |
---|
116 | logging.info("Ignored directive %s" %(com,)) |
---|
117 | else: |
---|
118 | logging.warning("Unknown directive %s" %(com,)) |
---|
119 | |
---|
120 | if len(img) > 0: |
---|
121 | self.add_image(LiloImage(img, path)) |
---|
122 | |
---|
123 | def add_image(self, image): |
---|
124 | self.images.append(image) |
---|
125 | |
---|
126 | def _get_default(self): |
---|
127 | for i in range(0, len(self.images) - 1): |
---|
128 | if self.images[i].title == self._default: |
---|
129 | return i |
---|
130 | return 0 |
---|
131 | def _set_default(self, val): |
---|
132 | self._default = val |
---|
133 | default = property(_get_default, _set_default) |
---|
134 | |
---|
135 | commands = { "default": "self.default", |
---|
136 | "timeout": "self.timeout", |
---|
137 | "prompt": None, |
---|
138 | "relocatable": None, |
---|
139 | } |
---|
140 | |
---|
141 | if __name__ == "__main__": |
---|
142 | if sys.argv < 2: |
---|
143 | raise RuntimeError, "Need a grub.conf to read" |
---|
144 | g = LiloConfigFile(sys.argv[1]) |
---|
145 | for i in g.images: |
---|
146 | print i #, i.title, i.root, i.kernel, i.args, i.initrd |
---|
147 | print g.default |
---|