Rename _pmdbstatus_t to _alpm_dbstatus_t
[pacman-ng.git] / test / pacman / pmdb.py
blob06bc9a6f7641607b84820263fd297fd8f670475e
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 shutil
21 from StringIO import StringIO
22 import tarfile
24 import pmpkg
25 import util
27 def _getsection(fd):
28 i = []
29 while 1:
30 line = fd.readline().strip("\n")
31 if not line:
32 break
33 i.append(line)
34 return i
36 def make_section(data, title, values):
37 if not values:
38 return
39 data.append("%%%s%%" % title)
40 if isinstance(values, (list, tuple)):
41 data.extend(str(item) for item in values)
42 else:
43 # just a single value
44 data.append(str(values))
45 data.append('\n')
48 class pmdb(object):
49 """Database object
50 """
52 def __init__(self, treename, root):
53 self.treename = treename
54 self.root = root
55 self.pkgs = []
56 self.option = {}
57 if self.treename == "local":
58 self.dbdir = os.path.join(root, util.PM_DBPATH, treename)
59 self.dbfile = None
60 self.is_local = True
61 else:
62 self.dbdir = None
63 self.dbfile = os.path.join(root, util.PM_SYNCDBPATH, treename + ".db")
64 self.is_local = False
66 def __str__(self):
67 return "%s" % self.treename
69 def getverify(self):
70 for value in ("Always", "Never", "Optional"):
71 if value in self.treename:
72 return value
73 return "Never"
75 def getpkg(self, name):
76 for pkg in self.pkgs:
77 if name == pkg.name:
78 return pkg
80 def db_read(self, name):
81 if not self.dbdir or not os.path.isdir(self.dbdir):
82 return None
84 dbentry = ""
85 for roots, dirs, files in os.walk(self.dbdir):
86 for i in dirs:
87 [pkgname, pkgver, pkgrel] = i.rsplit("-", 2)
88 if pkgname == name:
89 dbentry = i
90 break
91 if not dbentry:
92 return None
93 path = os.path.join(self.dbdir, dbentry)
95 [pkgname, pkgver, pkgrel] = dbentry.rsplit("-", 2)
96 pkg = pmpkg.pmpkg(pkgname, pkgver + "-" + pkgrel)
98 # desc
99 filename = os.path.join(path, "desc")
100 if not os.path.isfile(filename):
101 print "invalid db entry found (desc missing) for pkg", pkgname
102 return None
103 fd = open(filename, "r")
104 while 1:
105 line = fd.readline()
106 if not line:
107 break
108 line = line.strip("\n")
109 if line == "%DESC%":
110 pkg.desc = fd.readline().strip("\n")
111 elif line == "%GROUPS%":
112 pkg.groups = _getsection(fd)
113 elif line == "%URL%":
114 pkg.url = fd.readline().strip("\n")
115 elif line == "%LICENSE%":
116 pkg.license = _getsection(fd)
117 elif line == "%ARCH%":
118 pkg.arch = fd.readline().strip("\n")
119 elif line == "%BUILDDATE%":
120 pkg.builddate = fd.readline().strip("\n")
121 elif line == "%INSTALLDATE%":
122 pkg.installdate = fd.readline().strip("\n")
123 elif line == "%PACKAGER%":
124 pkg.packager = fd.readline().strip("\n")
125 elif line == "%REASON%":
126 try:
127 pkg.reason = int(fd.readline().strip("\n"))
128 except ValueError:
129 pkg.reason = -1
130 raise
131 elif line == "%SIZE%" or line == "%CSIZE%":
132 try:
133 pkg.size = int(fd.readline().strip("\n"))
134 except ValueError:
135 pkg.size = -1
136 raise
137 elif line == "%MD5SUM%":
138 pkg.md5sum = fd.readline().strip("\n")
139 elif line == "%PGPSIG%":
140 pkg.pgpsig = fd.readline().strip("\n")
141 elif line == "%REPLACES%":
142 pkg.replaces = _getsection(fd)
143 elif line == "%DEPENDS%":
144 pkg.depends = _getsection(fd)
145 elif line == "%OPTDEPENDS%":
146 pkg.optdepends = _getsection(fd)
147 elif line == "%CONFLICTS%":
148 pkg.conflicts = _getsection(fd)
149 elif line == "%PROVIDES%":
150 pkg.provides = _getsection(fd)
151 fd.close()
153 # files
154 filename = os.path.join(path, "files")
155 if not os.path.isfile(filename):
156 print "invalid db entry found (files missing) for pkg", pkgname
157 return None
158 fd = open(filename, "r")
159 while 1:
160 line = fd.readline()
161 if not line:
162 break
163 line = line.strip("\n")
164 if line == "%FILES%":
165 while line:
166 line = fd.readline().strip("\n")
167 if line and line[-1] != "/":
168 pkg.files.append(line)
169 if line == "%BACKUP%":
170 pkg.backup = _getsection(fd)
171 fd.close()
173 # install
174 filename = os.path.join(path, "install")
176 return pkg
179 # db_write is used to add both 'local' and 'sync' db entries
181 def db_write(self, pkg):
182 entry = {}
183 # desc/depends type entries
184 data = []
185 make_section(data, "NAME", pkg.name)
186 make_section(data, "VERSION", pkg.version)
187 make_section(data, "DESC", pkg.desc)
188 make_section(data, "GROUPS", pkg.groups)
189 make_section(data, "LICENSE", pkg.license)
190 make_section(data, "ARCH", pkg.arch)
191 make_section(data, "BUILDDATE", pkg.builddate)
192 make_section(data, "PACKAGER", pkg.packager)
193 make_section(data, "DEPENDS", pkg.depends)
194 make_section(data, "OPTDEPENDS", pkg.optdepends)
195 make_section(data, "CONFLICTS", pkg.conflicts)
196 make_section(data, "PROVIDES", pkg.provides)
197 make_section(data, "URL", pkg.url)
198 if self.is_local:
199 make_section(data, "INSTALLDATE", pkg.installdate)
200 make_section(data, "SIZE", pkg.size)
201 make_section(data, "REASON", pkg.reason)
202 else:
203 make_section(data, "FILENAME", pkg.filename())
204 make_section(data, "REPLACES", pkg.replaces)
205 make_section(data, "CSIZE", pkg.csize)
206 make_section(data, "ISIZE", pkg.isize)
207 make_section(data, "MD5SUM", pkg.md5sum)
208 make_section(data, "PGPSIG", pkg.pgpsig)
210 entry["desc"] = "\n".join(data) + "\n"
212 # files and install
213 if self.is_local:
214 data = []
215 make_section(data, "FILES", pkg.full_filelist())
216 make_section(data, "BACKUP", pkg.local_backup_entries())
217 entry["files"] = "\n".join(data) + "\n"
219 if any(pkg.install.values()):
220 entry["install"] = pkg.installfile() + "\n"
222 return entry
224 def generate(self):
225 pkg_entries = [(pkg, self.db_write(pkg)) for pkg in self.pkgs]
227 if self.dbdir:
228 for pkg, entry in pkg_entries:
229 path = os.path.join(self.dbdir, pkg.fullname())
230 util.mkdir(path)
231 for name, data in entry.iteritems():
232 filename = os.path.join(path, name)
233 util.mkfile(filename, data)
235 if self.dbfile:
236 tar = tarfile.open(self.dbfile, "w:gz")
237 for pkg, entry in pkg_entries:
238 # TODO: the addition of the directory is currently a
239 # requirement for successful reading of a DB by libalpm
240 info = tarfile.TarInfo(pkg.fullname())
241 info.type = tarfile.DIRTYPE
242 tar.addfile(info)
243 for name, data in entry.iteritems():
244 filename = os.path.join(pkg.fullname(), name)
245 info = tarfile.TarInfo(filename)
246 info.size = len(data)
247 tar.addfile(info, StringIO(data))
248 tar.close()
249 # TODO: this is a bit unnecessary considering only one test uses it
250 serverpath = os.path.join(self.root, util.SYNCREPO, self.treename)
251 util.mkdir(serverpath)
252 shutil.copy(self.dbfile, serverpath)
254 # vim: set ts=4 sw=4 et: