pacman-key: rework importing distro/repo provided keyrings
[pacman-ng.git] / test / pacman / pmrule.py
blob70c8838e2c4d529c8e2b1a9ef3f6fea529dc8428
1 #! /usr/bin/python
3 # Copyright (c) 2006 by Aurelien Foret <orelien@chez.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import os
19 import stat
21 import util
23 class pmrule(object):
24 """Rule object
25 """
27 def __init__(self, rule):
28 self.rule = rule
29 self.false = 0
30 self.result = 0
32 def __str__(self):
33 if len(self.rule) <= 40:
34 return self.rule
35 return self.rule[:37] + '...'
37 def check(self, test):
38 """
39 """
40 success = 1
42 [testname, args] = self.rule.split("=")
43 if testname[0] == "!":
44 self.false = 1
45 testname = testname[1:]
46 [kind, case] = testname.split("_")
47 if "|" in args:
48 [key, value] = args.split("|", 1)
49 else:
50 [key, value] = [args, None]
52 if kind == "PACMAN":
53 if case == "RETCODE":
54 if test.retcode != int(key):
55 success = 0
56 elif case == "OUTPUT":
57 logfile = os.path.join(test.root, util.LOGFILE)
58 if not os.access(logfile, os.F_OK):
59 print "LOGFILE not found, cannot validate 'OUTPUT' rule"
60 success = 0
61 elif not util.grep(logfile, key):
62 success = 0
63 else:
64 print "PACMAN rule '%s' not found" % case
65 success = -1
66 elif kind == "PKG":
67 localdb = test.db["local"]
68 newpkg = localdb.db_read(key)
69 if not newpkg:
70 success = 0
71 else:
72 if case == "EXIST":
73 success = 1
74 elif case == "VERSION":
75 if value != newpkg.version:
76 success = 0
77 elif case == "DESC":
78 if value != newpkg.desc:
79 success = 0
80 elif case == "GROUPS":
81 if not value in newpkg.groups:
82 success = 0
83 elif case == "PROVIDES":
84 if not value in newpkg.provides:
85 success = 0
86 elif case == "DEPENDS":
87 if not value in newpkg.depends:
88 success = 0
89 elif case == "OPTDEPENDS":
90 if not value in newpkg.optdepends:
91 success = 0
92 elif case == "REASON":
93 if newpkg.reason != int(value):
94 success = 0
95 elif case == "FILES":
96 if not value in newpkg.files:
97 success = 0
98 elif case == "BACKUP":
99 found = 0
100 for f in newpkg.backup:
101 name, md5sum = f.split("\t")
102 if value == name:
103 found = 1
104 if not found:
105 success = 0
106 else:
107 print "PKG rule '%s' not found" % case
108 success = -1
109 elif kind == "FILE":
110 filename = os.path.join(test.root, key)
111 if case == "EXIST":
112 if not os.path.isfile(filename):
113 success = 0
114 elif case == "MODIFIED":
115 for f in test.files:
116 if f.name == key:
117 if not f.ismodified():
118 success = 0
119 break
120 elif case == "MODE":
121 if not os.path.isfile(filename):
122 success = 0
123 else:
124 mode = os.lstat(filename)[stat.ST_MODE]
125 if int(value, 8) != stat.S_IMODE(mode):
126 success = 0
127 elif case == "TYPE":
128 if value == "dir":
129 if not os.path.isdir(filename):
130 success = 0
131 elif value == "file":
132 if not os.path.isfile(filename):
133 success = 0
134 elif value == "link":
135 if not os.path.islink(filename):
136 success = 0
137 elif case == "PACNEW":
138 if not os.path.isfile("%s.pacnew" % filename):
139 success = 0
140 elif case == "PACORIG":
141 if not os.path.isfile("%s.pacorig" % filename):
142 success = 0
143 elif case == "PACSAVE":
144 if not os.path.isfile("%s.pacsave" % filename):
145 success = 0
146 else:
147 print "FILE rule '%s' not found" % case
148 success = -1
149 elif kind == "DIR":
150 filename = os.path.join(test.root, key)
151 if case == "EXIST":
152 if not os.path.isdir(filename):
153 success = 0
154 else:
155 print "DIR rule '%s' not found" % case
156 success = -1
157 elif kind == "LINK":
158 filename = os.path.join(test.root, key)
159 if case == "EXIST":
160 if not os.path.islink(filename):
161 success = 0
162 else:
163 print "LINK rule '%s' not found" % case
164 success = -1
165 elif kind == "CACHE":
166 cachedir = os.path.join(test.root, util.PM_CACHEDIR)
167 if case == "EXISTS":
168 pkg = test.findpkg(key, value, allow_local=True)
169 if not pkg or not os.path.isfile(
170 os.path.join(cachedir, pkg.filename())):
171 success = 0
172 else:
173 print "Rule kind '%s' not found" % kind
174 success = -1
176 if self.false and success != -1:
177 success = not success
178 self.result = success
179 return success
181 # vim: set ts=4 sw=4 et: