Added gpg verification options per repo to the config file.
[pacman-ng.git] / test / pacman / util.py
blob472559230111f007a948a1deda8ca8e97fdb33a0
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/>.
19 import os
20 import re
21 import hashlib
22 import stat
25 # ALPM
26 PM_ROOT = "/"
27 PM_DBPATH = "var/lib/pacman"
28 PM_SYNCDBPATH = "var/lib/pacman/sync"
29 PM_LOCK = "var/lib/pacman/db.lck"
30 PM_CACHEDIR = "var/cache/pacman/pkg"
31 PM_EXT_PKG = ".pkg.tar.gz"
33 # Pacman
34 PACCONF = "etc/pacman.conf"
36 # Pactest
37 TMPDIR = "tmp"
38 SYNCREPO = "var/pub"
39 LOGFILE = "var/log/pactest.log"
41 verbose = 0
43 def vprint(msg):
44 if verbose:
45 print msg
48 # Methods to generate files
51 def getfilename(name):
52 """
53 """
54 filename = name
55 extra = ""
56 if filename[-1] == "*":
57 filename = filename.rstrip("*")
58 if filename.find(" -> ") != -1:
59 filename, extra = filename.split(" -> ")
60 elif filename.find("|") != -1:
61 filename, extra = filename.split("|")
62 return filename
64 def mkfile(name, data = ""):
65 """
66 """
67 isdir = 0
68 islink = 0
69 setperms = 0
70 filename = name
71 link = ""
72 perms = ""
74 if filename[-1] == "*":
75 filename = filename.rstrip("*")
76 if filename.find(" -> ") != -1:
77 islink = 1
78 filename, link = filename.split(" -> ")
79 elif filename.find("|") != -1:
80 setperms = 1
81 filename, perms = filename.split("|")
82 if filename[-1] == "/":
83 isdir = 1
85 if isdir:
86 path = filename
87 else:
88 path = os.path.dirname(filename)
89 if path and not os.path.isdir(path):
90 os.makedirs(path, 0755)
92 if isdir:
93 return
94 if islink:
95 curdir = os.getcwd()
96 if path:
97 os.chdir(path)
98 os.symlink(link, os.path.basename(filename))
99 os.chdir(curdir)
100 else:
101 fd = file(filename, "w")
102 if data:
103 fd.write(data)
104 if data[-1] != "\n":
105 fd.write("\n")
106 fd.close()
107 if setperms:
108 os.chmod(filename, int(perms, 8))
110 def mkinstallfile(filename, install):
113 data = []
114 for key, value in install.iteritems():
115 if value:
116 data.append("%s() {\n%s\n}" % (key, value))
118 mkfile(filename, "\n".join(data))
120 def mkcfgfile(filename, root, option, db):
123 # Options
124 data = ["[options]"]
125 for key, value in option.iteritems():
126 data.extend(["%s = %s" % (key, j) for j in value])
128 # Repositories
129 # sort by repo name so tests can predict repo order, rather than be
130 # subjects to the whims of python dict() ordering
131 for key in sorted(db.iterkeys()):
132 if key != "local":
133 value = db[key]
134 data.append("[%s]\n" \
135 "VerifySig = %s\n" \
136 "Server = file://%s" \
137 % (value.treename, value.getverify(), \
138 os.path.join(root, SYNCREPO, value.treename)))
139 for optkey, optval in value.option.iteritems():
140 data.extend(["%s = %s" % (optkey, j) for j in optval])
142 mkfile(os.path.join(root, filename), "\n".join(data))
146 # MD5 helpers
149 def getmd5sum(filename):
152 if not os.path.isfile(filename):
153 print "file %s does not exist!" % filename
154 return ""
155 fd = open(filename, "rb")
156 checksum = hashlib.md5()
157 while 1:
158 block = fd.read(32 * 1024)
159 if not block:
160 break
161 checksum.update(block)
162 fd.close()
163 return checksum.hexdigest()
165 def mkmd5sum(data):
168 checksum = hashlib.md5()
169 checksum.update("%s\n" % data)
170 return checksum.hexdigest()
174 # Mtime helpers
177 def getmtime(filename):
180 if not os.path.exists(filename):
181 print "path %s does not exist!" % filename
182 return 0, 0, 0
183 st = os.stat(filename)
184 return st[stat.ST_ATIME], st[stat.ST_MTIME], st[stat.ST_CTIME]
187 # Miscellaneous
190 def which(filename):
191 path = os.environ["PATH"].split(':')
192 for p in path:
193 f = os.path.join(p, filename)
194 if os.access(f, os.F_OK):
195 return f
196 return None
198 def grep(filename, pattern):
199 pat = re.compile(pattern)
200 myfile = open(filename, 'r')
201 for line in myfile:
202 if pat.search(line):
203 myfile.close()
204 return True
205 myfile.close()
206 return False
208 def mkdir(path):
209 if os.path.isdir(path):
210 return
211 elif os.path.isfile(path):
212 raise OSError("'%s' already exists and is not a directory" % path)
213 os.makedirs(path, 0755)
215 # vim: set ts=4 sw=4 et: