summaryrefslogtreecommitdiffstats
path: root/createprestorepo/packagelist.py
blob: c057f3f1277c8d7cc7c3e732ede133348800b53f (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# Copyright (c) 2007 Jonathan Dieter

import rpmUtils.miscutils
import os
import commands

class RpmItem:
    def __init__(self, nevra, f):
        (self.name, self.epoch, self.version, self.release, self.arch) = nevra
        self.epoch = str(self.epoch)
        self.filename = f
    
    def getNevra(self):
        return (self.name, self.epoch, self.version, self.release, self.arch)
    
    def __eq__(self, b):
        if b == None:
            return False
        try:
            # An RpmItem is equal to another RpmItem if the nevra's are equal
            if self.getNevra() == b.getNevra():
                return True
            else:
                return False
        except:
            # An RpmItem is equal to a DrpmItem if the nevra is equal to the drpm's destination nevra
            if self.getNevra() == b.getDstNevra():
                return True
            else:
                return False

class DrpmItem:
    def __init__(self, src_nevra, dst_nevra, f):
        (self.src_name, self.src_epoch, self.src_version, self.src_release, self.src_arch) = src_nevra
        (self.dst_name, self.dst_epoch, self.dst_version, self.dst_release, self.dst_arch) = dst_nevra
        self.src_epoch = str(self.src_epoch)
        self.dst_epoch = str(self.dst_epoch)
        self.filename = f
    
    def exists(self):
        return self.filename != None
    
    def getSrcNevra(self):
        return (self.src_name, self.src_epoch, self.src_version, self.src_release, self.src_arch)
        
    def getDstNevra(self):
        return (self.dst_name, self.dst_epoch, self.dst_version, self.dst_release, self.dst_arch)
        
    def getSrcDstNevra(self):
        return [self.getSrcNevra(), self.getDstNevra()]
        
    def __eq__(self, b):
        if b == None:
            return False
        try:
            # A DrpmItem is equal to another DrpmItem if both source and destination nevra's are equal
            if self.getSrcDstNevra() == b.getSrcDstNevra():
                return True
            else:
                return False
        except:
            # A DrpmItem is equal to an RpmItem if the destination nevra is equal to the rpm's nevra
            if self.getDstNevra() == b.getNevra():
                return True
            else:
                return False
        
            
class PackageList:
    def __init__(self, base_dir, drpm_dir, threshold, debug):
        self.base_dir  = base_dir
        self.drpm_dir   = drpm_dir
        self.threshold = threshold
        self.debug     = debug
        self.rpm_list  = {}
        self.sorted    = False
        
    def addRpm(self, rpm):
        """Add RpmItem to PackageList"""
        self.sorted = False
        rpm_key = "%s.%s" % (rpm.name, rpm.arch)
        self.rpm_list.setdefault(rpm_key, {})
        self.rpm_list[rpm_key].setdefault("rpms", [])
        found = False
        for found_rpm in self.rpm_list[rpm_key]["rpms"]:
            if rpm == found_rpm:
                found = True
                break
        if not found:
            self.rpm_list[rpm_key]["rpms"].append(rpm)

    def addDrpm(self, drpm):
        """Add DrpmItem to PackageList"""
        self.sorted = False
        rpm_key = "%s.%s" % (drpm.src_name, drpm.src_arch)
        self.rpm_list.setdefault(rpm_key, {})
        self.rpm_list[rpm_key].setdefault("drpms", [])
        found = False
        for found_drpm in self.rpm_list[rpm_key]["drpms"]:
            if drpm == found_drpm:
                found = True
                break
        if not found:
            self.rpm_list[rpm_key]["drpms"].append(drpm)

    def sort(self):
        """Sort all rpms and deltarpms in reverse order.  This is necessary before running any
        methods that try to work with rpms and deltarpms"""
        self.sorted = True
        for rpm_type in self.rpm_list.values():
            for key in rpm_type.keys():
                if key == "rpms" and len(rpm_type["rpms"]) > 1:
                    def sortByEVR(rpm1, rpm2):
                        (n1,e1,v1,r1,a1) = rpm1.getNevra()
                        (n2,e2,v2,r2,a2) = rpm2.getNevra()
                        rc = rpmUtils.miscutils.compareEVR((e1,v1,r1),(e2,v2,r2))
                        if rc == 0:
                            return 0
                        if rc > 0:
                            return -1
                        if rc < 0:
                            return 1   
                    rpm_type["rpms"].sort(sortByEVR)  # highest first in list
                elif key == "drpms" and len(rpm_type["drpms"]) > 1:
                    def sortByEVR(drpm1, drpm2, src=False):
                        if src:
                            (n1,e1,v1,r1,a1) = drpm1.getSrcNevra()
                            (n2,e2,v2,r2,a2) = drpm2.getSrcNevra()
                        else:                        
                            (n1,e1,v1,r1,a1) = drpm1.getDstNevra()
                            (n2,e2,v2,r2,a2) = drpm2.getDstNevra()
                        rc = rpmUtils.miscutils.compareEVR((e1,v1,r1),(e2,v2,r2))
                        if rc == 0:
                            if src:
                                return 0
                            else:
                                return sortByEVR(drpm1, drpm2, True)
                        if rc > 0:
                            return -1
                        if rc < 0:
                            return 1   
                    rpm_type["drpms"].sort(sortByEVR)  # highest first in list

    def dump(self):
        """Dump list of all packages and their rpms and deltarpms"""
        if not self.sorted:
            self.sort()
        temp_list = []
        for x in self.rpm_list.keys():
            temp_list.append(x)
        temp_list.sort()
        for x in temp_list:
            print x
            if self.rpm_list[x].has_key("rpms"):
                print "  RPMS:"
                for rpm in self.rpm_list[x]["rpms"]:
                    print "    %s:%s-%s" % (rpm.epoch, rpm.version, rpm.release)
            if self.rpm_list[x].has_key("drpms"):
                print "  DRPMS:"
                for drpm in self.rpm_list[x]["drpms"]:
                    print "    %s:%s-%s => %s:%s-%s" % (drpm.src_epoch, drpm.src_version, drpm.src_release, drpm.dst_epoch, drpm.dst_version, drpm.dst_release)
    
    def __doWork(self, nevra1, nevra2, f1, deltaCommand):
        (n1,e1,v1,r1,a1) = nevra1
        (n2,e2,v2,r2,a2) = nevra2
        
        deltaRPMName  = os.path.join(self.drpm_dir, '%s-%s-%s_%s-%s.%s.drpm' % (n1, v2, r2, v1, r1, a1))
        deltaCommand += deltaRPMName
        if self.debug:
            print deltaCommand
                
        if os.path.exists("%s.dontdelta" % deltaRPMName):
            f = open("%s.dontdelta" % deltaRPMName, "r")
            drpmsize = f.readline()[:-1]
            drpmsize = int(drpmsize)
            if not self.__drpmIsWorthKeeping(drpmsize, f1):
                return False
            else:
                if self.debug:
                    print "Original drpm is now worth creating...deleting %s%s.dontdelta" % (self.base_dir, deltaRPMName)
                try:
                    os.unlink("%s.dontdelta" % deltaRPMName)
                except Exception, e:
                    print "Error deleting %s.dontdelta" % os.path.basename(deltaRPMName), str(e)
                    
        (code, out) = commands.getstatusoutput(deltaCommand)
        if code:
            print "Error genDeltaRPM for %s: exitcode was %s - Reported Error: %s" % (n1, code, out)
            return False
        else:
            # Check whether or not we should keep the drpm
            drpmsize = os.path.getsize(deltaRPMName)
            if not self.__drpmIsWorthKeeping(drpmsize, f1):
                if self.debug:
                    print 'deleting %s' % (deltaRPMName)
                f = open("%s.dontdelta" % deltaRPMName, "w")
                f.write("%i\n" % os.path.getsize(deltaRPMName))
                f.close()
                try:
                    os.unlink(deltaRPMName)
                except Exception, e:
                    print "Error deleting deltarpm %s" % os.path.basename(deltaRPMName), str(e)
                return False
            else:
                if e1 == e2:
                    print 'Generated delta rpm for %s.%s - %s-%s => %s-%s' % (n1, a1, v2, r2, v1, r1)
                else:
                    print 'Generated delta rpm for %s.%s - %s:%s-%s => %s:%s-%s' % (n1, a1, e2, v2, r2, e1, v1, r1)
                drpm = DrpmItem(nevra2, nevra1, deltaRPMName)
                self.addDrpm(drpm)
                self.sort()
                if self.debug:
                    self.dump()
                return True
    
    def __combineDeltaRpm(self, combine_list, dstrpm):
        nevra1 = combine_list[-1].getDstNevra()
        nevra2 = combine_list[0].getSrcNevra()
        f1 = dstrpm.filename
        
        deltaCommand = 'combinedeltarpm '
        for drpm in combine_list:
            deltaCommand += "%s " % drpm.filename
        
        return self.__doWork(nevra1, nevra2, f1, deltaCommand)

        
    def __buildDeltaRpm(self, srcrpm, dstrpm):
        nevra1 = dstrpm.getNevra()
        nevra2 = srcrpm.getNevra()
        
        f1 = dstrpm.filename
        f2 = srcrpm.filename
        deltaCommand = 'makedeltarpm %s %s ' % (f2, f1)
        
        return self.__doWork(nevra1, nevra2, f1, deltaCommand)
    
    def __makeDeltaRpm(self, src_nevra, dstrpm, package):
        foundSrc = False
        foundDst = False
        tempQueue = []
        if package.has_key("drpms"):
            for drpm in package["drpms"]:
                if drpm.getDstNevra() == dstrpm.getNevra() and drpm.getSrcNevra() == src_nevra:
                    return True
                if drpm.getSrcNevra() == src_nevra:
                    for item in tempQueue:
                        if drpm.getDstNevra() == item.getSrcNevra():
                            combine_list = [drpm, item]
                            return self.__combineDeltaRpm(combine_list, dstrpm)
                elif drpm.getDstNevra() == dstrpm.getNevra():
                    tempQueue.append(drpm)
        
        for rpm in package["rpms"]:
            if rpm.getNevra() == src_nevra:
                return self.__buildDeltaRpm(rpm, dstrpm)
                
        print "Found nothing!"
        return False
           
                
    def makeDeltaRpms(self, DrpmsPerPackage=0, OnlyLastPackage=True, DoFirst=True):
        """Create deltarpms for packages.
           Attributes:
              OnlyLastPackage (Boolean): Only create deltarpms to point to last destination package
              DrpmsPerPackage (Int):     Number of deltarpms to make for each package (0 = all possible)
              DoFirst         (Boolean): Create deltarpm for first source package"""
        if not self.sorted:
            self.sort()
        temp_list = []
        for x in self.rpm_list.keys():
            temp_list.append(x)
        temp_list.sort()
        if self.debug:
            print "Building drpms"
        for rpm_name in temp_list:
            package = self.rpm_list[rpm_name]
            if not package.has_key("rpms") or len(package["rpms"]) == 0:
                if self.debug:
                    print "No rpms for %s: Doing nothing" % rpm_name
                continue
                
            if (not package.has_key("drpms") or len(package["drpms"]) == 0) and len(package["rpms"]) == 1:
                if self.debug:
                    print "Only one rpm available for %s: Doing nothing" % rpm_name
                continue
                
            build_list = []
            
            for rpm in package["rpms"]:
                if [rpm.getNevra(), 1] not in build_list:
                    build_list.append([rpm.getNevra(), 1])
                
            if package.has_key("drpms"):
                for drpm in package["drpms"]:
                    if [drpm.getSrcNevra(), 1] not in build_list and [drpm.getSrcNevra(), 0] not in build_list:
                        build_list.append([drpm.getSrcNevra(), 0])
                    if [drpm.getDstNevra(), 1] not in build_list and [drpm.getDstNevra(), 0] not in build_list:
                        build_list.append([drpm.getDstNevra(), 0])

            def sortByEVR(rpm1, rpm2):
                (n1,e1,v1,r1,a1) = rpm1[0]
                (n2,e2,v2,r2,a2) = rpm2[0]
                rc = rpmUtils.miscutils.compareEVR((e1,v1,r1),(e2,v2,r2))
                if rc == 0:
                    return 0
                if rc > 0:
                    return -1
                if rc < 0:
                    return 1
            build_list.sort(sortByEVR)
            
            if OnlyLastPackage:
                if build_list[0][1] != 1:
                    continue

                rpm = None
                for x in package["rpms"]:
                    if x.getNevra() == build_list[0][0]:
                        rpm = x
                        break
                if rpm == None:
                    continue
                    
                newpkg_list = [rpm]
            else:
                newpkg_list = []
                for build in build_list[:-1]:
                    if build[1] != 1:
                        continue
                        
                    rpm = None
                    for x in package["rpms"]:
                        if x.getNevra() == build[0]:
                            rpm = x
                            break
                    if rpm == None:
                        continue
                    
                    newpkg_list.append(rpm)

            for newpkg in newpkg_list:
                start   = False
                count   = 0
                do_last = True
                for build in build_list[:-1]:
                    if start:
                        self.__makeDeltaRpm(build[0], newpkg, package)
                        if not self.sorted:
                            self.sort()
                        count += 1
                    if DrpmsPerPackage != 0:
                        if count >= DrpmsPerPackage and not DoFirst:
                            do_last = False
                            break
                        elif count >= DrpmsPerPackage - 1 and DoFirst:
                            break
                    if build[0] == newpkg.getNevra():
                        start = True
                        
                if DoFirst == 1 and start and do_last:
                    self.__makeDeltaRpm(build_list[-1][0], newpkg, package)
                    if not self.sorted:
                        self.sort()
        return
                
    def __drpmIsWorthKeeping(self, drpmsize, newrpm):
        newsize = os.path.getsize(newrpm)
        # Delete the drpm if it's too large
        if drpmsize > self.threshold * newsize:
            return False
        return True