Move the MtimeDB class to portage.util.mtimedb and deprecate the commit_mtimedb
authorZac Medico <zmedico@gentoo.org>
Sun, 28 Feb 2010 04:09:59 +0000 (04:09 -0000)
committerZac Medico <zmedico@gentoo.org>
Sun, 28 Feb 2010 04:09:59 +0000 (04:09 -0000)
function.

svn path=/main/trunk/; revision=15490

pym/portage/__init__.py
pym/portage/_deprecated.py
pym/portage/util/mtimedb.py [new file with mode: 0644]

index 595580fd0c543ec13126f461194cc60a6dc45b3c..5f1b05e767e47e421f48ebcbfb9907cd9f506a34 100644 (file)
@@ -131,12 +131,14 @@ try:
                'portage.util.ExtractKernelVersion:ExtractKernelVersion',
                'portage.util.listdir:cacheddir,listdir',
                'portage.util.movefile:movefile',
+               'portage.util.mtimedb:MtimeDB',
                'portage.versions',
                'portage.versions:best,catpkgsplit,catsplit,cpv_getkey,' + \
                        'cpv_getkey@getCPFromCPV,endversion_keys,' + \
                        'suffix_value@endversion,pkgcmp,pkgsplit,vercmp,ververify',
                'portage.xpak',
-               'portage._deprecated:dep_virtual,digestParseFile,getvirtuals,pkgmerge',
+               'portage._deprecated:commit_mtimedb,dep_virtual,' + \
+                       'digestParseFile,getvirtuals,pkgmerge',
        )
 
        import portage.const
@@ -535,92 +537,18 @@ def deprecated_profile_check(settings=None):
                writemsg("\n\n", noiselevel=-1)
        return True
 
-def commit_mtimedb(mydict=None, filename=None):
-       if mydict is None:
-               global mtimedb
-               if "mtimedb" not in globals() or mtimedb is None:
-                       return
-               mtimedb.commit()
-               return
-       if filename is None:
-               global mtimedbfile
-               filename = mtimedbfile
-       mydict["version"] = VERSION
-       d = {} # for full backward compat, pickle it as a plain dict object.
-       d.update(mydict)
-       try:
-               f = atomic_ofstream(filename, mode='wb')
-               pickle.dump(d, f, protocol=2)
-               f.close()
-               portage.util.apply_secpass_permissions(filename,
-                       uid=uid, gid=portage_gid, mode=0o644)
-       except (IOError, OSError) as e:
-               pass
-
 def portageexit():
-       global uid,portage_gid,portdb,db
-       if secpass and os.environ.get("SANDBOX_ON") != "1":
+       if secpass > 1 and os.environ.get("SANDBOX_ON") != "1":
                close_portdbapi_caches()
-               commit_mtimedb()
+               try:
+                       mtimedb
+               except NameError:
+                       pass
+               else:
+                       mtimedb.commit()
 
 atexit_register(portageexit)
 
-class MtimeDB(dict):
-       def __init__(self, filename):
-               dict.__init__(self)
-               self.filename = filename
-               self._load(filename)
-
-       def _load(self, filename):
-               try:
-                       f = open(_unicode_encode(filename), 'rb')
-                       mypickle = pickle.Unpickler(f)
-                       try:
-                               mypickle.find_global = None
-                       except AttributeError:
-                               # TODO: If py3k, override Unpickler.find_class().
-                               pass
-                       d = mypickle.load()
-                       f.close()
-                       del f
-               except (IOError, OSError, EOFError, ValueError, pickle.UnpicklingError) as e:
-                       if isinstance(e, pickle.UnpicklingError):
-                               writemsg(_("!!! Error loading '%s': %s\n") % \
-                                       (filename, str(e)), noiselevel=-1)
-                       del e
-                       d = {}
-
-               if "old" in d:
-                       d["updates"] = d["old"]
-                       del d["old"]
-               if "cur" in d:
-                       del d["cur"]
-
-               d.setdefault("starttime", 0)
-               d.setdefault("version", "")
-               for k in ("info", "ldpath", "updates"):
-                       d.setdefault(k, {})
-
-               mtimedbkeys = set(("info", "ldpath", "resume", "resume_backup",
-                       "starttime", "updates", "version"))
-
-               for k in list(d):
-                       if k not in mtimedbkeys:
-                               writemsg(_("Deleting invalid mtimedb key: %s\n") % str(k))
-                               del d[k]
-               self.update(d)
-               self._clean_data = copy.deepcopy(d)
-
-       def commit(self):
-               if not self.filename:
-                       return
-               d = {}
-               d.update(self)
-               # Only commit if the internal state has changed.
-               if d != self._clean_data:
-                       commit_mtimedb(mydict=d, filename=self.filename)
-                       self._clean_data = copy.deepcopy(d)
-
 def create_trees(config_root=None, target_root=None, trees=None):
        if trees is None:
                trees = {}
index a9ed0622b5c93d3e25c7e58c8d20058d0742b5ee..f151cd6d02daf82dc2576611f800241339209eb5 100644 (file)
@@ -16,6 +16,11 @@ from portage.localization import _
 from portage.manifest import Manifest
 from portage.util import writemsg, writemsg_stdout
 
+def commit_mtimedb(mydict=None, filename=None):
+       warnings.warn("portage.commit_mtimedb() is deprecated, " + \
+               "use portage.mtimedb.commit() instead",
+               DeprecationWarning, stacklevel=2)
+
 def digestParseFile(myfilename, mysettings=None):
        """(filename) -- Parses a given file for entries matching:
        <checksumkey> <checksum_hex_string> <filename> <filesize>
diff --git a/pym/portage/util/mtimedb.py b/pym/portage/util/mtimedb.py
new file mode 100644 (file)
index 0000000..cbeb1bb
--- /dev/null
@@ -0,0 +1,77 @@
+# Copyright 2010 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id$
+
+__all__ = ['MtimeDB']
+
+import copy
+
+from portage import pickle, VERSION, _unicode_encode
+from portage.data import portage_gid, uid
+from portage.localization import _
+from portage.util import apply_secpass_permissions, atomic_ofstream, writemsg
+
+class MtimeDB(dict):
+       def __init__(self, filename):
+               dict.__init__(self)
+               self.filename = filename
+               self._load(filename)
+
+       def _load(self, filename):
+               try:
+                       f = open(_unicode_encode(filename), 'rb')
+                       mypickle = pickle.Unpickler(f)
+                       try:
+                               mypickle.find_global = None
+                       except AttributeError:
+                               # TODO: If py3k, override Unpickler.find_class().
+                               pass
+                       d = mypickle.load()
+                       f.close()
+                       del f
+               except (IOError, OSError, EOFError, ValueError, pickle.UnpicklingError) as e:
+                       if isinstance(e, pickle.UnpicklingError):
+                               writemsg(_("!!! Error loading '%s': %s\n") % \
+                                       (filename, str(e)), noiselevel=-1)
+                       del e
+                       d = {}
+
+               if "old" in d:
+                       d["updates"] = d["old"]
+                       del d["old"]
+               if "cur" in d:
+                       del d["cur"]
+
+               d.setdefault("starttime", 0)
+               d.setdefault("version", "")
+               for k in ("info", "ldpath", "updates"):
+                       d.setdefault(k, {})
+
+               mtimedbkeys = set(("info", "ldpath", "resume", "resume_backup",
+                       "starttime", "updates", "version"))
+
+               for k in list(d):
+                       if k not in mtimedbkeys:
+                               writemsg(_("Deleting invalid mtimedb key: %s\n") % str(k))
+                               del d[k]
+               self.update(d)
+               self._clean_data = copy.deepcopy(d)
+
+       def commit(self):
+               if not self.filename:
+                       return
+               d = {}
+               d.update(self)
+               # Only commit if the internal state has changed.
+               if d != self._clean_data:
+                       d["version"] = VERSION
+                       try:
+                               f = atomic_ofstream(self.filename, mode='wb')
+                       except EnvironmentError:
+                               pass
+                       else:
+                               pickle.dump(d, f, protocol=2)
+                               f.close()
+                               apply_secpass_permissions(self.filename,
+                                       uid=uid, gid=portage_gid, mode=0o644)
+                               self._clean_data = copy.deepcopy(d)