From: Zac Medico Date: Tue, 22 May 2007 22:38:27 +0000 (-0000) Subject: Split reading/writing of $PKGDIR/Packages into a PackageIndex class. X-Git-Tag: v2.2_pre1~1388 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=871e66b69027fc6727a0e8c4c7b7b4ac856d0880;p=portage.git Split reading/writing of $PKGDIR/Packages into a PackageIndex class. svn path=/main/trunk/; revision=6584 --- diff --git a/pym/portage/dbapi/bintree.py b/pym/portage/dbapi/bintree.py index ded6098c1..5ec32d252 100644 --- a/pym/portage/dbapi/bintree.py +++ b/pym/portage/dbapi/bintree.py @@ -115,6 +115,7 @@ class binarytree(object): self._pkg_paths = {} self._all_directory = os.path.isdir( os.path.join(self.pkgdir, "All")) + self._pkgindex = None self._pkgindex_keys = set(["CPV", "SLOT", "MTIME", "SIZE"]) def move_ent(self, mylist): @@ -373,24 +374,16 @@ class binarytree(object): dirs.sort() dirs.insert(0, "All") pkgfile = os.path.join(self.pkgdir, "Packages") - metadata = {} - header = {} + self._pkgindex = portage.getbinpkg.PackageIndex() + header = self._pkgindex.header + metadata = self._pkgindex.packages try: f = open(pkgfile) except EnvironmentError: pass else: try: - header = portage.getbinpkg.readpkgindex(f) - while True: - d = portage.getbinpkg.readpkgindex(f) - if not d: - break - mycpv = d.get("CPV") - if not mycpv: - continue - d.setdefault("SLOT", "0") - metadata[mycpv] = d + self._pkgindex.read(f) finally: f.close() del f @@ -498,19 +491,10 @@ class binarytree(object): stale = set(metadata).difference(cpv_all) for cpv in stale: del metadata[cpv] - cpv_all.sort() - import time from portage.util import atomic_ofstream - header["TIMESTAMP"] = str(long(time.time())) - header["PACKAGES"] = str(len(cpv_all)) f = atomic_ofstream(pkgfile) try: - portage.getbinpkg.writepkgindex(f, header.iteritems()) - for cpv in cpv_all: - d = metadata[cpv] - if d["SLOT"] == "0": - del d["SLOT"] - portage.getbinpkg.writepkgindex(f, d.iteritems()) + self._pkgindex.write(f) finally: f.close() diff --git a/pym/portage/getbinpkg.py b/pym/portage/getbinpkg.py index 070b036b5..f21dce364 100644 --- a/pym/portage/getbinpkg.py +++ b/pym/portage/getbinpkg.py @@ -590,3 +590,35 @@ def writepkgindex(pkgfile, items): for k, v in items: pkgfile.write("%s: %s\n" % (k, v)) pkgfile.write("\n") + +class PackageIndex(object): + + def __init__(self): + self.header = {} + self.packages = {} + + def read(self, pkgfile): + self.header.update(readpkgindex(pkgfile)) + while True: + d = readpkgindex(pkgfile) + if not d: + break + mycpv = d.get("CPV") + if not mycpv: + continue + d.setdefault("SLOT", "0") + self.packages[mycpv] = d + + def write(self, pkgfile): + cpv_all = self.packages.keys() + cpv_all.sort() + import time + self.header["TIMESTAMP"] = str(long(time.time())) + self.header["PACKAGES"] = str(len(cpv_all)) + writepkgindex(pkgfile, self.header.iteritems()) + for cpv in cpv_all: + metadata = self.packages[cpv] + if metadata["SLOT"] == "0": + metadata = metadata.copy() + del metadata["SLOT"] + writepkgindex(pkgfile, metadata.iteritems())