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 --- git_config.py | 54 ++++++++++++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 32 deletions(-) (limited to 'git_config.py') diff --git a/git_config.py b/git_config.py index 32879ec7..380bdd24 100644 --- a/git_config.py +++ b/git_config.py @@ -15,8 +15,8 @@ from __future__ import print_function +import json import os -import pickle import re import subprocess import sys @@ -80,7 +80,7 @@ class GitConfig(object): return cls(configfile = os.path.join(gitdir, 'config'), defaults = defaults) - def __init__(self, configfile, defaults=None, pickleFile=None): + def __init__(self, configfile, defaults=None, jsonFile=None): self.file = configfile self.defaults = defaults self._cache_dict = None @@ -88,12 +88,11 @@ class GitConfig(object): self._remotes = {} self._branches = {} - if pickleFile is None: - self._pickle = os.path.join( + self._json = jsonFile + if self._json is None: + self._json = os.path.join( os.path.dirname(self.file), - '.repopickle_' + os.path.basename(self.file)) - else: - self._pickle = pickleFile + '.repo_' + os.path.basename(self.file) + '.json') def Has(self, name, include_defaults = True): """Return true if this configuration file has the key. @@ -248,50 +247,41 @@ class GitConfig(object): return self._cache_dict def _Read(self): - d = self._ReadPickle() + d = self._ReadJson() if d is None: d = self._ReadGit() - self._SavePickle(d) + self._SaveJson(d) return d - def _ReadPickle(self): + def _ReadJson(self): try: - if os.path.getmtime(self._pickle) \ + if os.path.getmtime(self._json) \ <= os.path.getmtime(self.file): - os.remove(self._pickle) + os.remove(self._json) return None except OSError: return None try: - Trace(': unpickle %s', self.file) - fd = open(self._pickle, 'rb') + Trace(': parsing %s', self.file) + fd = open(self._json) try: - return pickle.load(fd) + return json.load(fd) finally: fd.close() - except EOFError: - os.remove(self._pickle) - return None - except IOError: - os.remove(self._pickle) - return None - except pickle.PickleError: - os.remove(self._pickle) + except (IOError, ValueError): + os.remove(self._json) return None - def _SavePickle(self, cache): + def _SaveJson(self, cache): try: - fd = open(self._pickle, 'wb') + fd = open(self._json, 'w') try: - pickle.dump(cache, fd, pickle.HIGHEST_PROTOCOL) + json.dump(cache, fd, indent=2) finally: fd.close() - except IOError: - if os.path.exists(self._pickle): - os.remove(self._pickle) - except pickle.PickleError: - if os.path.exists(self._pickle): - os.remove(self._pickle) + except (IOError, TypeError): + if os.path.exists(self.json): + os.remove(self._json) def _ReadGit(self): """ -- cgit v1.2.3-54-g00ecf