summaryrefslogtreecommitdiff
path: root/lib/thandy/packagesys/RPMPackages.py
blob: c710b8da716dd50af7c54e7cc8fcbe7c46ecc796 (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
# Copyright 2008 The Tor Project, Inc.  See LICENSE for licensing information.

import thandy.packagesys.PackageSystem as PS

import os
try:
    import rpm
except ImportError:
    rpm = None
import md5

import thandy.formats

__all__ = [ 'RPMPackageSystem' ]

_CALLBACK_CODES = {}

for name in dir(rpm):
    if name.startswith("RPMCALLBACK_"):
        _CALLBACK_CODES[getattr(rpm, name)] = name[12:]
del name

class RPMPackageTransaction:

   def _start(self):
       PS.PackageTransaction.__init__(self)
       self._tset = rpm.TransactionSet()

   def _commit(self):
       self._tset.run(self._callback, "")
       
   def _callback(self, what, amount, total, mydata, _):
       if what == rpm.RPMCALLBACK_INST_OPEN_FILE:
           hdr, path = mydata
           logging.info("Installing RPM for %s [%s]", hdr['name'], path)

       elif what == rpm.RPMCALLBACK_INST_CLOSE_FILE:
           hdr, path = mydata
           logging.info("Done installing RPM for %s", path)
           
       elif what == rpm.RPMCALLBACK_INST_PROGRESS:
           hdr, path = mydata
           logging.info("%s: %.5s%% done", name, float(amount)/total*100)

       else:
           hdr, path = mydata
           logging.info("RPM event %s on %s [%s/%s]",
                        _CALLBACK_CODES.get(what,str(what)),
                        hdr['name'], amount, total)

def addRPMInstall(ts, path):
    fd = os.open(path, os.O_RDONLY)
    try:
        hdr = ts.hdrFromFdno(fd)
    finally:
        os.close(fd)
    ts.addInstall(hdr, (hdr, path), "u")

def addRPMErase(ts, name):
    ts.addErase(name)

def getInstalledRPMVersions(name, ts=None):
    if ts is None:
        ts = rpm.TransactionSet()
        #XXXX need to close?

    versions = set()
    for match in ts.dbMatch(rpm.RPMTAG_NAME, name):
        versions.add(match['version'])

    return versions

def fileMD5(fname):
    d = md5.new()
    try:
        f = open(fname, 'r')
        try:
            while 1:
                s = f.read(4096)
                if not s:
                    break
                d.update(s)

        finally:
            f.close()
    except OSError, e:
        logging.warn("Couldn't get digest of %s: %s", fname, e)
        return None

    return d.hexdigest()

def checkRPMInstall(name, version, ts=None):
    if ts is None:
        ts = rpm.TransactionSet()
        #XXXX need to close?

    found = False
    all_ok = True

    for h in ts.dbMatch(rpm.RPMTAG_NAME, name):
        if h['version'] != version:
            continue

        found = True

        for fname, flags, md5sum in zip(h['filenames'], h['fileflags'], h['filemd5s']):
            haveMD5 = fileMD5(fname)
            if not haveMD5:
                if flags & RPMFILE_MISSINGOK:
                    logging.info("%s is missing or unreadable from %s %s; "
                                 "that's ok.", fname, name, h['version'])
                else:
                    logging.warn("%s is missing or unreadable from %s %s.",
                                 fname, name, h['version'])
                    all_ok = False
            elif haveMD5 == md5sum:
                logging.info("%s is unchanged from %s %s",
                             fname, name, h['version'])
            else:
                # file changed.  If it's not configuration, that's a problem.
                if not flags & RPMFILE_CONFIG:
                    logging.warn("%s changed from installed version of %s %s",
                                 fname, name, h['version'])
                    all_ok = False

    return found and all_ok

class RPMChacker(PS.Checker):
    def __init__(self, rpmname, rpmversion):
        PS.Checker.__init__(self)
        self._name = rpmname
        self._version = rpmversion

    def __repr__(self):
        return "RPMChecker(%r, %r)"%(self._name, self._version)

    def getInstalledVersions(self):
        return getInstalledRPMVersions(self._name, self._transaction)

    def isInstalled(self):
        vers = getInstalledRPMVersions(self._name, self._transaction)
        return self._version in vers

#    def checkInstall(self):
#        return checkRPMInstall(self._name, self._version)

class RPMInstaller(PS.Installer):
    def __init__(self, rpmname, relPath):
        PS.Installer.__init__(self, relPath)
        self._name = rpmname

    def __repr__(self):
        return "RPMInstaller(%r, %r)"%(self._name, self._relPath)

    def install(self, transaction):
        addRPMInstall(transaction._trans, self.getFilename())

    def remove(self, transaction):
        addRPMErase(transaction._trans, self._name)