OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 """Defines class Rietveld to easily access a rietveld instance. | 5 """Defines class Rietveld to easily access a rietveld instance. |
6 | 6 |
7 Security implications: | 7 Security implications: |
8 | 8 |
9 The following hypothesis are made: | 9 The following hypothesis are made: |
10 - Rietveld enforces: | 10 - Rietveld enforces: |
11 - Nobody else than issue owner can upload a patch set | 11 - Nobody else than issue owner can upload a patch set |
12 - Verifies the issue owner credentials when creating new issues | 12 - Verifies the issue owner credentials when creating new issues |
13 - A issue owner can't change once the issue is created | 13 - A issue owner can't change once the issue is created |
14 - A patch set cannot be modified | 14 - A patch set cannot be modified |
15 """ | 15 """ |
16 | 16 |
| 17 import copy |
17 import json | 18 import json |
18 import logging | 19 import logging |
19 import re | 20 import re |
20 import time | 21 import time |
21 import urllib2 | 22 import urllib2 |
22 | 23 |
23 from third_party import upload | 24 from third_party import upload |
24 import patch | 25 import patch |
25 | 26 |
26 # Hack out upload logging.info() | 27 # Hack out upload logging.info() |
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 if not 'Name or service not known' in e.reason: | 389 if not 'Name or service not known' in e.reason: |
389 # Usually internal GAE flakiness. | 390 # Usually internal GAE flakiness. |
390 raise | 391 raise |
391 # If reaching this line, loop again. Uses a small backoff. | 392 # If reaching this line, loop again. Uses a small backoff. |
392 time.sleep(1+maxtries*2) | 393 time.sleep(1+maxtries*2) |
393 finally: | 394 finally: |
394 upload.ErrorExit = old_error_exit | 395 upload.ErrorExit = old_error_exit |
395 | 396 |
396 # DEPRECATED. | 397 # DEPRECATED. |
397 Send = get | 398 Send = get |
| 399 |
| 400 |
| 401 class CachingRietveld(Rietveld): |
| 402 """Caches the common queries. |
| 403 |
| 404 Not to be used in long-standing processes, like the commit queue. |
| 405 """ |
| 406 def __init__(self, *args, **kwargs): |
| 407 super(CachingRietveld, self).__init__(*args, **kwargs) |
| 408 self._cache = {} |
| 409 |
| 410 def _lookup(self, function_name, args, update): |
| 411 """Caches the return values corresponding to the arguments. |
| 412 |
| 413 It is important that the arguments are standardized, like None vs False. |
| 414 """ |
| 415 function_cache = self._cache.setdefault(function_name, {}) |
| 416 if args not in function_cache: |
| 417 function_cache[args] = update(*args) |
| 418 return copy.deepcopy(function_cache[args]) |
| 419 |
| 420 def get_description(self, issue): |
| 421 return self._lookup( |
| 422 'get_description', |
| 423 (issue,), |
| 424 super(CachingRietveld, self).get_description) |
| 425 |
| 426 def get_issue_properties(self, issue, messages): |
| 427 """Returns the issue properties. |
| 428 |
| 429 Because in practice the presubmit checks often ask without messages first |
| 430 and then with messages, always ask with messages and strip off if not asked |
| 431 for the messages. |
| 432 """ |
| 433 # It's a tad slower to request with the message but it's better than |
| 434 # requesting the properties twice. |
| 435 data = self._lookup( |
| 436 'get_issue_properties', |
| 437 (issue, True), |
| 438 super(CachingRietveld, self).get_issue_properties) |
| 439 if not messages: |
| 440 # Assumes self._lookup uses deepcopy. |
| 441 del data['messages'] |
| 442 return data |
| 443 |
| 444 def get_patchset_properties(self, issue, patchset): |
| 445 return self._lookup( |
| 446 'get_patchset_properties', |
| 447 (issue, patchset), |
| 448 super(CachingRietveld, self).get_patchset_properties) |
OLD | NEW |