summaryrefslogtreecommitdiffstats
path: root/prunedrpms/PruneDrpms.py
blob: 629970a88b02d33984c250b2c4779b36c9edab25 (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
#!/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 string

DEBUG = False

def getFileList(path, ext, filelist=[]):
    """Return all files in path matching ext, store them in filelist,
    recurse dirs. Returns a list object"""

    extlen = len(ext)
    totalpath = os.path.normpath(path)
    try:
        dir_list = os.listdir(totalpath)
    except OSError, e:
        errorprint(_('Error accessing directory %s, %s') % (totalpath, e))
        sys.exit(1)

    for d in dir_list:
        if os.path.isdir(totalpath + '/' + d):
            filelist = getFileList(os.path.join(totalpath, d), ext, filelist)
        else:
            if string.lower(d[-extlen:]) == '%s' % (ext):
                filelist.append(os.path.join(totalpath, d))

    return filelist

def pruneRepo(srcdir):
    ts = rpmUtils.transaction.initReadOnlyTransaction()
    changed = False
    
    # Create list of src.rpm files.
    # We don't use "glob", so sub-directories are supported.
    print 'Expiring :', srcdir

    rpmfiles = getFileList(srcdir, ".rpm")
    foundrpms = {}
    for f in rpmfiles:
        try:
           hdr = rpmUtils.miscutils.hdrFromPackage(ts,f)
        except:
           print "Unable to open %s" % f
        else:
            n = hdr['name']
            v = hdr['version']
            r = hdr['release']
            e = hdr['epoch']
            a = hdr['arch']
            if e is None:
                e = 0
            foundrpms.setdefault((n,e,v,r,a), True)

    drpmfiles = getFileList(srcdir, ".drpm")

    rm_files = []
    for f in drpmfiles:
        try:
           hdr = rpmUtils.miscutils.hdrFromPackage(ts,f)
        except:
           print "Unable to open %s" % f
        else:
            n = hdr['name']
            v = hdr['version']
            r = hdr['release']
            e = hdr['epoch']
            a = hdr['arch']
            if e is None:
                e = 0
            if not foundrpms.has_key((n,e,v,r,a)):
                rm_files.append(f)

    for f in rm_files:
        print '  Removing', os.path.basename(f)
        if not DEBUG:
            os.remove(f)


def main(srcdir):
    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

    return pruneRepo(srcdir)


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

    main(sys.argv[1])
    sys.exit(0)