Commit 4bac4b55 authored by maruel@chromium.org's avatar maruel@chromium.org

Create CachingRietveld to automatically cache results for presubmit checks.

Multiple presubmit checks may call the same function multiple times, so it's
worth caching the results to speed up the presubmit check run.

Convert presubmit_support, git-cl and gcl to use it.

R=dpranke@chromium.org
BUG=


Review URL: https://chromiumcodereview.appspot.com/11280143

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@169726 0039d316-1c4b-4281-b951-d872f2087c98
parent f01fad37
......@@ -363,7 +363,7 @@ class ChangeInfo(object):
if not self._rpc_server:
if not self.rietveld:
ErrorExit(CODEREVIEW_SETTINGS_FILE_NOT_FOUND)
self._rpc_server = rietveld.Rietveld(self.rietveld, None, None)
self._rpc_server = rietveld.CachingRietveld(self.rietveld, None, None)
return self._rpc_server
def CloseIssue(self):
......
......@@ -742,8 +742,8 @@ or verify this branch is set up to track another (via the --track argument to
"""Returns an upload.RpcServer() to access this review's rietveld instance.
"""
if not self._rpc_server:
self._rpc_server = rietveld.Rietveld(self.GetRietveldServer(),
None, None)
self._rpc_server = rietveld.CachingRietveld(
self.GetRietveldServer(), None, None)
return self._rpc_server
def _IssueSetting(self):
......
......@@ -1237,7 +1237,7 @@ def Main(argv):
logging.info('Found %d file(s).' % len(files))
rietveld_obj = None
if options.rietveld_url:
rietveld_obj = rietveld.Rietveld(
rietveld_obj = rietveld.CachingRietveld(
options.rietveld_url,
options.rietveld_email,
options.rietveld_password)
......
......@@ -14,6 +14,7 @@ The following hypothesis are made:
- A patch set cannot be modified
"""
import copy
import json
import logging
import re
......@@ -395,3 +396,53 @@ class Rietveld(object):
# DEPRECATED.
Send = get
class CachingRietveld(Rietveld):
"""Caches the common queries.
Not to be used in long-standing processes, like the commit queue.
"""
def __init__(self, *args, **kwargs):
super(CachingRietveld, self).__init__(*args, **kwargs)
self._cache = {}
def _lookup(self, function_name, args, update):
"""Caches the return values corresponding to the arguments.
It is important that the arguments are standardized, like None vs False.
"""
function_cache = self._cache.setdefault(function_name, {})
if args not in function_cache:
function_cache[args] = update(*args)
return copy.deepcopy(function_cache[args])
def get_description(self, issue):
return self._lookup(
'get_description',
(issue,),
super(CachingRietveld, self).get_description)
def get_issue_properties(self, issue, messages):
"""Returns the issue properties.
Because in practice the presubmit checks often ask without messages first
and then with messages, always ask with messages and strip off if not asked
for the messages.
"""
# It's a tad slower to request with the message but it's better than
# requesting the properties twice.
data = self._lookup(
'get_issue_properties',
(issue, True),
super(CachingRietveld, self).get_issue_properties)
if not messages:
# Assumes self._lookup uses deepcopy.
del data['messages']
return data
def get_patchset_properties(self, issue, patchset):
return self._lookup(
'get_patchset_properties',
(issue, patchset),
super(CachingRietveld, self).get_patchset_properties)
......@@ -69,6 +69,7 @@ class TestGitCl(TestCase):
self.mock(git_cl.breakpad, 'SendStack', self._mocked_call)
self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock)
self.mock(git_cl.rietveld, 'Rietveld', RietveldMock)
self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock)
self.mock(git_cl.upload, 'RealMain', self.fail)
self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock)
# It's important to reset settings to not have inter-tests interference.
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment