summaryrefslogtreecommitdiffstats
path: root/lilo.py
blob: a79101cdb16a954a15ad18b955ae38607bc8d252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import string
import os

class LiloConfiguration:
    def __repr__ (self, tab = 0):
	s = ""
	for n in self.order:
	    if (tab):
		s = s + '\t'
	    if n[0] == '#':
		s = s + n[1:]
	    else:
		s = s + n
		if self.items[n]:
		    s = s + "=" + self.items[n]
	    s = s + '\n'
	for image in self.images:
	    (type, name, cl) = image
	    s = s + "\n%s=%s\n" % (type, name)
	    s = s + cl.__repr__(1)
	return s

    def addEntry(self, item, val = None):
	if not self.items.has_key(item):
	    self.order.append(item)
	if (val):
	    self.items[item] = str(val)
	else:
	    self.items[item] = None

    def addImage (self, type, name, config):
	self.images.append((type, name, config))

    def delImage (self, name):
        for entry in self.images:
            type, label, config = entry
            if label == name:
                self.images.remove (entry)

    def write(self, file):
	f = open(file, "w")
	f.write(self.__repr__())
	f.close()
	os.chmod(file, 0644)

    def read (self, file):
	f = open(file, "r")
	image = None
	for l in f.readlines():
	    l = l[:-1]
	    orig = l
	    while (l and (l[0] == ' ' or l[0] == '\t')):
		l = l[1:]
	    if (not l or l[0] == '#'):
		self.order.append('#' + orig)
		continue
	    fields = string.split(l, '=', 1)
	    if (len(fields) == 2):
		if (fields[0] == "image"):
		    image = LiloConfiguration()
		    self.addImage(fields[0], fields[1], image)
		    args = None
		elif (fields[0] == "other"):
		    image = LiloConfiguration()
		    self.addImage(fields[0], fields[1], image)
		    args = None
                else:
		    args = (fields[0], fields[1])
	    else:
		args = (l,)

	    if (args and image):
		apply(image.addEntry, args)
	    elif args:
		apply(self.addEntry, args)
	    
	f.close()

    def __init__(self):
	self.order = []
	self.images = []		# more (type, name, LiloConfiguration) pair
	self.items = {}

if __name__ == "__main__":
    config = LiloConfiguration ()
    config.read ('lilo.conf')
    print config
    config.delImage ('/boot/vmlinuz-2.2.5-15')
    print '----------------------------------'
    print config
    config.delImage ('/dev/hda3')
    print '----------------------------------'    
    print config