From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1RyZgr-0005cf-L1 for garchives@archives.gentoo.org; Sat, 18 Feb 2012 02:06:21 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 88451E07ED; Sat, 18 Feb 2012 02:06:11 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 3B1C1E07ED for ; Sat, 18 Feb 2012 02:06:11 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 711311B4004 for ; Sat, 18 Feb 2012 02:06:10 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 56DD9E5400 for ; Sat, 18 Feb 2012 02:06:08 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: <1329530487.42adf1d5d2be6f186e27bfa2a808cfd1b065dd32.zmedico@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/util/_dyn_libs/ X-VCS-Repository: proj/portage X-VCS-Files: pym/portage/util/_dyn_libs/PreservedLibsRegistry.py X-VCS-Directories: pym/portage/util/_dyn_libs/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: 42adf1d5d2be6f186e27bfa2a808cfd1b065dd32 X-VCS-Branch: master Date: Sat, 18 Feb 2012 02:06:08 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: 2c5fc8f7-c7ec-4a81-ae19-03edc7aebdd4 X-Archives-Hash: 668f071e6051b871a052c804bdd6f16e commit: 42adf1d5d2be6f186e27bfa2a808cfd1b065dd32 Author: Zac Medico gentoo org> AuthorDate: Sat Feb 18 02:01:27 2012 +0000 Commit: Zac Medico gentoo org> CommitDate: Sat Feb 18 02:01:27 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/portage.git;a= =3Dcommit;h=3D42adf1d5 PreservedLibsRegistry: add JSON read/write Support serialization as JSON instead of pickle, so that /var/lib/portage/preserved_libs_registry is human readable/writable, for those rare cases where it may be useful. Currently, pickle is still used for writes. The plan is to migrate to JSON after JSON read has been supported for some time. --- .../util/_dyn_libs/PreservedLibsRegistry.py | 61 ++++++++++++++= ++--- 1 files changed, 51 insertions(+), 10 deletions(-) diff --git a/pym/portage/util/_dyn_libs/PreservedLibsRegistry.py b/pym/po= rtage/util/_dyn_libs/PreservedLibsRegistry.py index 510c390..90d6732 100644 --- a/pym/portage/util/_dyn_libs/PreservedLibsRegistry.py +++ b/pym/portage/util/_dyn_libs/PreservedLibsRegistry.py @@ -1,7 +1,8 @@ -# Copyright 1998-2011 Gentoo Foundation +# Copyright 1998-2012 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 =20 import errno +import json import logging import sys =20 @@ -27,6 +28,19 @@ if sys.hexversion >=3D 0x3000000: =20 class PreservedLibsRegistry(object): """ This class handles the tracking of preserved library objects """ + + # Enable this after JSON read has been supported for some time. + _json_write =3D False + + _json_write_opts =3D { + "ensure_ascii": False, + "indent": "\t", + "sort_keys": True + } + if sys.hexversion < 0x3020000: + # indent only supports int number of spaces + _json_write_opts["indent"] =3D 4 + def __init__(self, root, filename): """=20 @param root: root used to check existence of paths in pruneNonExistin= g @@ -56,17 +70,11 @@ class PreservedLibsRegistry(object): """ Reload the registry data from file """ self._data =3D None f =3D None + content =3D None try: f =3D open(_unicode_encode(self._filename, encoding=3D_encodings['fs'], errors=3D'strict'), 'rb') - if os.fstat(f.fileno()).st_size =3D=3D 0: - # ignore empty lock file - pass - else: - self._data =3D pickle.load(f) - except (AttributeError, EOFError, ValueError, pickle.UnpicklingError) = as e: - writemsg_level(_("!!! Error loading '%s': %s\n") % \ - (self._filename, e), level=3Dlogging.ERROR, noiselevel=3D-1) + content =3D f.read() except EnvironmentError as e: if not hasattr(e, 'errno'): raise @@ -79,8 +87,33 @@ class PreservedLibsRegistry(object): finally: if f is not None: f.close() + + # content is empty if it's an empty lock file + if content: + try: + self._data =3D pickle.loads(content) + except SystemExit: + raise + except Exception as e: + try: + self._data =3D json.loads(_unicode_decode(content, + encoding=3D_encodings['repo.content'], errors=3D'strict')) + except SystemExit: + raise + except Exception: + writemsg_level(_("!!! Error loading '%s': %s\n") % + (self._filename, e), level=3Dlogging.ERROR, + noiselevel=3D-1) + if self._data is None: self._data =3D {} + else: + for k, v in self._data.items(): + if isinstance(v, (list, tuple)) and len(v) =3D=3D 3 and \ + isinstance(v[2], set): + # convert set to list, for write with JSONEncoder + self._data[k] =3D (v[0], v[1], list(v[2])) + self._data_orig =3D self._data.copy() self.pruneNonExisting() =20 @@ -97,7 +130,12 @@ class PreservedLibsRegistry(object): return try: f =3D atomic_ofstream(self._filename, 'wb') - pickle.dump(self._data, f, protocol=3D2) + if self._json_write: + f.write(_unicode_encode( + json.dumps(self._data, f, **self._json_write_opts), + encoding=3D_encodings['repo.content'], errors=3D'strict')) + else: + pickle.dump(self._data, f, protocol=3D2) f.close() except EnvironmentError as e: if e.errno !=3D PermissionDenied.errno: @@ -138,6 +176,9 @@ class PreservedLibsRegistry(object): self._normalize_counter(self._data[cps][1]) =3D=3D counter: del self._data[cps] elif len(paths) > 0: + if isinstance(paths, set): + # convert set to list, for write with JSONEncoder + paths =3D list(paths) self._data[cps] =3D (cpv, counter, paths) =20 def unregister(self, cpv, slot, counter):