summaryrefslogtreecommitdiffstats
path: root/server.py
blob: c7bfe4c112bc187587e13cbf159291e7ef979255 (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
#!/usr/bin/python -t
# -*- mode: Python; indent-tabs-mode: nil; -*-
#
# 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 errno, os, sys
import fnmatch, re
import rpmUtils.transaction, rpmUtils.miscutils
import commands

#### import Utils

DEBUG = False
#### Utils.setdebug(DEBUG)

SUFFIX='drpm'

def genDeltaRPM(ts, newrpm, oldrpm):
    (f1,n1,e1,v1,r1) = newrpm
    (f2,n2,e2,v2,r2) = oldrpm
    hdr = rpmUtils.miscutils.hdrFromPackage(ts,f1)
    arch = hdr['arch']
    print 'Generating delta rpm for %s' % n1
    v12 = "_".join([v1,v2])
    r12 = "_".join([r1,r2])
    deltaRPMName= '%s.%s.%s' % ("-".join([n1,v12,r12]), arch, SUFFIX)
    deltaCommand = 'makedeltarpm %s %s %s' % (f2, f1, deltaRPMName)
    if DEBUG:
        print "DEBUG " + deltaCommand
    (code, out) = commands.getstatusoutput(deltaCommand)
    if code:
        raise Exception("genDeltaRPM: exitcode was %s - Reported Error: %s" % (code, out))

def pruneRepo(keep,whitelist,srcdir):
    ts = rpmUtils.transaction.initReadOnlyTransaction()
    changed = False
    
    # Create list of .rpm files.
    # We don't use "glob", so sub-directories are supported.
    print 'Expiring (keep=%d):' % keep, srcdir
    srcfiles = []
    for root, dirs, files in os.walk(srcdir):
            for f in fnmatch.filter(files,'*.rpm'):
                srcfiles.append(os.path.join(root,f))
    if not len(srcfiles):
        print '  Nothing found.'
        return changed
    assert srcfiles[0].startswith(srcdir)

    # Create map: rpm %name -> list of tuples (filename,name,e,v,r)
    newestsrcrpms = {}
    for f in srcfiles:
        hdr = rpmUtils.miscutils.hdrFromPackage(ts,f)
        n = hdr['name']
        v = hdr['version']
        r = hdr['release']
        e = hdr['epoch']
        if e is None:
            e = 0
        newestsrcrpms.setdefault(n,[])
        newestsrcrpms[n].append((f,n,e,v,r))

    # Now purge old src.rpm unless their %name matches a white-list pattern.
    for l in newestsrcrpms.values():
        x = len(l)

        if x > 1:
            # White-listing.
            (f,n,e,v,r) = l[0]
            keepthis = False
            for r in whitelist:
                if re.compile(r).search(n):
                    keepthis = True
                    break
            if keepthis:
                print '  Skipping',n
                continue

            def sortByEVR(fnevr1, fnevr2):
                (f1,n1,e1,v1,r1) = fnevr1
                (f2,n2,e2,v2,r2) = fnevr2
                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
            
            l.sort(sortByEVR)  # highest first in list
            # Generate delta rpm
            genDeltaRPM(ts, l[0],l[1])
        
        oldies = []
        if len(l) > abs(keep):
            oldies = l[keep:]
        for (f,n,e,v,r) in oldies:
            print '  Removing', os.path.basename(f)
            srcfiles.remove(f)
            if not DEBUG:
                os.remove(f)
                print "not removing\n"
            changed = True

    if not len(srcfiles):
        print 'WARNING: No .rpms left. Stopping here.'
        return changed

    # Examine binary repository directories and remove everything which
    # is missing its corresponding src.rpm.
    return changed


def main(bin_rpm_path):
    assert rpmUtils.miscutils.compareEVR((1,2,3),(1,2,0)) > 0
    assert rpmUtils.miscutils.compareEVR((0,1,2),(0,1,2)) == 0
    assert rpmUtils.miscutils.compareEVR((1,2,3),(4,0,99)) < 0

    #### keep = (dist == 'development') and 1 or 2
    keep = 2
    #### whitelist = cfg.repoprune_keepdict[dist]
    whitelist = ""

    return pruneRepo(keep,whitelist,bin_rpm_path)


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print 'Usage: %s <bin_rpm_dir> \n' % os.path.basename(sys.argv[0])
        sys.exit(errno.EINVAL)
    bin_rpm_path = sys.argv[1]

    #### cfg = Utils.load_config_module(sys.argv[1])

    #### Utils.signer_gid_check(cfg.signersgid)
    #### os.umask(cfg.signersumask)
    
    #### for dist in sys.argv[2:]:
    ####     if not cfg.archdict.has_key(dist):
    ####         print "No distribution release named '%s' found" % dist
    ####         sys.exit(errno.EINVAL)
    main(bin_rpm_path)
    sys.exit(0)