makepkg: use printf when writing pkginfo
[pacman-ng.git] / test / pacman / pmdb.py
blob8cb1b8326522ad8d32fc7a4c9d0e053f4f5eaea3
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 tempfile
21 import shutil
22 import tarfile
24 import pmpkg
25 from util import *
28 def _mkfilelist(files):
29 """Generate a list of files from the list supplied as an argument.
31 Each path is decomposed to generate the list of all directories leading
32 to the file.
34 Example with 'usr/local/bin/dummy':
35 The resulting list will be
36 usr/
37 usr/local/
38 usr/local/bin/
39 usr/local/bin/dummy
40 """
41 i = []
42 for f in files:
43 dir = getfilename(f)
44 i.append(dir)
45 while "/" in dir:
46 [dir, tmp] = dir.rsplit("/", 1)
47 if not dir + "/" in files:
48 i.append(dir + "/")
49 i.sort()
50 return i
52 def _mkbackuplist(backup):
53 """
54 """
55 return ["%s\t%s" % (getfilename(i), mkmd5sum(i)) for i in backup]
57 def _getsection(fd):
58 """
59 """
60 i = []
61 while 1:
62 line = fd.readline().strip("\n")
63 if not line:
64 break
65 i.append(line)
66 return i
68 def _mksection(title, data):
69 """
70 """
71 s = ""
72 if isinstance(data, list):
73 s = "\n".join(data)
74 else:
75 s = data
76 return "%%%s%%\n" \
77 "%s\n" % (title, s)
80 class pmdb:
81 """Database object
82 """
84 def __init__(self, treename, dbdir):
85 self.treename = treename
86 self.dbdir = dbdir
87 self.pkgs = []
88 self.option = {}
90 def __str__(self):
91 return "%s" % self.treename
93 def getpkg(self, name):
94 """
95 """
96 for pkg in self.pkgs:
97 if name == pkg.name:
98 return pkg
100 def db_read(self, name):
104 path = os.path.join(self.dbdir, self.treename)
105 if not os.path.isdir(path):
106 return None
108 dbentry = ""
109 for roots, dirs, files in os.walk(path):
110 for i in dirs:
111 [pkgname, pkgver, pkgrel] = i.rsplit("-", 2)
112 if pkgname == name:
113 dbentry = i
114 break
115 if not dbentry:
116 return None
117 path = os.path.join(path, dbentry)
119 [pkgname, pkgver, pkgrel] = dbentry.rsplit("-", 2)
120 pkg = pmpkg.pmpkg(pkgname, pkgver + "-" + pkgrel)
122 # desc
123 filename = os.path.join(path, "desc")
124 if not os.path.isfile(filename):
125 print "invalid db entry found (desc missing) for pkg", pkgname
126 return None
127 fd = open(filename, "r")
128 while 1:
129 line = fd.readline()
130 if not line:
131 break
132 line = line.strip("\n")
133 if line == "%DESC%":
134 pkg.desc = fd.readline().strip("\n")
135 elif line == "%GROUPS%":
136 pkg.groups = _getsection(fd)
137 elif line == "%URL%":
138 pkg.url = fd.readline().strip("\n")
139 elif line == "%LICENSE%":
140 pkg.license = _getsection(fd)
141 elif line == "%ARCH%":
142 pkg.arch = fd.readline().strip("\n")
143 elif line == "%BUILDDATE%":
144 pkg.builddate = fd.readline().strip("\n")
145 elif line == "%INSTALLDATE%":
146 pkg.installdate = fd.readline().strip("\n")
147 elif line == "%PACKAGER%":
148 pkg.packager = fd.readline().strip("\n")
149 elif line == "%REASON%":
150 pkg.reason = int(fd.readline().strip("\n"))
151 elif line == "%SIZE%" or line == "%CSIZE%":
152 pkg.size = int(fd.readline().strip("\n"))
153 elif line == "%MD5SUM%":
154 pkg.md5sum = fd.readline().strip("\n")
155 elif line == "%REPLACES%":
156 pkg.replaces = _getsection(fd)
157 elif line == "%FORCE%":
158 fd.readline()
159 pkg.force = 1
160 fd.close()
161 pkg.checksum["desc"] = getmd5sum(filename)
162 pkg.mtime["desc"] = getmtime(filename)
164 # files
165 filename = os.path.join(path, "files")
166 if not os.path.isfile(filename):
167 print "invalid db entry found (files missing) for pkg", pkgname
168 return None
169 fd = open(filename, "r")
170 while 1:
171 line = fd.readline()
172 if not line:
173 break
174 line = line.strip("\n")
175 if line == "%FILES%":
176 while line:
177 line = fd.readline().strip("\n")
178 if line and line[-1] != "/":
179 pkg.files.append(line)
180 if line == "%BACKUP%":
181 pkg.backup = _getsection(fd)
182 fd.close()
183 pkg.checksum["files"] = getmd5sum(filename)
184 pkg.mtime["files"] = getmtime(filename)
186 # depends
187 filename = os.path.join(path, "depends")
188 if not os.path.isfile(filename):
189 print "invalid db entry found (depends missing) for pkg", pkgname
190 return None
191 fd = file(filename, "r")
192 while 1:
193 line = fd.readline()
194 if not line:
195 break
196 line = line.strip("\n")
197 if line == "%DEPENDS%":
198 pkg.depends = _getsection(fd)
199 elif line == "%OPTDEPENDS%":
200 pkg.optdepends = _getsection(fd)
201 elif line == "%CONFLICTS%":
202 pkg.conflicts = _getsection(fd)
203 elif line == "%PROVIDES%":
204 pkg.provides = _getsection(fd)
205 # TODO this was going to be changed, but isn't anymore
206 #elif line == "%REPLACES%":
207 # pkg.replaces = _getsection(fd)
208 #elif line == "%FORCE%":
209 # fd.readline()
210 # pkg.force = 1
211 fd.close()
212 pkg.checksum["depends"] = getmd5sum(filename)
213 pkg.mtime["depends"] = getmtime(filename)
215 # install
216 filename = os.path.join(path, "install")
217 if os.path.isfile(filename):
218 pkg.checksum["install"] = getmd5sum(filename)
219 pkg.mtime["install"] = getmtime(filename)
221 return pkg
224 # db_write is used to add both 'local' and 'sync' db entries
226 def db_write(self, pkg):
230 if self.treename == "local":
231 path = os.path.join(self.dbdir, self.treename, pkg.fullname())
232 else:
233 path = os.path.join(self.dbdir, "sync", self.treename, pkg.fullname())
234 mkdir(path)
236 # desc
237 # for local db entries: name, version, desc, groups, url, license,
238 # arch, builddate, installdate, packager,
239 # size, reason
240 # for sync entries: name, version, desc, groups, csize, md5sum,
241 # replaces, force
242 data = [_mksection("NAME", pkg.name)]
243 data.append(_mksection("VERSION", pkg.version))
244 if pkg.desc:
245 data.append(_mksection("DESC", pkg.desc))
246 if pkg.groups:
247 data.append(_mksection("GROUPS", pkg.groups))
248 if pkg.license:
249 data.append(_mksection("LICENSE", pkg.license))
250 if pkg.arch:
251 data.append(_mksection("ARCH", pkg.arch))
252 if pkg.builddate:
253 data.append(_mksection("BUILDDATE", pkg.builddate))
254 if pkg.packager:
255 data.append(_mksection("PACKAGER", pkg.packager))
256 if self.treename == "local":
257 if pkg.url:
258 data.append(_mksection("URL", pkg.url))
259 if pkg.installdate:
260 data.append(_mksection("INSTALLDATE", pkg.installdate))
261 if pkg.size:
262 data.append(_mksection("SIZE", pkg.size))
263 if pkg.reason:
264 data.append(_mksection("REASON", pkg.reason))
265 else:
266 data.append(_mksection("FILENAME", pkg.filename()))
267 if pkg.replaces:
268 data.append(_mksection("REPLACES", pkg.replaces))
269 if pkg.force:
270 data.append(_mksection("FORCE", ""))
271 if pkg.csize:
272 data.append(_mksection("CSIZE", pkg.csize))
273 if pkg.md5sum:
274 data.append(_mksection("MD5SUM", pkg.md5sum))
275 if data:
276 data.append("")
277 filename = os.path.join(path, "desc")
278 mkfile(filename, "\n".join(data))
279 pkg.checksum["desc"] = getmd5sum(filename)
280 pkg.mtime["desc"] = getmtime(filename)
282 # files
283 # for local entries, fields are: files, backup
284 # for sync ones: none
285 if self.treename == "local":
286 data = []
287 if pkg.files:
288 data.append(_mksection("FILES", _mkfilelist(pkg.files)))
289 if pkg.backup:
290 data.append(_mksection("BACKUP", _mkbackuplist(pkg.backup)))
291 if data:
292 data.append("")
293 filename = os.path.join(path, "files")
294 mkfile(filename, "\n".join(data))
295 pkg.checksum["files"] = getmd5sum(filename)
296 pkg.mtime["files"] = getmtime(filename)
298 # depends
299 # for local db entries: depends, conflicts, provides
300 # for sync ones: depends, conflicts, provides
301 data = []
302 if pkg.depends:
303 data.append(_mksection("DEPENDS", pkg.depends))
304 if pkg.optdepends:
305 data.append(_mksection("OPTDEPENDS", pkg.optdepends))
306 if pkg.conflicts:
307 data.append(_mksection("CONFLICTS", pkg.conflicts))
308 if pkg.provides:
309 data.append(_mksection("PROVIDES", pkg.provides))
310 #if self.treename != "local":
311 # if pkg.replaces:
312 # data.append(_mksection("REPLACES", pkg.replaces))
313 # if pkg.force:
314 # data.append(_mksection("FORCE", ""))
315 if data:
316 data.append("")
317 filename = os.path.join(path, "depends")
318 mkfile(filename, "\n".join(data))
319 pkg.checksum["depends"] = getmd5sum(filename)
320 pkg.mtime["depends"] = getmtime(filename)
322 # install
323 if self.treename == "local":
324 empty = 1
325 for value in pkg.install.values():
326 if value:
327 empty = 0
328 if not empty:
329 filename = os.path.join(path, "install")
330 mkinstallfile(filename, pkg.install)
331 pkg.checksum["install"] = getmd5sum(filename)
332 pkg.mtime["install"] = getmtime(filename)
334 def gensync(self, path):
338 curdir = os.getcwd()
339 tmpdir = tempfile.mkdtemp()
340 os.chdir(tmpdir)
342 for pkg in self.pkgs:
343 mkdescfile(pkg.fullname(), pkg)
345 # Generate database archive
346 mkdir(path)
347 archive = os.path.join(path, "%s%s" % (self.treename, PM_EXT_DB))
348 tar = tarfile.open(archive, "w:gz")
349 for root, dirs, files in os.walk('.'):
350 for d in dirs:
351 tar.add(os.path.join(root, d), recursive=False)
352 for f in files:
353 tar.add(os.path.join(root, f))
354 tar.close()
356 os.chdir(curdir)
357 shutil.rmtree(tmpdir)
359 def ispkgmodified(self, pkg):
363 modified = 0
365 oldpkg = self.getpkg(pkg.name)
366 if not oldpkg:
367 return 0
369 vprint("\toldpkg.checksum : %s" % oldpkg.checksum)
370 vprint("\toldpkg.mtime : %s" % oldpkg.mtime)
372 for key in pkg.mtime.keys():
373 if key == "install" \
374 and oldpkg.mtime[key] == (0, 0, 0) \
375 and pkg.mtime[key] == (0, 0, 0):
376 continue
377 if oldpkg.mtime[key][1:3] != pkg.mtime[key][1:3]:
378 modified += 1
380 return modified
383 if __name__ == "__main__":
384 db = pmdb("local")
385 print db
386 # vim: set ts=4 sw=4 et: