Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1870)

Unified Diff: rietveld.py

Issue 11280143: Create CachingRietveld to automatically cache results for presubmit checks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: add tests Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « presubmit_support.py ('k') | tests/git_cl_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: rietveld.py
diff --git a/rietveld.py b/rietveld.py
index d4ac39bab08060ea3c6414d3b9ad32b986d266f0..b707913bb332257cea234412972925cda2caba05 100644
--- a/rietveld.py
+++ b/rietveld.py
@@ -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)
« no previous file with comments | « presubmit_support.py ('k') | tests/git_cl_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698