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
90 return "%s" % self
.treename
92 def getpkg(self
, name
):
99 def db_read(self
, name
):
103 path
= os
.path
.join(self
.dbdir
, self
.treename
)
104 if not os
.path
.isdir(path
):
108 for roots
, dirs
, files
in os
.walk(path
):
110 [pkgname
, pkgver
, pkgrel
] = i
.rsplit("-", 2)
116 path
= os
.path
.join(path
, dbentry
)
118 [pkgname
, pkgver
, pkgrel
] = dbentry
.rsplit("-", 2)
119 pkg
= pmpkg
.pmpkg(pkgname
, pkgver
+ "-" + pkgrel
)
122 filename
= os
.path
.join(path
, "desc")
123 if not os
.path
.isfile(filename
):
124 print "invalid db entry found (desc missing) for pkg", pkgname
126 fd
= open(filename
, "r")
131 line
= line
.strip("\n")
133 pkg
.desc
= fd
.readline().strip("\n")
134 elif line
== "%GROUPS%":
135 pkg
.groups
= _getsection(fd
)
136 elif line
== "%URL%":
137 pkg
.url
= fd
.readline().strip("\n")
138 elif line
== "%LICENSE%":
139 pkg
.license
= _getsection(fd
)
140 elif line
== "%ARCH%":
141 pkg
.arch
= fd
.readline().strip("\n")
142 elif line
== "%BUILDDATE%":
143 pkg
.builddate
= fd
.readline().strip("\n")
144 elif line
== "%INSTALLDATE%":
145 pkg
.installdate
= fd
.readline().strip("\n")
146 elif line
== "%PACKAGER%":
147 pkg
.packager
= fd
.readline().strip("\n")
148 elif line
== "%REASON%":
149 pkg
.reason
= int(fd
.readline().strip("\n"))
150 elif line
== "%SIZE%" or line
== "%CSIZE%":
151 pkg
.size
= int(fd
.readline().strip("\n"))
152 elif line
== "%MD5SUM%":
153 pkg
.md5sum
= fd
.readline().strip("\n")
154 elif line
== "%REPLACES%":
155 pkg
.replaces
= _getsection(fd
)
156 elif line
== "%FORCE%":
160 pkg
.checksum
["desc"] = getmd5sum(filename
)
161 pkg
.mtime
["desc"] = getmtime(filename
)
164 filename
= os
.path
.join(path
, "files")
165 if not os
.path
.isfile(filename
):
166 print "invalid db entry found (files missing) for pkg", pkgname
168 fd
= open(filename
, "r")
173 line
= line
.strip("\n")
174 if line
== "%FILES%":
176 line
= fd
.readline().strip("\n")
177 if line
and line
[-1] != "/":
178 pkg
.files
.append(line
)
179 if line
== "%BACKUP%":
180 pkg
.backup
= _getsection(fd
)
182 pkg
.checksum
["files"] = getmd5sum(filename
)
183 pkg
.mtime
["files"] = getmtime(filename
)
186 filename
= os
.path
.join(path
, "depends")
187 if not os
.path
.isfile(filename
):
188 print "invalid db entry found (depends missing) for pkg", pkgname
190 fd
= file(filename
, "r")
195 line
= line
.strip("\n")
196 if line
== "%DEPENDS%":
197 pkg
.depends
= _getsection(fd
)
198 elif line
== "%OPTDEPENDS%":
199 pkg
.optdepends
= _getsection(fd
)
200 elif line
== "%CONFLICTS%":
201 pkg
.conflicts
= _getsection(fd
)
202 elif line
== "%PROVIDES%":
203 pkg
.provides
= _getsection(fd
)
204 # TODO this was going to be changed, but isn't anymore
205 #elif line == "%REPLACES%":
206 # pkg.replaces = _getsection(fd)
207 #elif line == "%FORCE%":
211 pkg
.checksum
["depends"] = getmd5sum(filename
)
212 pkg
.mtime
["depends"] = getmtime(filename
)
215 filename
= os
.path
.join(path
, "install")
216 if os
.path
.isfile(filename
):
217 pkg
.checksum
["install"] = getmd5sum(filename
)
218 pkg
.mtime
["install"] = getmtime(filename
)
223 # db_write is used to add both 'local' and 'sync' db entries
225 def db_write(self
, pkg
):
229 if self
.treename
== "local":
230 path
= os
.path
.join(self
.dbdir
, self
.treename
, pkg
.fullname())
232 path
= os
.path
.join(self
.dbdir
, "sync", self
.treename
, pkg
.fullname())
236 # for local db entries: name, version, desc, groups, url, license,
237 # arch, builddate, installdate, packager,
239 # for sync entries: name, version, desc, groups, csize, md5sum,
241 data
= [_mksection("NAME", pkg
.name
)]
242 data
.append(_mksection("VERSION", pkg
.version
))
244 data
.append(_mksection("DESC", pkg
.desc
))
246 data
.append(_mksection("GROUPS", pkg
.groups
))
248 data
.append(_mksection("LICENSE", pkg
.license
))
250 data
.append(_mksection("ARCH", pkg
.arch
))
252 data
.append(_mksection("BUILDDATE", pkg
.builddate
))
254 data
.append(_mksection("PACKAGER", pkg
.packager
))
255 if self
.treename
== "local":
257 data
.append(_mksection("URL", pkg
.url
))
259 data
.append(_mksection("INSTALLDATE", pkg
.installdate
))
261 data
.append(_mksection("SIZE", pkg
.size
))
263 data
.append(_mksection("REASON", pkg
.reason
))
265 data
.append(_mksection("FILENAME", pkg
.filename()))
267 data
.append(_mksection("REPLACES", pkg
.replaces
))
269 data
.append(_mksection("FORCE", ""))
271 data
.append(_mksection("CSIZE", pkg
.csize
))
273 data
.append(_mksection("MD5SUM", pkg
.md5sum
))
276 filename
= os
.path
.join(path
, "desc")
277 mkfile(filename
, "\n".join(data
))
278 pkg
.checksum
["desc"] = getmd5sum(filename
)
279 pkg
.mtime
["desc"] = getmtime(filename
)
282 # for local entries, fields are: files, backup
283 # for sync ones: none
284 if self
.treename
== "local":
287 data
.append(_mksection("FILES", _mkfilelist(pkg
.files
)))
289 data
.append(_mksection("BACKUP", _mkbackuplist(pkg
.backup
)))
292 filename
= os
.path
.join(path
, "files")
293 mkfile(filename
, "\n".join(data
))
294 pkg
.checksum
["files"] = getmd5sum(filename
)
295 pkg
.mtime
["files"] = getmtime(filename
)
298 # for local db entries: depends, conflicts, provides
299 # for sync ones: depends, conflicts, provides
302 data
.append(_mksection("DEPENDS", pkg
.depends
))
304 data
.append(_mksection("OPTDEPENDS", pkg
.optdepends
))
306 data
.append(_mksection("CONFLICTS", pkg
.conflicts
))
308 data
.append(_mksection("PROVIDES", pkg
.provides
))
309 #if self.treename != "local":
311 # data.append(_mksection("REPLACES", pkg.replaces))
313 # data.append(_mksection("FORCE", ""))
316 filename
= os
.path
.join(path
, "depends")
317 mkfile(filename
, "\n".join(data
))
318 pkg
.checksum
["depends"] = getmd5sum(filename
)
319 pkg
.mtime
["depends"] = getmtime(filename
)
322 if self
.treename
== "local":
324 for value
in pkg
.install
.values():
328 filename
= os
.path
.join(path
, "install")
329 mkinstallfile(filename
, pkg
.install
)
330 pkg
.checksum
["install"] = getmd5sum(filename
)
331 pkg
.mtime
["install"] = getmtime(filename
)
333 def gensync(self
, path
):
338 tmpdir
= tempfile
.mkdtemp()
341 for pkg
in self
.pkgs
:
342 mkdescfile(pkg
.fullname(), pkg
)
344 # Generate database archive
346 archive
= os
.path
.join(path
, "%s%s" % (self
.treename
, PM_EXT_DB
))
347 tar
= tarfile
.open(archive
, "w:gz")
348 for root
, dirs
, files
in os
.walk('.'):
350 tar
.add(os
.path
.join(root
, d
), recursive
=False)
352 tar
.add(os
.path
.join(root
, f
))
356 shutil
.rmtree(tmpdir
)
358 def ispkgmodified(self
, pkg
):
364 oldpkg
= self
.getpkg(pkg
.name
)
368 vprint("\toldpkg.checksum : %s" % oldpkg
.checksum
)
369 vprint("\toldpkg.mtime : %s" % oldpkg
.mtime
)
371 for key
in pkg
.mtime
.keys():
372 if key
== "install" \
373 and oldpkg
.mtime
[key
] == (0, 0, 0) \
374 and pkg
.mtime
[key
] == (0, 0, 0):
376 if oldpkg
.mtime
[key
][1:3] != pkg
.mtime
[key
][1:3]:
382 if __name__
== "__main__":
385 # vim: set ts=4 sw=4 et: