autoclean.sh: abide by POSIX shebang
[pacman-ng.git] / test / pacman / pmrule.py
blob778b6aacf2961d4853f1c0d4179460ee6bbdb1b1
1 #! /usr/bin/python2
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 success = 0
91 for optdep in newpkg.optdepends:
92 if value == optdep.split(':', 1)[0]:
93 success = 1
94 break
95 elif case == "REASON":
96 if newpkg.reason != int(value):
97 success = 0
98 elif case == "FILES":
99 if not value in newpkg.files:
100 success = 0
101 elif case == "BACKUP":
102 found = 0
103 for f in newpkg.backup:
104 name, md5sum = f.split("\t")
105 if value == name:
106 found = 1
107 if not found:
108 success = 0
109 else:
110 print "PKG rule '%s' not found" % case
111 success = -1
112 elif kind == "FILE":
113 filename = os.path.join(test.root, key)
114 if case == "EXIST":
115 if not os.path.isfile(filename):
116 success = 0
117 elif case == "MODIFIED":
118 for f in test.files:
119 if f.name == key:
120 if not f.ismodified():
121 success = 0
122 break
123 elif case == "MODE":
124 if not os.path.isfile(filename):
125 success = 0
126 else:
127 mode = os.lstat(filename)[stat.ST_MODE]
128 if int(value, 8) != stat.S_IMODE(mode):
129 success = 0
130 elif case == "TYPE":
131 if value == "dir":
132 if not os.path.isdir(filename):
133 success = 0
134 elif value == "file":
135 if not os.path.isfile(filename):
136 success = 0
137 elif value == "link":
138 if not os.path.islink(filename):
139 success = 0
140 elif case == "PACNEW":
141 if not os.path.isfile("%s.pacnew" % filename):
142 success = 0
143 elif case == "PACORIG":
144 if not os.path.isfile("%s.pacorig" % filename):
145 success = 0
146 elif case == "PACSAVE":
147 if not os.path.isfile("%s.pacsave" % filename):
148 success = 0
149 else:
150 print "FILE rule '%s' not found" % case
151 success = -1
152 elif kind == "DIR":
153 filename = os.path.join(test.root, key)
154 if case == "EXIST":
155 if not os.path.isdir(filename):
156 success = 0
157 else:
158 print "DIR rule '%s' not found" % case
159 success = -1
160 elif kind == "LINK":
161 filename = os.path.join(test.root, key)
162 if case == "EXIST":
163 if not os.path.islink(filename):
164 success = 0
165 else:
166 print "LINK rule '%s' not found" % case
167 success = -1
168 elif kind == "CACHE":
169 cachedir = os.path.join(test.root, util.PM_CACHEDIR)
170 if case == "EXISTS":
171 pkg = test.findpkg(key, value, allow_local=True)
172 if not pkg or not os.path.isfile(
173 os.path.join(cachedir, pkg.filename())):
174 success = 0
175 else:
176 print "Rule kind '%s' not found" % kind
177 success = -1
179 if self.false and success != -1:
180 success = not success
181 self.result = success
182 return success
184 # vim: set ts=4 sw=4 et: