dload: rename cd_filename to content_disp_name
[pacman-ng.git] / test / pacman / util.py
blobcde44c6e566e05763877ac30e5d087ff53d06e7f
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/>.
19 import os
20 import re
21 import hashlib
24 # ALPM
25 PM_ROOT = "/"
26 PM_DBPATH = "var/lib/pacman"
27 PM_SYNCDBPATH = "var/lib/pacman/sync"
28 PM_LOCK = "var/lib/pacman/db.lck"
29 PM_CACHEDIR = "var/cache/pacman/pkg"
30 PM_EXT_PKG = ".pkg.tar.gz"
32 # Pacman
33 PACCONF = "etc/pacman.conf"
35 # Pactest
36 TMPDIR = "tmp"
37 SYNCREPO = "var/pub"
38 LOGFILE = "var/log/pactest.log"
40 verbose = 0
42 def vprint(msg):
43 if verbose:
44 print msg
47 # Methods to generate files
50 def getfileinfo(filename):
51 data = {
52 'changed': False,
53 'isdir': False,
54 'islink': False,
55 'link': None,
56 'hasperms': False,
57 'perms': None,
59 if filename[-1] == "*":
60 data["changed"] = True
61 filename = filename.rstrip("*")
62 if filename.find(" -> ") != -1:
63 filename, link = filename.split(" -> ")
64 data["islink"] = True
65 data["link"] = link
66 elif filename.find("|") != -1:
67 filename, perms = filename.split("|")
68 data["hasperms"] = True
69 data["perms"] = int(perms, 8)
70 if filename[-1] == "/":
71 data["isdir"] = True
73 data["filename"] = filename
74 return data
76 def mkfile(base, name, data=""):
77 info = getfileinfo(name)
78 filename = info["filename"]
80 path = os.path.join(base, filename)
81 if info["isdir"]:
82 if not os.path.isdir(path):
83 os.makedirs(path, 0755)
84 return
86 dir_path = os.path.dirname(path)
87 if dir_path and not os.path.isdir(dir_path):
88 os.makedirs(dir_path, 0755)
90 if info["islink"]:
91 os.symlink(info["link"], path)
92 else:
93 writedata(path, data)
95 if info["perms"]:
96 os.chmod(path, info["perms"])
98 def writedata(filename, data):
99 if isinstance(data, list):
100 data = "\n".join(data)
101 fd = file(filename, "w")
102 if data:
103 fd.write(data)
104 if data[-1] != "\n":
105 fd.write("\n")
106 fd.close()
108 def mkcfgfile(filename, root, option, db):
109 # Options
110 data = ["[options]"]
111 for key, value in option.iteritems():
112 data.extend(["%s = %s" % (key, j) for j in value])
114 # Repositories
115 # sort by repo name so tests can predict repo order, rather than be
116 # subjects to the whims of python dict() ordering
117 for key in sorted(db.iterkeys()):
118 if key != "local":
119 value = db[key]
120 data.append("[%s]\n" \
121 "SigLevel = %s\n" \
122 "Server = file://%s" \
123 % (value.treename, value.getverify(), \
124 os.path.join(root, SYNCREPO, value.treename)))
125 for optkey, optval in value.option.iteritems():
126 data.extend(["%s = %s" % (optkey, j) for j in optval])
128 mkfile(root, filename, "\n".join(data))
132 # MD5 helpers
135 def getmd5sum(filename):
136 if not os.path.isfile(filename):
137 return ""
138 fd = open(filename, "rb")
139 checksum = hashlib.md5()
140 while 1:
141 block = fd.read(32 * 1024)
142 if not block:
143 break
144 checksum.update(block)
145 fd.close()
146 return checksum.hexdigest()
148 def mkmd5sum(data):
149 checksum = hashlib.md5()
150 checksum.update("%s\n" % data)
151 return checksum.hexdigest()
155 # Miscellaneous
158 def which(filename):
159 path = os.environ["PATH"].split(':')
160 for p in path:
161 f = os.path.join(p, filename)
162 if os.access(f, os.F_OK):
163 return f
164 return None
166 def grep(filename, pattern):
167 pat = re.compile(pattern)
168 myfile = open(filename, 'r')
169 for line in myfile:
170 if pat.search(line):
171 myfile.close()
172 return True
173 myfile.close()
174 return False
176 def mkdir(path):
177 if os.path.isdir(path):
178 return
179 elif os.path.isfile(path):
180 raise OSError("'%s' already exists and is not a directory" % path)
181 os.makedirs(path, 0755)
183 # vim: set ts=4 sw=4 et: