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/>.
21 from StringIO
import StringIO
30 line
= fd
.readline().strip("\n")
36 def make_section(data
, title
, values
):
39 data
.append("%%%s%%" % title
)
40 if isinstance(values
, (list, tuple)):
41 data
.extend(str(item
) for item
in values
)
44 data
.append(str(values
))
52 def __init__(self
, treename
, root
):
53 self
.treename
= treename
57 if self
.treename
== "local":
58 self
.dbdir
= os
.path
.join(root
, util
.PM_DBPATH
, treename
)
63 self
.dbfile
= os
.path
.join(root
, util
.PM_SYNCDBPATH
, treename
+ ".db")
67 return "%s" % self
.treename
70 for value
in ("Always", "Never", "Optional"):
71 if value
in self
.treename
:
75 def getpkg(self
, name
):
80 def db_read(self
, name
):
81 if not self
.dbdir
or not os
.path
.isdir(self
.dbdir
):
85 for roots
, dirs
, files
in os
.walk(self
.dbdir
):
87 [pkgname
, pkgver
, pkgrel
] = i
.rsplit("-", 2)
93 path
= os
.path
.join(self
.dbdir
, dbentry
)
95 [pkgname
, pkgver
, pkgrel
] = dbentry
.rsplit("-", 2)
96 pkg
= pmpkg
.pmpkg(pkgname
, pkgver
+ "-" + pkgrel
)
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
103 fd
= open(filename
, "r")
108 line
= line
.strip("\n")
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%":
127 pkg
.reason
= int(fd
.readline().strip("\n"))
131 elif line
== "%SIZE%" or line
== "%CSIZE%":
133 pkg
.size
= int(fd
.readline().strip("\n"))
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
)
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
158 fd
= open(filename
, "r")
163 line
= line
.strip("\n")
164 if line
== "%FILES%":
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
)
174 filename
= os
.path
.join(path
, "install")
179 # db_write is used to add both 'local' and 'sync' db entries
181 def db_write(self
, pkg
):
183 # desc/depends type entries
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
)
199 make_section(data
, "INSTALLDATE", pkg
.installdate
)
200 make_section(data
, "SIZE", pkg
.size
)
201 make_section(data
, "REASON", pkg
.reason
)
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"
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"
225 pkg_entries
= [(pkg
, self
.db_write(pkg
)) for pkg
in self
.pkgs
]
228 for pkg
, entry
in pkg_entries
:
229 path
= os
.path
.join(self
.dbdir
, pkg
.fullname())
231 for name
, data
in entry
.iteritems():
232 filename
= os
.path
.join(path
, name
)
233 util
.mkfile(filename
, data
)
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
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
))
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: