makepkg: use printf when writing pkginfo
[pacman-ng.git] / test / pacman / pmrule.py
blobe7c9c44ffa774236c002b2f2acedbf62eaeeb190
1 #! /usr/bin/python
3 # Copyright (c) 2006 by Aurelien Foret <orelien@chez.com>
4 #
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 from util import *
20 from stat import *
22 class pmrule:
23 """Rule object
24 """
26 def __init__(self, rule):
27 self.rule = rule
28 self.false = 0
29 self.result = 0
31 def __str__(self):
32 return "rule = %s" % self.rule
34 def check(self, root, retcode, localdb, files):
35 """
36 """
38 success = 1
40 [test, args] = self.rule.split("=")
41 if test[0] == "!":
42 self.false = 1
43 test = test.lstrip("!")
44 [kind, case] = test.split("_")
45 if "|" in args:
46 [key, value] = args.split("|", 1)
47 else:
48 [key, value] = [args, None]
50 if kind == "PACMAN":
51 if case == "RETCODE":
52 if retcode != int(key):
53 success = 0
54 elif case == "OUTPUT":
55 logfile = os.path.join(root, LOGFILE)
56 if not os.access(logfile, os.F_OK):
57 print "LOGFILE not found, cannot validate 'OUTPUT' rule"
58 success = 0
59 elif not grep(os.path.join(root, LOGFILE), key):
60 success = 0
61 else:
62 print "PACMAN rule '%s' not found" % case
63 success = -1
64 elif kind == "PKG":
65 newpkg = localdb.db_read(key)
66 if not newpkg:
67 success = 0
68 else:
69 vprint("\tnewpkg.checksum : %s" % newpkg.checksum)
70 vprint("\tnewpkg.mtime : %s" % newpkg.mtime)
71 if case == "EXIST":
72 success = 1
73 elif case == "MODIFIED":
74 if not localdb.ispkgmodified(newpkg):
75 success = 0
76 elif case == "VERSION":
77 if value != newpkg.version:
78 success = 0
79 elif case == "GROUPS":
80 if not value in newpkg.groups:
81 success = 0
82 elif case == "PROVIDES":
83 if not value in newpkg.provides:
84 success = 0
85 elif case == "DEPENDS":
86 if not value in newpkg.depends:
87 success = 0
88 elif case == "OPTDEPENDS":
89 if not value in newpkg.optdepends:
90 success = 0
91 elif case == "REASON":
92 if newpkg.reason != int(value):
93 success = 0
94 elif case == "FILES":
95 if not value in newpkg.files:
96 success = 0
97 elif case == "BACKUP":
98 found = 0
99 for f in newpkg.backup:
100 name, md5sum = f.split("\t")
101 if value == name:
102 found = 1
103 if not found:
104 success = 0
105 else:
106 print "PKG rule '%s' not found" % case
107 success = -1
108 elif kind == "FILE":
109 filename = os.path.join(root, key)
110 if case == "EXIST":
111 if not os.path.isfile(filename):
112 success = 0
113 elif case == "MODIFIED":
114 for f in files:
115 if f.name == key:
116 if not f.ismodified():
117 success = 0
118 elif case == "MODE":
119 if not os.path.isfile(filename):
120 success = 0
121 else:
122 mode = os.lstat(filename)[ST_MODE]
123 if int(value,8) != S_IMODE(mode):
124 success = 0
125 elif case == "TYPE":
126 if value == "dir":
127 if not os.path.isdir(filename):
128 success = 0
129 elif value == "file":
130 if not os.path.isfile(filename):
131 success = 0
132 elif value == "link":
133 if not os.path.islink(filename):
134 success = 0
135 elif case == "PACNEW":
136 if not os.path.isfile("%s%s" % (filename, PM_PACNEW)):
137 success = 0
138 elif case == "PACORIG":
139 if not os.path.isfile("%s%s" % (filename, PM_PACORIG)):
140 success = 0
141 elif case == "PACSAVE":
142 if not os.path.isfile("%s%s" % (filename, PM_PACSAVE)):
143 success = 0
144 else:
145 print "FILE rule '%s' not found" % case
146 success = -1
147 else:
148 print "Rule kind '%s' not found" % kind
149 success = -1
151 if self.false and success != -1:
152 success = not success
153 self.result = success
154 return success
157 if __name__ != "__main__":
158 rule = pmrule("PKG_EXIST=dummy")
159 # vim: set ts=4 sw=4 et: