summaryrefslogtreecommitdiffstats
path: root/scripts/splittree.py
blob: d65c642a50c3d9ea21ab82d81a39f4063fbb1734 (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env python

# Copyright (C) 2003-2005 Red Hat, Inc.
#
# 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
#

import sys
import os
import os.path
import string
import getopt
import time
import types
import rpm

global _ts
_ts = None

# returns n-v-r.a from file filename
def nvra(pkgfile):
    global _ts
    if _ts is None:
        _ts = rpm.TransactionSet()
        _ts.setVSFlags(-1)
    fd = os.open(pkgfile, os.O_RDONLY)
    h = _ts.hdrFromFdno(fd)
    os.close(fd)
    return "%s-%s-%s.%s.rpm" %(h['name'], h['version'], h['release'],
                               h['arch'])
    

class Timber:
    """Split trees like no other"""
    def __init__(self):
        
        """self.release_str : the name and version of the product"
     
self.package_order_file : the location of the file which has
the package ordering
        
self.arch : the arch the tree is intended for
        
self.real_arch : the arch found in the unified tree's
.discinfo file
        
self.dist_dir : the loaction of the unified tree

self.src_dir : the location of the unified SRPM dir
        
self.start_time : the timestamp that's in .discinfo files
        
self.dir_info : The info other than start_time that goes
into the .discinfo files. The info should already exist
after running buildinstall in the unified tree
        
self.total_discs : total number of discs
        
self.total_bin_discs : total number of discs with RPMs
        
self.total_srpm_discs : total number of discs with SRPMs
        
self.reverse_sort_srpms : sort the srpms in reverse order to
fit. Usually only needed if we share a disc between SRPMs
and RPMs. Set to 1 to turn on.

self.reserve_size : Additional size needed to be reserved on the first disc.
"""
        
        self.target_size = 640.0 * 1024.0 * 1024
        self.fudge_factor = 1.2 * 1024.0 * 1024
        self.comps_size = 10.0 * 1024 * 1024
        self.reserve_size = 0
        self.release_str = None
        self.package_order_file = None
        self.arch = None
        self.real_arch = None
        self.dist_dir = None
        self.src_dir = None
        self.start_time = None
        self.dir_info = None
        self.total_discs = None
        self.bin_discs = None
        self.src_discs = None
        self.product_path = "anaconda"
        self.bin_list = []
        self.src_list = []
        self.shared_list = []
        self.reverse_sort_srpms=None
        self.common_files = ['beta_eula.txt', 'EULA', 'README', 'GPL', 'RPM-GPG-KEY', 'RPM-GPG-KEY-beta', 'RPM-GPG-KEY-fedora']
        self.logfile = []


    def getSize(self, path, blocksize=None):
        """Gets the size as reported by du -s"""

        if blocksize:
            p = os.popen("du -s --block-size=1 %s" % path, 'r')
            thesize = p.read()
            p.close()
            thesize = long(string.split(thesize)[0])
            return thesize
        else:
            p = os.popen("du -sh %s" % path, 'r')
            thesize = p.read()
            p.close()
            thesize = string.split(thesize)[0]
            return thesize



    def reportSizes(self, disc, firstpkg=None, lastpkg=None):
        """appends to self.logfile"""
        
        if firstpkg:
            self.logfile.append("First package on disc%d: %s" % (disc, firstpkg))
        if lastpkg:
            self.logfile.append("Last package on disc%d : %s" % (disc, lastpkg))

        discsize = self.getSize("%s-disc%d" % (self.dist_dir, disc))
        self.logfile.append("%s-disc%d size: %s" % (self.arch, disc, discsize))


        
    def createDiscInfo(self, discnumber):
        """creates the .discinfo files in the split trees"""

        if not os.path.exists("%s/.discinfo" % self.dist_dir):
            raise RuntimeError, "CRITICAL ERROR : .discinfo doesn't exist in the unified tree, not splitting"

        # if start_time isn't set then we haven't got this info yet
        if not self.start_time:
            file = open("%s/.discinfo" % (self.dist_dir), 'r')
            self.start_time = file.readline()[:-1]
            self.release_str = file.readline()[:-1]
            self.real_arch = file.readline()[:-1]

            if self.real_arch != self.arch:
                raise RuntimeError, "CRITICAL ERROR : self.real_arch is not the same as self.arch"

            # skip the disc number line from the unified tree
            file.readline()
            
            # basedir, packagedir, and pixmapdir
            self.dir_info = [file.readline()[:-1], file.readline()[:-1], file.readline()[:-1]]

            file.close()

        discinfo_file = open("%s-disc%d/.discinfo" % (self.dist_dir, discnumber), 'w')
        discinfo_file.write("%s\n" % self.start_time)
        discinfo_file.write(self.release_str + '\n')
        discinfo_file.write(self.real_arch + '\n')
        discinfo_file.write("%s\n" % discnumber)
        for i in range(0, len(self.dir_info)):
            discinfo_file.write(self.dir_info[i] + '\n')
        discinfo_file.close()

        
            
    def linkFiles(self, src_dir, dest_dir, filelist):
        """Creates hardlinks from files in the unified dir to files in the split dirs. This is not for RPMs or SRPMs"""

        for file in filelist:
            src = "%s/%s" % (src_dir, file)
            dest = "%s/%s" % (dest_dir, file)
            try:
                os.link(src, dest)
            except OSError, (errno, msg):
                pass
            


    def createSplitDirs(self):
        """Figures out which discs are for RPMs, which are for SRPMs,
        and which are shared. Also creates links for files on disc1
        and files which are common across all discs"""

        if self.bin_discs > self.total_discs or self.src_discs > self.total_discs:
            raise RuntimeError, "CRITICAL ERROR : Number of discs specified exceeds the total number of discs"

        # get a list of discs for each type of disc. shared_list will
        # be returned for sorting out which discs SRPMS can land on
        self.bin_list = range(1, self.bin_discs + 1)
        self.src_list = range(self.total_discs - self.src_discs + 1, self.total_discs + 1)
        self.shared_list = range(self.total_discs - self.src_discs + 1, self.bin_discs + 1)


        for i in range(self.bin_list[0], self.bin_list[-1] + 1):
            if i == 1:
                p = os.popen('find %s/ -type f -not -name .discinfo' % self.dist_dir, 'r')
                filelist = p.read()
                p.close()
                filelist = string.split(filelist)
                
                p = os.popen('find %s/ -type d -not -name RPMS -not -name SRPMS' % self.dist_dir, 'r')
                dirlist = p.read()
                p.close()
                dirlist = string.split(dirlist)
                
                dont_create = []
                # we need to clean up the dirlist first. We don't want everything yet
                for j in range(0, len(dirlist)):
                    dirlist[j] = string.replace(dirlist[j], self.dist_dir, '')


                # now create the dirs for disc1
                for j in range(0, len(dirlist)):
                    os.makedirs("%s-disc%d/%s" % (self.dist_dir, i, dirlist[j]))
                                
                for j in range(0, len(filelist)):
                    filelist[j] = string.replace(filelist[j], self.dist_dir, '')
                    try:
                        os.link(os.path.normpath("%s/%s" % (self.dist_dir, filelist[j])),
                                os.path.normpath("%s-disc%d/%s" % (self.dist_dir, i, filelist[j])))
                    except OSError, (errno, msg):
                        pass

                # now create the product/RPMS dir
                os.makedirs("%s-disc%d/%s/RPMS" % (self.dist_dir, i, self.product_path))
                
            else:
                os.makedirs("%s-disc%d/%s/RPMS" % (self.dist_dir, i, self.product_path))
                self.linkFiles(self.dist_dir, "%s-disc%d" %(self.dist_dir, i), self.common_files)
            self.createDiscInfo(i)
            
	if (self.src_discs != 0):
            for i in range(self.src_list[0], self.src_list[-1] + 1):
                os.makedirs("%s-disc%d/SRPMS" % (self.dist_dir, i))
                self.linkFiles(self.dist_dir,
                               "%s-disc%d" %(self.dist_dir, i),
                               self.common_files)
                self.createDiscInfo(i)

                
        
    def splitRPMS(self, reportSize = 1):
        """Creates links in the split dirs for the RPMs"""
        
        packages = {}
                
        rpmlist = os.listdir("%s/%s/RPMS" % (self.dist_dir, self.product_path))
        rpmlist.sort()

        # create the packages dictionary in this format: n-v-r.a:['n-v-r.arch.rpm']
        for filename in rpmlist:
            filesize = os.path.getsize("%s/%s/RPMS/%s" % (self.dist_dir, self.product_path, filename))
            pkg_nvr = nvra("%s/%s/RPMS/%s" %(self.dist_dir, self.product_path, filename))
            
            if packages.has_key(pkg_nvr):
                # append in case we have multiple packages with the
                # same n-v-r. Ex: the kernel has multiple n-v-r's for
                # different arches
                packages[pkg_nvr].append(filename)
            else:
                packages[pkg_nvr] = [filename]
                
        orderedlist = []
        
        # read the ordered pacakge list into orderedlist
        file = open(self.package_order_file, 'r')
        for pkg_nvr in file.readlines():
            pkg_nvr = string.rstrip(pkg_nvr)
            if pkg_nvr[0:8] != "warning:":
                orderedlist.append(pkg_nvr)
        file.close()

        # last package is the last package placed on the disc
        lastpackage = ''
        
        # packagenum resets when we change discs. It's used to
        # determine the first package in the split tree and that's
        # about it
        packagenum = 0

        disc = self.bin_list[0]

        for rpm_nvr in orderedlist:
            if not packages.has_key(rpm_nvr):
                continue
            for file_name in packages[rpm_nvr]:
                curused = self.getSize("%s-disc%s" % (self.dist_dir, disc), blocksize=1)
                filesize = self.getSize("%s/%s/RPMS/%s" % (self.dist_dir, self.product_path, file_name), blocksize=1)
                newsize = filesize + curused

                # compensate for the size of the comps package which has yet to be created
                if disc == 1:
                    maxsize = self.target_size - self.comps_size - self.reserve_size
                else:
                    maxsize = self.target_size
                    
                packagenum = packagenum + 1

                if packagenum == 1:
                    firstpackage = file_name

                # move to the next disc if true
                if newsize > maxsize:
                    self.reportSizes(disc, firstpkg=firstpackage, lastpkg=lastpackage)
                    # try it, if we are already on the last disc then complain loudly
                    try:
                        nextdisc=self.bin_list.index(disc+1)
                        disc = self.bin_list[nextdisc]
                        os.link("%s/%s/RPMS/%s" % (self.dist_dir, self.product_path, file_name),
                                "%s-disc%d/%s/RPMS/%s" % (self.dist_dir, disc, self.product_path, file_name))
                        packagenum = 1
                        firstpackage = file_name
                        
                    except:
                        # back down to the last RPM disc and complain about the overflow
                        disc = disc - 1
                        self.logfile.append("No more discs to put packages, overflowing on disc%d" % disc)
                        continue
                    
                else:
                    os.link("%s/%s/RPMS/%s" % (self.dist_dir, self.product_path, file_name),
                            "%s-disc%d/%s/RPMS/%s" % (self.dist_dir, disc, self.product_path, file_name))
                    lastpackage = file_name

        if reportSize == 1:
            self.reportSizes(disc, firstpkg=firstpackage, lastpkg=lastpackage)

        

    def getLeastUsedTree(self):
        """Returns the least full tree to use for SRPMS"""

        sizes = []
        for i in range(0, len(self.src_list)):
            sizes.append([self.getSize("%s-disc%d" % (self.dist_dir, self.src_list[i]), blocksize=1), self.src_list[i]])
        sizes.sort()
        return sizes[0]



    def splitSRPMS(self):
        """Puts the srpms onto the SRPM split discs. The packages are
        ordered by size, and placed one by one on the disc with the
        most space available"""

        srpm_list = []

        srpm_disc_list = self.src_list
        # create a list of [[size, srpm]]
        for srpm in os.listdir("%s" % self.src_dir):
            srpm_size = self.getSize("%s/%s" % (self.src_dir, srpm), blocksize=1)
            srpm_list.append([srpm_size, srpm])

        srpm_list.sort()
        srpm_list.reverse()

        for i in range(0, len(srpm_list)):
            # make sure that the src disc is within the size limits,
            # if it isn't, pull it out of the list. If there's only
            # one disk make loud noises over the overflow
            for disc in self.src_list:
                if self.getSize("%s-disc%s" % (self.dist_dir, disc), blocksize=1) > self.target_size:
                    if len(self.src_list) < 2:
                        self.logfile.append("Overflowing %s on disc%d" % (srpm_list[i][1], disc))
                        break
                    else:
                        discsize = self.getSize("%s-disc%d" % (self.dist_dir, srpm_disc_list[i]))
                        self.logfile.append("%s-disc%d size: %s" % (self.arch, disc, discsize))
                        self.src_list.pop(self.src_list.index(disc))
            os.link("%s/%s" % (self.src_dir, srpm_list[i][1]),
                    "%s-disc%d/SRPMS/%s" % (self.dist_dir, self.getLeastUsedTree()[1], srpm_list[i][1]))
        
        for i in range(0, len(srpm_disc_list)):
            self.reportSizes(srpm_disc_list[i])


    def main(self):
        """Just runs everything"""
        self.createSplitDirs()
        self.splitRPMS()
	if (self.src_discs != 0):
            self.splitSRPMS()
        return self.logfile



def usage(theerror):
    print theerror
    print """Usage: %s --arch=i386 --total-discs=8 --bin-discs=4 --src-discs=4 --release-string="distro name" --pkgorderfile=/tmp/pkgorder.12345 --distdir=/usr/src/someunifiedtree --srcdir=/usr/src/someunifiedtree/SRPMS --productpath=product""" % sys.argv[0]
    sys.exit(1)

        
if "__main__" == __name__:
    import getopt
    
    timber = Timber()

    theargs = ["arch=", "total-discs=", "bin-discs=",
               "src-discs=", "release-string=", "pkgorderfile=",
               "distdir=", "srcdir=", "productpath=", "reserve-size="]

    try:
        options, args = getopt.getopt(sys.argv[1:], '', theargs)
    except getopt.error, error:
        usage(error)

    myopts = {}
    for i in options:
        myopts[i[0]] = i[1]

    options = myopts

    if options.has_key("--arch"):
        timber.arch = options['--arch']
    else:
        usage("You forgot to specify --arch")

    if options.has_key("--total-discs"):
        timber.total_discs = int(options['--total-discs'])
    else:
        usage("You forgot to specify --total-discs")

    if options.has_key("--bin-discs"):
        timber.bin_discs = int(options['--bin-discs'])
    else:
        usage("You forgot to specify --bin-discs")

    if options.has_key("--src-discs"):
        timber.src_discs = int(options['--src-discs'])
    else:
        usage("You forgot to specify --src-discs")

    if options.has_key("--release-string"):
        timber.release_str = options["--release-string"]
    else:
        usage("You forgot to specify --release-string")

    if options.has_key("--pkgorderfile"):
        timber.package_order_file = options["--pkgorderfile"]
    else:
        usage("You forgot to specify --pkgorderfile")

    if options.has_key("--distdir"):
        timber.dist_dir = options["--distdir"]
    else:
        usage("You forgot to specify --distdir")

    if options.has_key("--srcdir"):
        timber.src_dir = options["--srcdir"]
    else:
        usage("You forgot to specify --srcdir")
    
    if options.has_key("--productpath"):
	timber.product_path = options["--productpath"]

    if options.has_key("--reserve-size"):
        timber.reserve_size = float(options["--reserve_size"])

    logfile = timber.main()
    
    for logentry in range(0, len(logfile)):
        print logfile[logentry]

    sys.exit(0)