~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/pygrub/src/ExtLinuxConf.py

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# ExtLinuxConf.py - Simple syslinux config parsing
 
3
#
 
4
# Copyright 2010 Citrix Systems Ltd.
 
5
#
 
6
# This software may be freely redistributed under the terms of the GNU
 
7
# general public license.
 
8
#
 
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.
 
12
#
 
13
 
 
14
import sys, re, os
 
15
import logging
 
16
import GrubConf
 
17
 
 
18
class ExtLinuxImage(object):
 
19
    def __init__(self, lines, path):
 
20
        self.reset(lines, path)
 
21
 
 
22
    def __repr__(self):
 
23
        return ("title: %s\n"
 
24
                "  root: %s\n"
 
25
                "  kernel: %s\n"
 
26
                "  args: %s\n"
 
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
 
31
        self._args = ""
 
32
        self.title = ""
 
33
        self.lines = []
 
34
        self.path = path
 
35
        self.root = ""
 
36
        map(self.set_from_line, lines)
 
37
 
 
38
    def set_from_line(self, line, replace = None):
 
39
        (com, arg) = GrubConf.grub_exact_split(line, 2)
 
40
        com = com.lower()
 
41
 
 
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"):
 
46
                kernel = None
 
47
                args = None
 
48
                initrd = None
 
49
                modules = arg.split("---")
 
50
 
 
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
 
55
 
 
56
                if kernel:
 
57
                    setattr(self, "kernel", kernel.strip())
 
58
                if initrd:
 
59
                    setattr(self, "initrd", initrd.strip())
 
60
 
 
61
                # Bypass regular self.commands handling
 
62
                com = None
 
63
 
 
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()))
 
67
            else:
 
68
                logging.info("Ignored image directive %s" %(com,))
 
69
        elif com is not None:
 
70
            logging.warning("Unknown image directive %s" %(com,))
 
71
 
 
72
        # now put the line in the list of lines
 
73
        if replace is None:
 
74
            self.lines.append(line)
 
75
        else:
 
76
            self.lines.pop(replace)
 
77
            self.lines.insert(replace, line)
 
78
 
 
79
    def set_kernel(self, val):
 
80
        if val.find(" ") == -1:
 
81
            self._kernel = (None,val)
 
82
            self._args = None
 
83
            return
 
84
        (kernel, args) = val.split(None, 1)
 
85
        self._kernel = (None,kernel)
 
86
        self._args = args
 
87
    def get_kernel(self):
 
88
        return self._kernel
 
89
    def get_args(self):
 
90
        return self._args
 
91
    kernel = property(get_kernel, set_kernel)
 
92
    args = property(get_args)
 
93
 
 
94
    def set_initrd(self, val):
 
95
        self._initrd = (None,val)
 
96
    def get_initrd(self):
 
97
        return self._initrd
 
98
    initrd = property(get_initrd, set_initrd)
 
99
 
 
100
    def set_readonly(self, val):
 
101
        self._readonly = 1
 
102
    def get_readonly(self):
 
103
        return self._readonly
 
104
    readonly = property(get_readonly, set_readonly)
 
105
 
 
106
    # set up command handlers
 
107
    commands = {
 
108
        "label": "title",
 
109
        "kernel": "kernel",
 
110
        "append": "args"}
 
111
 
 
112
class ExtLinuxConfigFile(object):
 
113
    def __init__(self, fn = None):
 
114
        self.filename = fn
 
115
        self.images = []
 
116
        self.timeout = -1
 
117
        self._default = 0
 
118
 
 
119
        if fn is not None:
 
120
            self.parse()
 
121
 
 
122
    def parse(self, buf = None):
 
123
        if buf is None:
 
124
            if self.filename is None:
 
125
                raise ValueError, "No config file defined to parse!"
 
126
 
 
127
            f = open(self.filename, 'r')
 
128
            lines = f.readlines()
 
129
            f.close()
 
130
        else:
 
131
            lines = buf.split("\n")
 
132
 
 
133
        path = os.path.dirname(self.filename)
 
134
        img = []
 
135
        for l in lines:
 
136
            l = l.strip()
 
137
            # skip blank lines
 
138
            if len(l) == 0:
 
139
                continue
 
140
            # skip comments
 
141
            if l.startswith('#'):
 
142
                continue
 
143
            # new image
 
144
            if l.lower().startswith("label"):
 
145
                if len(img) > 0:
 
146
                    self.add_image(ExtLinuxImage(img, path))
 
147
                img = [l]
 
148
                continue
 
149
 
 
150
            if len(img) > 0:
 
151
                img.append(l)
 
152
                continue
 
153
 
 
154
            (com, arg) = GrubConf.grub_exact_split(l, 2)
 
155
            com = com.lower()
 
156
            if self.commands.has_key(com):
 
157
                if self.commands[com] is not None:
 
158
                    setattr(self, self.commands[com], arg.strip())
 
159
                else:
 
160
                    logging.info("Ignored directive %s" %(com,))
 
161
            else:
 
162
                logging.warning("Unknown directive %s" %(com,))
 
163
 
 
164
        if len(img) > 0:
 
165
            self.add_image(ExtLinuxImage(img, path))
 
166
 
 
167
    def hasPassword(self):
 
168
        return False
 
169
 
 
170
    def hasPasswordAccess(self):
 
171
        return True
 
172
 
 
173
    def add_image(self, image):
 
174
        self.images.append(image)
 
175
 
 
176
    def _get_default(self):
 
177
        for i in range(len(self.images)):
 
178
            if self.images[i].title == self._default:
 
179
                return i
 
180
        return 0
 
181
    def _set_default(self, val):
 
182
        self._default = val
 
183
    default = property(_get_default, _set_default)
 
184
 
 
185
    commands = { "default": "default",
 
186
                 "timeout": "timeout",
 
187
                 "serial": None,
 
188
                 "prompt": None,
 
189
                 "display": None,
 
190
                 "f1": None,
 
191
                 "f2": None,
 
192
                 }
 
193
        
 
194
if __name__ == "__main__":
 
195
    if sys.argv < 2:
 
196
        raise RuntimeError, "Need a configuration file to read"
 
197
    g = ExtLinuxConfigFile(sys.argv[1])
 
198
    for i in g.images:
 
199
        print i
 
200
    print g.default