- bump version to 0.130.1
[opensuse:osc.git] / osc / checker.py
1 from tempfile import mkdtemp
2 import os
3 from shutil import rmtree
4 import rpm
5 import base64
6
7 class KeyError(Exception):
8     def __init__(self, key, *args):
9         Exception.__init__(self)
10         self.args = args
11         self.key = key
12     def __str__(self):
13         return ''+self.key+' :'+' '.join(self.args)
14
15 class Checker:
16     def __init__(self):
17         self.dbdir = mkdtemp(prefix='oscrpmdb')
18         self.imported = {}
19         rpm.addMacro('_dbpath', self.dbdir)
20         self.ts = rpm.TransactionSet()
21         self.ts.initDB()
22         self.ts.openDB()
23         self.ts.setVSFlags(0)
24         #self.ts.Debug(1)
25
26     def readkeys(self, keys=[]):
27         rpm.addMacro('_dbpath', self.dbdir)
28         for key in keys:
29             try:
30                 self.readkey(key)
31             except KeyError, e:
32                 print e
33
34         if not len(self.imported):
35             raise KeyError('', "no key imported")
36
37         rpm.delMacro("_dbpath")
38
39 # python is an idiot
40 #    def __del__(self):
41 #        self.cleanup()
42
43     def cleanup(self):
44         self.ts.closeDB()
45         rmtree(self.dbdir)
46
47     def readkey(self, file):
48         if file in self.imported:
49             return
50
51         fd = open(file, "r")
52         line = fd.readline()
53         if line and line[0:14] == "-----BEGIN PGP":
54             line = fd.readline()
55             while line and line != "\n":
56                 line = fd.readline()
57             if not line:
58                 raise KeyError(file, "not a pgp public key")
59         else:
60             raise KeyError(file, "not a pgp public key")
61
62         key = ''
63         line = fd.readline()
64         while line:
65             if line[0:12] == "-----END PGP":
66                 break
67             line = line.rstrip()
68             key += line
69             line = fd.readline()
70         fd.close()
71         if not line or line[0:12] != "-----END PGP":
72             raise KeyError(file, "not a pgp public key")
73
74         bkey = base64.b64decode(key)
75
76         r = self.ts.pgpImportPubkey(bkey)
77         if r != 0:
78             raise KeyError(file, "failed to import pubkey")
79         self.imported[file] = 1
80
81     def check(self, pkg):
82         fd = os.open(pkg, os.O_RDONLY)
83         hdr = self.ts.hdrFromFdno(fd)
84         os.close(fd)
85
86 if __name__ == "__main__":
87     import sys
88     keyfiles = []
89     pkgs = []
90     for arg in sys.argv[1:]:
91         if arg[-4:] == '.rpm':
92             pkgs.append(arg)
93         else:
94             keyfiles.append(arg)
95
96     checker = Checker()
97     try:
98         checker.readkeys(keyfiles)
99         for pkg in pkgs:
100             checker.check(pkg)
101     except Exception, e:
102         checker.cleanup()
103         raise e
104
105 # vim: sw=4 et