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, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
29 def _mkfilelist(files
):
30 """Generate a list of files from the list supplied as an argument.
32 Each path is decomposed to generate the list of all directories leading
35 Example with 'usr/local/bin/dummy':
36 The resulting list will be
47 [dir, tmp
] = dir.rsplit("/", 1)
48 if not dir + "/" in files
:
53 def _mkbackuplist(backup
):
56 return ["%s\t%s" % (getfilename(i
), mkmd5sum(i
)) for i
in backup
]
63 line
= fd
.readline().strip("\n")
69 def _mksection(title
, data
):
73 if isinstance(data
, list):
85 def __init__(self
, treename
, dbdir
):
86 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 fd
= file(filename
, "r")
129 line
= line
.strip("\n")
131 pkg
.desc
= fd
.readline().strip("\n")
132 elif line
== "%GROUPS%":
133 pkg
.groups
= _getsection(fd
)
134 elif line
== "%URL%":
135 pkg
.url
= fd
.readline().strip("\n")
136 elif line
== "%LICENSE%":
137 pkg
.license
= _getsection(fd
)
138 elif line
== "%ARCH%":
139 pkg
.arch
= fd
.readline().strip("\n")
140 elif line
== "%BUILDDATE%":
141 pkg
.builddate
= fd
.readline().strip("\n")
142 elif line
== "%INSTALLDATE%":
143 pkg
.installdate
= fd
.readline().strip("\n")
144 elif line
== "%PACKAGER%":
145 pkg
.packager
= fd
.readline().strip("\n")
146 elif line
== "%REASON%":
147 pkg
.reason
= int(fd
.readline().strip("\n"))
148 elif line
== "%SIZE%" or line
== "%CSIZE%":
149 pkg
.size
= int(fd
.readline().strip("\n"))
150 elif line
== "%MD5SUM%":
151 pkg
.md5sum
= fd
.readline().strip("\n")
152 elif line
== "%REPLACES%":
153 pkg
.replaces
= _getsection(fd
)
154 elif line
== "%FORCE%":
158 pkg
.checksum
["desc"] = getmd5sum(filename
)
159 pkg
.mtime
["desc"] = getmtime(filename
)
162 filename
= os
.path
.join(path
, "files")
163 fd
= file(filename
, "r")
168 line
= line
.strip("\n")
169 if line
== "%FILES%":
171 line
= fd
.readline().strip("\n")
172 if line
and line
[-1] != "/":
173 pkg
.files
.append(line
)
174 if line
== "%BACKUP%":
175 pkg
.backup
= _getsection(fd
)
177 pkg
.checksum
["files"] = getmd5sum(filename
)
178 pkg
.mtime
["files"] = getmtime(filename
)
181 filename
= os
.path
.join(path
, "depends")
182 fd
= file(filename
, "r")
187 line
= line
.strip("\n")
188 if line
== "%DEPENDS%":
189 pkg
.depends
= _getsection(fd
)
190 elif line
== "%OPTDEPENDS%":
191 pkg
.optdepends
= _getsection(fd
)
192 elif line
== "%CONFLICTS%":
193 pkg
.conflicts
= _getsection(fd
)
194 elif line
== "%PROVIDES%":
195 pkg
.provides
= _getsection(fd
)
196 # TODO this was going to be changed, but isn't anymore
197 #elif line == "%REPLACES%":
198 # pkg.replaces = _getsection(fd)
199 #elif line == "%FORCE%":
203 pkg
.checksum
["depends"] = getmd5sum(filename
)
204 pkg
.mtime
["depends"] = getmtime(filename
)
207 filename
= os
.path
.join(path
, "install")
208 if os
.path
.isfile(filename
):
209 pkg
.checksum
["install"] = getmd5sum(filename
)
210 pkg
.mtime
["install"] = getmtime(filename
)
215 # db_write is used to add both 'local' and 'sync' db entries
217 def db_write(self
, pkg
):
221 if self
.treename
== "local":
222 path
= os
.path
.join(self
.dbdir
, self
.treename
, pkg
.fullname())
224 path
= os
.path
.join(self
.dbdir
, "sync", self
.treename
, pkg
.fullname())
228 # for local db entries: name, version, desc, groups, url, license,
229 # arch, builddate, installdate, packager,
231 # for sync entries: name, version, desc, groups, csize, md5sum,
233 data
= [_mksection("NAME", pkg
.name
)]
234 data
.append(_mksection("VERSION", pkg
.version
))
236 data
.append(_mksection("DESC", pkg
.desc
))
238 data
.append(_mksection("GROUPS", pkg
.groups
))
239 if self
.treename
== "local":
241 data
.append(_mksection("URL", pkg
.url
))
243 data
.append(_mksection("LICENSE", pkg
.license
))
245 data
.append(_mksection("ARCH", pkg
.arch
))
247 data
.append(_mksection("BUILDDATE", pkg
.builddate
))
249 data
.append(_mksection("INSTALLDATE", pkg
.installdate
))
251 data
.append(_mksection("PACKAGER", pkg
.packager
))
253 data
.append(_mksection("SIZE", pkg
.size
))
255 data
.append(_mksection("REASON", pkg
.reason
))
258 data
.append(_mksection("REPLACES", pkg
.replaces
))
260 data
.append(_mksection("FORCE", ""))
262 data
.append(_mksection("CSIZE", pkg
.csize
))
264 data
.append(_mksection("MD5SUM", pkg
.md5sum
))
267 filename
= os
.path
.join(path
, "desc")
268 mkfile(filename
, "\n".join(data
))
269 pkg
.checksum
["desc"] = getmd5sum(filename
)
270 pkg
.mtime
["desc"] = getmtime(filename
)
273 # for local entries, fields are: files, backup
274 # for sync ones: none
275 if self
.treename
== "local":
278 data
.append(_mksection("FILES", _mkfilelist(pkg
.files
)))
280 data
.append(_mksection("BACKUP", _mkbackuplist(pkg
.backup
)))
283 filename
= os
.path
.join(path
, "files")
284 mkfile(filename
, "\n".join(data
))
285 pkg
.checksum
["files"] = getmd5sum(filename
)
286 pkg
.mtime
["files"] = getmtime(filename
)
289 # for local db entries: depends, conflicts, provides
290 # for sync ones: depends, conflicts, provides
293 data
.append(_mksection("DEPENDS", pkg
.depends
))
295 data
.append(_mksection("OPTDEPENDS", pkg
.optdepends
))
297 data
.append(_mksection("CONFLICTS", pkg
.conflicts
))
299 data
.append(_mksection("PROVIDES", pkg
.provides
))
300 #if self.treename != "local":
302 # data.append(_mksection("REPLACES", pkg.replaces))
304 # data.append(_mksection("FORCE", ""))
307 filename
= os
.path
.join(path
, "depends")
308 mkfile(filename
, "\n".join(data
))
309 pkg
.checksum
["depends"] = getmd5sum(filename
)
310 pkg
.mtime
["depends"] = getmtime(filename
)
313 if self
.treename
== "local":
315 for value
in pkg
.install
.values():
319 filename
= os
.path
.join(path
, "install")
320 mkinstallfile(filename
, pkg
.install
)
321 pkg
.checksum
["install"] = getmd5sum(filename
)
322 pkg
.mtime
["install"] = getmtime(filename
)
324 def gensync(self
, path
):
329 tmpdir
= tempfile
.mkdtemp()
332 for pkg
in self
.pkgs
:
333 mkdescfile(pkg
.fullname(), pkg
)
335 # Generate database archive
337 archive
= os
.path
.join(path
, "%s%s" % (self
.treename
, PM_EXT_DB
))
338 os
.system("tar zcf %s *" % archive
)
341 shutil
.rmtree(tmpdir
)
343 def ispkgmodified(self
, pkg
):
349 oldpkg
= self
.getpkg(pkg
.name
)
353 vprint("\toldpkg.checksum : %s" % oldpkg
.checksum
)
354 vprint("\toldpkg.mtime : %s" % oldpkg
.mtime
)
356 for key
in pkg
.mtime
.keys():
357 if key
== "install" \
358 and oldpkg
.mtime
[key
] == (0, 0, 0) \
359 and pkg
.mtime
[key
] == (0, 0, 0):
361 if oldpkg
.mtime
[key
][1:3] != pkg
.mtime
[key
][1:3]:
367 if __name__
== "__main__":
370 # vim: set ts=4 sw=4 et: