aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'portage_with_autodep/pym/portage/util/mtimedb.py')
-rw-r--r--portage_with_autodep/pym/portage/util/mtimedb.py75
1 files changed, 61 insertions, 14 deletions
diff --git a/portage_with_autodep/pym/portage/util/mtimedb.py b/portage_with_autodep/pym/portage/util/mtimedb.py
index 67f93e8..30922a9 100644
--- a/portage_with_autodep/pym/portage/util/mtimedb.py
+++ b/portage_with_autodep/pym/portage/util/mtimedb.py
@@ -1,4 +1,4 @@
-# Copyright 2010-2011 Gentoo Foundation
+# Copyright 2010-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
__all__ = ['MtimeDB']
@@ -9,35 +9,77 @@ try:
except ImportError:
import pickle
+import errno
+import io
+import json
+import sys
+
import portage
+from portage import _encodings
+from portage import _unicode_decode
from portage import _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):
+
+ # JSON read support has been available since portage-2.1.10.49.
+ _json_write = True
+
+ _json_write_opts = {
+ "ensure_ascii": False,
+ "indent": "\t",
+ "sort_keys": True
+ }
+ if sys.hexversion < 0x30200F0:
+ # indent only supports int number of spaces
+ _json_write_opts["indent"] = 4
+
def __init__(self, filename):
dict.__init__(self)
self.filename = filename
self._load(filename)
def _load(self, filename):
+ f = None
+ content = None
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().
+ content = f.read()
+ except EnvironmentError as e:
+ if getattr(e, 'errno', None) in (errno.ENOENT, errno.EACCES):
pass
- d = mypickle.load()
- f.close()
- del f
- except (IOError, OSError, EOFError, ValueError, pickle.UnpicklingError) as e:
- if isinstance(e, pickle.UnpicklingError):
+ else:
writemsg(_("!!! Error loading '%s': %s\n") % \
- (filename, str(e)), noiselevel=-1)
- del e
+ (filename, e), noiselevel=-1)
+ finally:
+ if f is not None:
+ f.close()
+
+ d = None
+ if content:
+ try:
+ d = json.loads(_unicode_decode(content,
+ encoding=_encodings['repo.content'], errors='strict'))
+ except SystemExit:
+ raise
+ except Exception as e:
+ try:
+ mypickle = pickle.Unpickler(io.BytesIO(content))
+ try:
+ mypickle.find_global = None
+ except AttributeError:
+ # Python >=3
+ pass
+ d = mypickle.load()
+ except SystemExit:
+ raise
+ except Exception:
+ writemsg(_("!!! Error loading '%s': %s\n") % \
+ (filename, e), noiselevel=-1)
+
+ if d is None:
d = {}
if "old" in d:
@@ -74,7 +116,12 @@ class MtimeDB(dict):
except EnvironmentError:
pass
else:
- pickle.dump(d, f, protocol=2)
+ if self._json_write:
+ f.write(_unicode_encode(
+ json.dumps(d, **self._json_write_opts),
+ encoding=_encodings['repo.content'], errors='strict'))
+ else:
+ pickle.dump(d, f, protocol=2)
f.close()
apply_secpass_permissions(self.filename,
uid=uid, gid=portage_gid, mode=0o644)