From 85b24acd6a11af3d84cc71fc35ebdb6d3c78065f Mon Sep 17 00:00:00 2001 From: Anthony King Date: Tue, 6 May 2014 15:57:48 +0100 Subject: Use JSON instead of pickle Use JSON as it is shown to be much faster than pickle. Also clean up the loading and saving functions. Change-Id: I45b3dee7b4d59a1c0e0d38d4a83b543ac5839390 --- subcmds/sync.py | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) (limited to 'subcmds/sync.py') diff --git a/subcmds/sync.py b/subcmds/sync.py index a0a68960..6f77310f 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py @@ -14,10 +14,10 @@ # limitations under the License. from __future__ import print_function +import json import netrc from optparse import SUPPRESS_HELP import os -import pickle import re import shutil import socket @@ -760,7 +760,7 @@ class _FetchTimes(object): _ALPHA = 0.5 def __init__(self, manifest): - self._path = os.path.join(manifest.repodir, '.repopickle_fetchtimes') + self._path = os.path.join(manifest.repodir, '.repo_fetchtimes.json') self._times = None self._seen = set() @@ -779,22 +779,17 @@ class _FetchTimes(object): def _Load(self): if self._times is None: try: - f = open(self._path, 'rb') - except IOError: - self._times = {} - return self._times - try: + f = open(self._path) try: - self._times = pickle.load(f) - except IOError: - try: - os.remove(self._path) - except OSError: - pass - self._times = {} - finally: - f.close() - return self._times + self._times = json.load(f) + finally: + f.close() + except (IOError, ValueError): + try: + os.remove(self._path) + except OSError: + pass + self._times = {} def Save(self): if self._times is None: @@ -808,13 +803,13 @@ class _FetchTimes(object): del self._times[name] try: - f = open(self._path, 'wb') + f = open(self._path, 'w') try: - pickle.dump(self._times, f) - except (IOError, OSError, pickle.PickleError): - try: - os.remove(self._path) - except OSError: - pass - finally: - f.close() + json.dump(self._times, f, indent=2) + finally: + f.close() + except (IOError, TypeError): + try: + os.remove(self._path) + except OSError: + pass -- cgit v1.2.3-54-g00ecf