Rename pmdelta_t to alpm_delta_t
[pacman-ng.git] / test / pacman / util.py
blob0cf0eabeb2e14be85e10a836d9888f202b5b3cb2
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
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 mkfile(name, data = ""):
51 isdir = 0
52 islink = 0
53 setperms = 0
54 filename = name
55 link = ""
56 perms = ""
58 if filename[-1] == "*":
59 filename = filename.rstrip("*")
60 if filename.find(" -> ") != -1:
61 islink = 1
62 filename, link = filename.split(" -> ")
63 elif filename.find("|") != -1:
64 setperms = 1
65 filename, perms = filename.split("|")
66 if filename[-1] == "/":
67 isdir = 1
69 if isdir:
70 path = filename
71 else:
72 path = os.path.dirname(filename)
73 if path and not os.path.isdir(path):
74 os.makedirs(path, 0755)
76 if isdir:
77 return
78 if islink:
79 curdir = os.getcwd()
80 if path:
81 os.chdir(path)
82 os.symlink(link, os.path.basename(filename))
83 os.chdir(curdir)
84 else:
85 fd = file(filename, "w")
86 if data:
87 fd.write(data)
88 if data[-1] != "\n":
89 fd.write("\n")
90 fd.close()
91 if setperms:
92 os.chmod(filename, int(perms, 8))
94 def mkcfgfile(filename, root, option, db):
95 # Options
96 data = ["[options]"]
97 for key, value in option.iteritems():
98 data.extend(["%s = %s" % (key, j) for j in value])
100 # Repositories
101 # sort by repo name so tests can predict repo order, rather than be
102 # subjects to the whims of python dict() ordering
103 for key in sorted(db.iterkeys()):
104 if key != "local":
105 value = db[key]
106 data.append("[%s]\n" \
107 "VerifySig = %s\n" \
108 "Server = file://%s" \
109 % (value.treename, value.getverify(), \
110 os.path.join(root, SYNCREPO, value.treename)))
111 for optkey, optval in value.option.iteritems():
112 data.extend(["%s = %s" % (optkey, j) for j in optval])
114 mkfile(os.path.join(root, filename), "\n".join(data))
118 # MD5 helpers
121 def getmd5sum(filename):
122 if not os.path.isfile(filename):
123 return ""
124 fd = open(filename, "rb")
125 checksum = hashlib.md5()
126 while 1:
127 block = fd.read(32 * 1024)
128 if not block:
129 break
130 checksum.update(block)
131 fd.close()
132 return checksum.hexdigest()
134 def mkmd5sum(data):
135 checksum = hashlib.md5()
136 checksum.update("%s\n" % data)
137 return checksum.hexdigest()
141 # Miscellaneous
144 def which(filename):
145 path = os.environ["PATH"].split(':')
146 for p in path:
147 f = os.path.join(p, filename)
148 if os.access(f, os.F_OK):
149 return f
150 return None
152 def grep(filename, pattern):
153 pat = re.compile(pattern)
154 myfile = open(filename, 'r')
155 for line in myfile:
156 if pat.search(line):
157 myfile.close()
158 return True
159 myfile.close()
160 return False
162 def mkdir(path):
163 if os.path.isdir(path):
164 return
165 elif os.path.isfile(path):
166 raise OSError("'%s' already exists and is not a directory" % path)
167 os.makedirs(path, 0755)
169 # vim: set ts=4 sw=4 et: