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/>.
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
34 Example with 'usr/local/bin/dummy':
35 The resulting list will be
46 [dir, tmp
] = dir.rsplit("/", 1)
47 if not dir + "/" in files
:
52 def _mkbackuplist(backup
):
55 return ["%s\t%s" % (getfilename(i
), mkmd5sum(i
)) for i
in backup
]
62 line
= fd
.readline().strip("\n")
68 def _mksection(title
, data
):
72 if isinstance(data
, list):
84 def __init__(self
, treename
, dbdir
):
85 self
.treename
= treename
91 return "%s" % self
.treename
93 def getpkg(self
, name
):
100 def db_read(self
, name
):
104 path
= os
.path
.join(self
.dbdir
, self
.treename
)
105 if not os
.path
.isdir(path
):
109 for roots
, dirs
, files
in os
.walk(path
):
111 [pkgname
, pkgver
, pkgrel
] = i
.rsplit("-", 2)
117 path
= os
.path
.join(path
, dbentry
)
119 [pkgname
, pkgver
, pkgrel
] = dbentry
.rsplit("-", 2)
120 pkg
= pmpkg
.pmpkg(pkgname
, pkgver
+ "-" + pkgrel
)
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
127 fd
= open(filename
, "r")
132 line
= line
.strip("\n")
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%":
161 pkg
.checksum
["desc"] = getmd5sum(filename
)
162 pkg
.mtime
["desc"] = getmtime(filename
)
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
169 fd
= open(filename
, "r")
174 line
= line
.strip("\n")
175 if line
== "%FILES%":
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
)
183 pkg
.checksum
["files"] = getmd5sum(filename
)
184 pkg
.mtime
["files"] = getmtime(filename
)
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
191 fd
= file(filename
, "r")
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%":
212 pkg
.checksum
["depends"] = getmd5sum(filename
)
213 pkg
.mtime
["depends"] = getmtime(filename
)
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
)
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())
233 path
= os
.path
.join(self
.dbdir
, "sync", self
.treename
, pkg
.fullname())
237 # for local db entries: name, version, desc, groups, url, license,
238 # arch, builddate, installdate, packager,
240 # for sync entries: name, version, desc, groups, csize, md5sum,
242 data
= [_mksection("NAME", pkg
.name
)]
243 data
.append(_mksection("VERSION", pkg
.version
))
245 data
.append(_mksection("DESC", pkg
.desc
))
247 data
.append(_mksection("GROUPS", pkg
.groups
))
249 data
.append(_mksection("LICENSE", pkg
.license
))
251 data
.append(_mksection("ARCH", pkg
.arch
))
253 data
.append(_mksection("BUILDDATE", pkg
.builddate
))
255 data
.append(_mksection("PACKAGER", pkg
.packager
))
256 if self
.treename
== "local":
258 data
.append(_mksection("URL", pkg
.url
))
260 data
.append(_mksection("INSTALLDATE", pkg
.installdate
))
262 data
.append(_mksection("SIZE", pkg
.size
))
264 data
.append(_mksection("REASON", pkg
.reason
))
266 data
.append(_mksection("FILENAME", pkg
.filename()))
268 data
.append(_mksection("REPLACES", pkg
.replaces
))
270 data
.append(_mksection("FORCE", ""))
272 data
.append(_mksection("CSIZE", pkg
.csize
))
274 data
.append(_mksection("MD5SUM", pkg
.md5sum
))
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
)
283 # for local entries, fields are: files, backup
284 # for sync ones: none
285 if self
.treename
== "local":
288 data
.append(_mksection("FILES", _mkfilelist(pkg
.files
)))
290 data
.append(_mksection("BACKUP", _mkbackuplist(pkg
.backup
)))
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
)
299 # for local db entries: depends, conflicts, provides
300 # for sync ones: depends, conflicts, provides
303 data
.append(_mksection("DEPENDS", pkg
.depends
))
305 data
.append(_mksection("OPTDEPENDS", pkg
.optdepends
))
307 data
.append(_mksection("CONFLICTS", pkg
.conflicts
))
309 data
.append(_mksection("PROVIDES", pkg
.provides
))
310 #if self.treename != "local":
312 # data.append(_mksection("REPLACES", pkg.replaces))
314 # data.append(_mksection("FORCE", ""))
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
)
323 if self
.treename
== "local":
325 for value
in pkg
.install
.values():
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
):
339 tmpdir
= tempfile
.mkdtemp()
342 for pkg
in self
.pkgs
:
343 mkdescfile(pkg
.fullname(), pkg
)
345 # Generate database archive
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('.'):
351 tar
.add(os
.path
.join(root
, d
), recursive
=False)
353 tar
.add(os
.path
.join(root
, f
))
357 shutil
.rmtree(tmpdir
)
359 def ispkgmodified(self
, pkg
):
365 oldpkg
= self
.getpkg(pkg
.name
)
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):
377 if oldpkg
.mtime
[key
][1:3] != pkg
.mtime
[key
][1:3]:
383 if __name__
== "__main__":
386 # vim: set ts=4 sw=4 et: