summaryrefslogtreecommitdiffstats
path: root/comps.py
blob: 85af5788479303a43210c67511f29fc80946cfe5 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import sys
import rpm
import os
from string import *
import types
import iutil
import urllib

XFreeServerPackages = { 'XFree86-3DLabs' : 1, 	'XFree86-8514' : 1,
			'XFree86-AGX' : 1, 	'XFree86-I128' : 1,
			'XFree86-Mach32' : 1, 	'XFree86-Mach64' : 1,
			'XFree86-Mach8' : 1, 	'XFree86-Mono' : 1,
			'XFree86-P9000' : 1, 	'XFree86-S3' : 1,
			'XFree86-S3V' : 1, 	'XFree86-SVGA' : 1,
			'XFree86-W32' : 1,	'XFree86-Sun' : 1,
			'XFree86-SunMono' : 1,	'XFree86-Sun24' : 1 }

class Package:
    def __getitem__(self, item):
	return self.h[item]

    def __repr__(self):
	return self.name

    def __init__(self, header):
	self.h = header
	self.name = header[rpm.RPMTAG_NAME]
	self.selected = 0

class HeaderList:
    def selected(self):
	l = []
 	keys = self.packages.keys()
	keys.sort()
	for name in keys:
	    if self.packages[name].selected: l.append(self.packages[name])
	return l

    def has_key(self, item):
	return self.packages.has_key(item)

    def keys(self):
        return self.packages.keys()

    def __getitem__(self, item):
	return self.packages[item]

    def __init__(self, hdlist):
        self.hdlist = hdlist
	self.packages = {}
	for h in hdlist:
	    name = h[rpm.RPMTAG_NAME]
	    score1 = rpm.archscore(h['arch'])
	    if (score1):
		if self.packages.has_key(name):
		    score2 = rpm.archscore(self.packages[name].h['arch'])
		    if (score1 < score2):
			self.packages[name] = Package(h)
		else:
		    self.packages[name] = Package(h)

class HeaderListFromFile (HeaderList):

    def __init__(self, path):
	hdlist = rpm.readHeaderListFromFile(path)
	HeaderList.__init__(self, hdlist)

class HeaderListFD (HeaderList):
    def __init__(self, fd):
	hdlist = rpm.readHeaderListFromFD (fd)
	HeaderList.__init__(self, hdlist)

class Component:
    def __len__(self):
	return len(self.items)

    def __getitem__(self, key):
	return self.items[key]

    def addPackage(self, package):
	self.items[package] = package

    def addConditional (self, condition, package):
        if self.conditional.has_key (condition):
            self.conditional[condition].append (package)
        else:
            self.conditional[condition] = [ package ]

    def addInclude(self, component):
	self.includes.append(component)
	
    def addRequires(self, component):
	self.requires = component

    def conditionalSelect (self, key):
        for pkg in self.conditional[key]:
            pkg.selected = 1

    def conditionalUnselect (self, key):
        for pkg in self.conditional[key]:
            pkg.selected = 0

    def select(self, recurse = 1):
        self.selected = 1
	for pkg in self.items.keys ():
	    self.items[pkg].selected = 1
        # turn on any conditional packages
        for (condition, pkgs) in self.conditional.items ():
            if condition.selected:
                for pkg in pkgs:
                    pkg.selected = 1

        # components that have conditional packages keyed on this
        # component AND are currently selected have those conditional
        # packages turned turned on when this component is turned on.
        if self.dependents:
            for dep in self.dependents:
                if dep.selected:
                    dep.conditionalSelect (self)
                
	if recurse:
	    for n in self.includes:
		if n.requires:
		    if n.requires.selected:
			n.select(recurse)
	        else:
		    n.select(recurse)
		if n.requires:
		    if n.requires.selected:
			n.select(recurse)
	        else:
		    n.select(recurse)

    def unselect(self, recurse = 1):
        self.selected = 0
	for n in self.items.keys ():
	    self.items[n].selected = 0

        # always turn off conditional packages, regardless
        # if the condition is met or not.
        for (condition, pkgs) in self.conditional.items ():
            for pkg in pkgs:
                pkg.selected = 0

        # now we must turn off conditional packages in components that
        # are keyed on this package
        if self.dependents:
            for dep in self.dependents:
                dep.conditionalUnselect (self)

	if recurse:
	    for n in self.includes:
		n.unselect(recurse)

    def __init__(self, name, selected, hidden = 0):
	self.name = name
	self.hidden = hidden
	self.selected = selected
	self.items = {}
        self.conditional = {}
        self.dependents = []
	self.requires = None
	self.includes = []

class ComponentSet:
    def __len__(self):
	return len(self.comps)

    def __getitem__(self, key):
	if (type(key) == types.IntType):
	    return self.comps[key]
	return self.compsDict[key]

    def keys(self):
	return self.compsDict.keys()

    def exprMatch(self, expr, arch1, arch2):
	if os.environ.has_key('LANG'):
	    lang = os.environ['LANG']
	else:
	    lang = None

	if expr[0] != '(':
	    raise ValueError, "leading ( expected"
	expr = expr[1:]
	if expr[len(expr) - 1] != ')':
	    raise ValueError, "bad kickstart file [missing )]"
	expr = expr[:len(expr) - 1]

	exprList = split(expr, 'and')
	truth = 1
	for expr in exprList:
	    l = split(expr)

	    if l[0] == "lang":
		if len(l) != 2:
		    raise ValueError, "too many arguments for lang"
		if l[1] != lang:
		    newTruth = 0
		else:
		    newTruth = 1
	    elif l[0] == "arch":
		if len(l) != 2:
		    raise ValueError, "too many arguments for arch"
		newTruth = self.archMatch(l[1], arch1, arch2)
	    else:
		s = "unknown condition type %s" % (l[0],)
		raise ValueError, s

	    truth = truth and newTruth

	return truth

    # this checks to see if "item" is one of the archs
    def archMatch(self, item, arch1, arch2):
	if item == arch1 or (arch2 and item == arch2): 
	    return 1
	return 0

    def readCompsFile(self, filename, packages):
	arch = iutil.getArch()
	arch2 = None
	if arch == "sparc" and os.uname ()[4] == "sparc64":
	    arch2 = "sparc64"
	file = urllib.urlopen(filename)
	lines = file.readlines()

	file.close()
	top = lines[0]
	lines = lines[1:]
	if (top != "3\n" and top != "4\n"):
	    raise TypeError, "comp file version 3 or 4 expected"
	
	comp = None
        conditional = None
	self.comps = []
	self.compsDict = {}
	for l in lines:
	    l = strip (l)
	    if (not l): continue

	    if (find(l, ":") > -1):
		(archList, l) = split(l, ":", 1)
		if archList[0] == '(':
		    l = strip(l)
		    if not self.exprMatch(archList, arch, arch2):
			continue
		else:
		    while (l[0] == " "): l = l[1:]

		    skipIfFound = 0
		    if (archList[0] == '!'):
			skipIfFound = 1
			archList = archList[1:]
		    archList = split(archList)
		    found = 0
		    for n in archList:
			if self.archMatch(n, arch, arch2):	
			    found = 1
			    break
		    if ((found and skipIfFound) or 
			(not found and not skipIfFound)):
			continue

	    if (find(l, "?") > -1):
                (trash, cond) = split (l, '?', 1)
                (cond, trash) = split (cond, '{', 1)
                conditional = self.compsDict[strip (cond)]
                continue

	    if (comp == None):
		(default, l) = split(l, None, 1)
		hidden = 0
		if (l[0:6] == "--hide"):
		    hidden = 1
		    (foo, l) = split(l, None, 1)
                (l, trash) = split(l, '{', 1)
                l = strip (l)
                if l == "Base":
                    hidden = 1
		comp = Component(l, default == '1', hidden)
	    elif (l == "}"):
                if conditional:
		    if comp.conditional.has_key (conditional):
			conditional.dependents.append (comp)
                    conditional = None
                else:
                    self.comps.append(comp)
                    self.compsDict[comp.name] = comp
                    comp = None
	    else:
		if (l[0] == "@"):
		    (at, l) = split(l, None, 1)
		    comp.addInclude(self.compsDict[l])
		else:
                    if conditional:
                        comp.addConditional (conditional, packages[l])
                    else:
                        comp.addPackage(packages[l])
                    
        everything = Component("Everything", 0, 0)
        for package in packages.keys ():
	    if (packages[package]['name'] != 'kernel' and
	        	packages[package]['name'] != 'kernel-BOOT' and
	        	packages[package]['name'] != 'kernel-smp' and
		  not XFreeServerPackages.has_key(packages[package]['name'])):
		everything.addPackage (packages[package])
        self.comps.append (everything)
        self.compsDict["Everything"] = everything
        
    def __repr__(self):
	s = ""
	for n in self.comps:
	    s = s + "{ " + n.name + " [";
	    for include in n.includes:
		s = s + " @" + include.name
		
	    for package in n:
		s = s + " " + package
	    s = s + " ] } "

	return s

    def __init__(self, file, hdlist):
	self.packages = hdlist
	self.readCompsFile(file, self.packages)