Split reading/writing of $PKGDIR/Packages into a PackageIndex class.
authorZac Medico <zmedico@gentoo.org>
Tue, 22 May 2007 22:38:27 +0000 (22:38 -0000)
committerZac Medico <zmedico@gentoo.org>
Tue, 22 May 2007 22:38:27 +0000 (22:38 -0000)
svn path=/main/trunk/; revision=6584

pym/portage/dbapi/bintree.py
pym/portage/getbinpkg.py

index ded6098c1d18287cc779a3886f966dd7364c184c..5ec32d252bfc7e191206b2d66578be47efc3375b 100644 (file)
@@ -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()
 
index 070b036b57e52a42c18a3bbc4dc2d74ed005c600..f21dce364a868dda3582568345e4e5f0a5ec7773 100644 (file)
@@ -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())