2006-10-15 15:04:27 -04:00
|
|
|
#! /usr/bin/python
|
|
|
|
#
|
|
|
|
# Copyright (c) 2006 by Aurelien Foret <orelien@chez.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-10-15 15:04:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
import os
|
2008-04-17 03:02:11 -04:00
|
|
|
import tarfile
|
2006-10-15 15:04:27 -04:00
|
|
|
|
|
|
|
import pmpkg
|
2010-10-26 23:08:52 -04:00
|
|
|
import util
|
2006-10-15 15:04:27 -04:00
|
|
|
|
|
|
|
def _mkfilelist(files):
|
2007-02-21 01:33:13 -05:00
|
|
|
"""Generate a list of files from the list supplied as an argument.
|
|
|
|
|
|
|
|
Each path is decomposed to generate the list of all directories leading
|
|
|
|
to the file.
|
|
|
|
|
|
|
|
Example with 'usr/local/bin/dummy':
|
|
|
|
The resulting list will be
|
|
|
|
usr/
|
|
|
|
usr/local/
|
|
|
|
usr/local/bin/
|
|
|
|
usr/local/bin/dummy
|
|
|
|
"""
|
2010-10-26 23:08:52 -04:00
|
|
|
file_set = set()
|
2007-02-21 01:33:13 -05:00
|
|
|
for f in files:
|
2010-10-26 23:08:52 -04:00
|
|
|
name = util.getfilename(f)
|
|
|
|
file_set.add(name)
|
|
|
|
while "/" in name:
|
|
|
|
[name, tmp] = name.rsplit("/", 1)
|
|
|
|
file_set.add(name + "/")
|
|
|
|
return sorted(file_set)
|
2006-10-15 15:04:27 -04:00
|
|
|
|
|
|
|
def _mkbackuplist(backup):
|
2007-02-21 01:33:13 -05:00
|
|
|
"""
|
|
|
|
"""
|
2010-10-26 23:08:52 -04:00
|
|
|
return ["%s\t%s" % (util.getfilename(i), util.mkmd5sum(i)) for i in backup]
|
2006-10-15 15:04:27 -04:00
|
|
|
|
|
|
|
def _getsection(fd):
|
2007-02-21 01:33:13 -05:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
i = []
|
|
|
|
while 1:
|
|
|
|
line = fd.readline().strip("\n")
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
i.append(line)
|
|
|
|
return i
|
2006-10-15 15:04:27 -04:00
|
|
|
|
|
|
|
def _mksection(title, data):
|
2007-02-21 01:33:13 -05:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
s = ""
|
|
|
|
if isinstance(data, list):
|
|
|
|
s = "\n".join(data)
|
|
|
|
else:
|
|
|
|
s = data
|
|
|
|
return "%%%s%%\n" \
|
|
|
|
"%s\n" % (title, s)
|
2006-10-15 15:04:27 -04:00
|
|
|
|
|
|
|
|
2011-01-22 17:28:43 -05:00
|
|
|
class pmdb(object):
|
2007-02-21 01:33:13 -05:00
|
|
|
"""Database object
|
|
|
|
"""
|
|
|
|
|
2010-10-10 19:05:04 -04:00
|
|
|
def __init__(self, treename, root):
|
2007-02-21 01:33:13 -05:00
|
|
|
self.treename = treename
|
|
|
|
self.pkgs = []
|
2010-05-17 19:47:30 -04:00
|
|
|
self.option = {}
|
2010-10-10 19:05:04 -04:00
|
|
|
if self.treename == "local":
|
2010-10-26 23:08:52 -04:00
|
|
|
self.dbdir = os.path.join(root, util.PM_DBPATH, treename)
|
2010-10-10 19:05:04 -04:00
|
|
|
else:
|
2010-10-26 23:08:52 -04:00
|
|
|
self.dbdir = os.path.join(root, util.PM_SYNCDBPATH, treename)
|
|
|
|
self.dbfile = os.path.join(root, util.PM_SYNCDBPATH, treename + ".db")
|
2007-02-21 01:33:13 -05:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s" % self.treename
|
|
|
|
|
2008-12-17 05:55:07 -05:00
|
|
|
def getverify(self):
|
|
|
|
for value in "Always","Never","Optional":
|
|
|
|
if value in self.treename:
|
|
|
|
return value
|
|
|
|
return "Never"
|
|
|
|
|
2007-02-21 01:33:13 -05:00
|
|
|
def getpkg(self, name):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
for pkg in self.pkgs:
|
|
|
|
if name == pkg.name:
|
|
|
|
return pkg
|
|
|
|
|
|
|
|
def db_read(self, name):
|
|
|
|
"""
|
|
|
|
"""
|
2010-10-10 19:05:04 -04:00
|
|
|
path = self.dbdir
|
2007-02-21 01:33:13 -05:00
|
|
|
if not os.path.isdir(path):
|
|
|
|
return None
|
|
|
|
|
|
|
|
dbentry = ""
|
|
|
|
for roots, dirs, files in os.walk(path):
|
|
|
|
for i in dirs:
|
|
|
|
[pkgname, pkgver, pkgrel] = i.rsplit("-", 2)
|
|
|
|
if pkgname == name:
|
|
|
|
dbentry = i
|
|
|
|
break
|
|
|
|
if not dbentry:
|
|
|
|
return None
|
|
|
|
path = os.path.join(path, dbentry)
|
|
|
|
|
|
|
|
[pkgname, pkgver, pkgrel] = dbentry.rsplit("-", 2)
|
|
|
|
pkg = pmpkg.pmpkg(pkgname, pkgver + "-" + pkgrel)
|
|
|
|
|
|
|
|
# desc
|
|
|
|
filename = os.path.join(path, "desc")
|
2008-02-07 20:36:17 -05:00
|
|
|
if not os.path.isfile(filename):
|
|
|
|
print "invalid db entry found (desc missing) for pkg", pkgname
|
|
|
|
return None
|
|
|
|
fd = open(filename, "r")
|
2007-02-21 01:33:13 -05:00
|
|
|
while 1:
|
|
|
|
line = fd.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
line = line.strip("\n")
|
|
|
|
if line == "%DESC%":
|
|
|
|
pkg.desc = fd.readline().strip("\n")
|
|
|
|
elif line == "%GROUPS%":
|
|
|
|
pkg.groups = _getsection(fd)
|
|
|
|
elif line == "%URL%":
|
|
|
|
pkg.url = fd.readline().strip("\n")
|
|
|
|
elif line == "%LICENSE%":
|
|
|
|
pkg.license = _getsection(fd)
|
|
|
|
elif line == "%ARCH%":
|
|
|
|
pkg.arch = fd.readline().strip("\n")
|
|
|
|
elif line == "%BUILDDATE%":
|
|
|
|
pkg.builddate = fd.readline().strip("\n")
|
|
|
|
elif line == "%INSTALLDATE%":
|
|
|
|
pkg.installdate = fd.readline().strip("\n")
|
|
|
|
elif line == "%PACKAGER%":
|
|
|
|
pkg.packager = fd.readline().strip("\n")
|
|
|
|
elif line == "%REASON%":
|
|
|
|
pkg.reason = int(fd.readline().strip("\n"))
|
|
|
|
elif line == "%SIZE%" or line == "%CSIZE%":
|
|
|
|
pkg.size = int(fd.readline().strip("\n"))
|
|
|
|
elif line == "%MD5SUM%":
|
|
|
|
pkg.md5sum = fd.readline().strip("\n")
|
2008-06-03 22:22:49 -04:00
|
|
|
elif line == "%PGPSIG%":
|
|
|
|
pkg.pgpsig = fd.readline().strip("\n")
|
2007-02-21 01:33:13 -05:00
|
|
|
elif line == "%REPLACES%":
|
|
|
|
pkg.replaces = _getsection(fd)
|
2010-10-30 02:01:30 -04:00
|
|
|
elif line == "%DEPENDS%":
|
|
|
|
pkg.depends = _getsection(fd)
|
|
|
|
elif line == "%OPTDEPENDS%":
|
|
|
|
pkg.optdepends = _getsection(fd)
|
|
|
|
elif line == "%CONFLICTS%":
|
|
|
|
pkg.conflicts = _getsection(fd)
|
|
|
|
elif line == "%PROVIDES%":
|
|
|
|
pkg.provides = _getsection(fd)
|
2007-02-21 01:33:13 -05:00
|
|
|
fd.close()
|
|
|
|
|
|
|
|
# files
|
|
|
|
filename = os.path.join(path, "files")
|
2008-02-07 20:36:17 -05:00
|
|
|
if not os.path.isfile(filename):
|
|
|
|
print "invalid db entry found (files missing) for pkg", pkgname
|
|
|
|
return None
|
|
|
|
fd = open(filename, "r")
|
2007-02-21 01:33:13 -05:00
|
|
|
while 1:
|
|
|
|
line = fd.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
line = line.strip("\n")
|
|
|
|
if line == "%FILES%":
|
|
|
|
while line:
|
|
|
|
line = fd.readline().strip("\n")
|
|
|
|
if line and line[-1] != "/":
|
|
|
|
pkg.files.append(line)
|
|
|
|
if line == "%BACKUP%":
|
|
|
|
pkg.backup = _getsection(fd)
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
# install
|
|
|
|
filename = os.path.join(path, "install")
|
|
|
|
|
|
|
|
return pkg
|
|
|
|
|
|
|
|
#
|
|
|
|
# db_write is used to add both 'local' and 'sync' db entries
|
|
|
|
#
|
|
|
|
def db_write(self, pkg):
|
|
|
|
"""
|
|
|
|
"""
|
2010-10-10 19:05:04 -04:00
|
|
|
path = os.path.join(self.dbdir, pkg.fullname())
|
2010-10-26 23:08:52 -04:00
|
|
|
util.mkdir(path)
|
2007-02-21 01:33:13 -05:00
|
|
|
|
|
|
|
# desc
|
|
|
|
# for local db entries: name, version, desc, groups, url, license,
|
|
|
|
# arch, builddate, installdate, packager,
|
2010-10-30 02:01:30 -04:00
|
|
|
# size, reason, depends, conflicts, provides
|
2007-02-21 01:33:13 -05:00
|
|
|
# for sync entries: name, version, desc, groups, csize, md5sum,
|
2010-10-30 02:01:30 -04:00
|
|
|
# replaces, force, depends, conflicts, provides
|
2007-02-21 01:33:13 -05:00
|
|
|
data = [_mksection("NAME", pkg.name)]
|
|
|
|
data.append(_mksection("VERSION", pkg.version))
|
|
|
|
if pkg.desc:
|
|
|
|
data.append(_mksection("DESC", pkg.desc))
|
|
|
|
if pkg.groups:
|
|
|
|
data.append(_mksection("GROUPS", pkg.groups))
|
2007-12-02 19:11:23 -05:00
|
|
|
if pkg.license:
|
|
|
|
data.append(_mksection("LICENSE", pkg.license))
|
|
|
|
if pkg.arch:
|
|
|
|
data.append(_mksection("ARCH", pkg.arch))
|
|
|
|
if pkg.builddate:
|
|
|
|
data.append(_mksection("BUILDDATE", pkg.builddate))
|
|
|
|
if pkg.packager:
|
|
|
|
data.append(_mksection("PACKAGER", pkg.packager))
|
2010-10-30 02:01:30 -04:00
|
|
|
if pkg.depends:
|
|
|
|
data.append(_mksection("DEPENDS", pkg.depends))
|
|
|
|
if pkg.optdepends:
|
|
|
|
data.append(_mksection("OPTDEPENDS", pkg.optdepends))
|
|
|
|
if pkg.conflicts:
|
|
|
|
data.append(_mksection("CONFLICTS", pkg.conflicts))
|
|
|
|
if pkg.provides:
|
|
|
|
data.append(_mksection("PROVIDES", pkg.provides))
|
2011-06-22 14:58:07 -04:00
|
|
|
if pkg.url:
|
|
|
|
data.append(_mksection("URL", pkg.url))
|
2007-02-21 01:33:13 -05:00
|
|
|
if self.treename == "local":
|
|
|
|
if pkg.installdate:
|
|
|
|
data.append(_mksection("INSTALLDATE", pkg.installdate))
|
|
|
|
if pkg.size:
|
|
|
|
data.append(_mksection("SIZE", pkg.size))
|
|
|
|
if pkg.reason:
|
|
|
|
data.append(_mksection("REASON", pkg.reason))
|
|
|
|
else:
|
2008-02-13 23:42:34 -05:00
|
|
|
data.append(_mksection("FILENAME", pkg.filename()))
|
2007-02-26 03:33:48 -05:00
|
|
|
if pkg.replaces:
|
|
|
|
data.append(_mksection("REPLACES", pkg.replaces))
|
2007-02-21 01:33:13 -05:00
|
|
|
if pkg.csize:
|
|
|
|
data.append(_mksection("CSIZE", pkg.csize))
|
2011-06-22 14:58:07 -04:00
|
|
|
if pkg.isize:
|
|
|
|
data.append(_mksection("ISIZE", pkg.isize))
|
2007-02-21 01:33:13 -05:00
|
|
|
if pkg.md5sum:
|
|
|
|
data.append(_mksection("MD5SUM", pkg.md5sum))
|
2008-06-03 22:22:49 -04:00
|
|
|
if pkg.pgpsig:
|
|
|
|
data.append(_mksection("PGPSIG", pkg.pgpsig))
|
2007-02-21 01:33:13 -05:00
|
|
|
if data:
|
|
|
|
data.append("")
|
|
|
|
filename = os.path.join(path, "desc")
|
2010-10-26 23:08:52 -04:00
|
|
|
util.mkfile(filename, "\n".join(data))
|
2007-02-21 01:33:13 -05:00
|
|
|
|
|
|
|
# files
|
|
|
|
# for local entries, fields are: files, backup
|
|
|
|
# for sync ones: none
|
|
|
|
if self.treename == "local":
|
|
|
|
data = []
|
|
|
|
if pkg.files:
|
|
|
|
data.append(_mksection("FILES", _mkfilelist(pkg.files)))
|
|
|
|
if pkg.backup:
|
|
|
|
data.append(_mksection("BACKUP", _mkbackuplist(pkg.backup)))
|
|
|
|
if data:
|
|
|
|
data.append("")
|
|
|
|
filename = os.path.join(path, "files")
|
2010-10-26 23:08:52 -04:00
|
|
|
util.mkfile(filename, "\n".join(data))
|
2007-02-21 01:33:13 -05:00
|
|
|
|
|
|
|
# install
|
|
|
|
if self.treename == "local":
|
|
|
|
empty = 1
|
|
|
|
for value in pkg.install.values():
|
|
|
|
if value:
|
|
|
|
empty = 0
|
|
|
|
if not empty:
|
|
|
|
filename = os.path.join(path, "install")
|
2010-10-26 23:08:52 -04:00
|
|
|
util.mkinstallfile(filename, pkg.install)
|
2007-02-21 01:33:13 -05:00
|
|
|
|
2010-10-10 19:05:04 -04:00
|
|
|
def gensync(self):
|
2007-02-21 01:33:13 -05:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
2010-10-10 19:05:04 -04:00
|
|
|
if not self.dbfile:
|
|
|
|
return
|
2007-02-21 01:33:13 -05:00
|
|
|
curdir = os.getcwd()
|
2010-10-10 19:05:04 -04:00
|
|
|
os.chdir(self.dbdir)
|
2007-02-21 01:33:13 -05:00
|
|
|
|
|
|
|
# Generate database archive
|
2010-10-10 19:05:04 -04:00
|
|
|
tar = tarfile.open(self.dbfile, "w:gz")
|
2010-10-10 19:05:03 -04:00
|
|
|
for i in os.listdir("."):
|
|
|
|
tar.add(i)
|
2008-04-17 03:02:11 -04:00
|
|
|
tar.close()
|
2007-02-21 01:33:13 -05:00
|
|
|
|
|
|
|
os.chdir(curdir)
|
|
|
|
|
|
|
|
# vim: set ts=4 sw=4 et:
|