Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/branch_utility.py |
| diff --git a/chrome/common/extensions/docs/server2/branch_utility.py b/chrome/common/extensions/docs/server2/branch_utility.py |
| index 1d384dba6f39c99d03f229e81f54d90047d83a7a..bc048770ce57cc6c1d1d2a93f7a10cba25731b31 100644 |
| --- a/chrome/common/extensions/docs/server2/branch_utility.py |
| +++ b/chrome/common/extensions/docs/server2/branch_utility.py |
| @@ -4,49 +4,59 @@ |
| import json |
| -OMAHA_PROXY_URL = 'http://omahaproxy.appspot.com/json' |
| - |
| -def SplitChannelNameFromPath(path, default='stable'): |
| - try: |
| - first, second = path.split('/', 1) |
| - except ValueError: |
| - first = path |
| - second ='' |
| - if first in ['trunk', 'dev', 'beta', 'stable']: |
| - return (first, second) |
| - else: |
| - return (default, path) |
| - |
| -def GetBranchNumberForChannelName(channel_name, |
| - urlfetch, |
| - base_path=OMAHA_PROXY_URL): |
| - """Returns an empty string if the branch number cannot be found. |
| - Throws exception on network errors. |
| - """ |
| - if channel_name == 'trunk' or channel_name == 'local': |
| - return channel_name |
| - |
| - fetch_data = urlfetch.fetch(base_path) |
| - if fetch_data.content == '': |
| - raise Exception('Fetch returned zero results.') |
| - |
| - version_json = json.loads(fetch_data.content) |
| - branch_numbers = {} |
| - for entry in version_json: |
| - if entry['os'] not in ['win', 'linux', 'mac', 'cros']: |
| - continue |
| - for version in entry['versions']: |
| - if version['channel'] != channel_name: |
| +import appengine_memcache as memcache |
| +import operator |
| + |
| +class BranchUtility(object): |
| + def __init__(self, base_path, default_branch, fetcher, memcache): |
| + self._base_path = base_path |
| + self._default_branch = default_branch |
| + self._fetcher = fetcher |
| + self._memcache = memcache |
| + |
| + def SplitChannelNameFromPath(self, path): |
| + try: |
| + first, second = path.split('/', 1) |
| + except ValueError: |
| + first = path |
| + second = '' |
| + if first in ['trunk', 'dev', 'beta', 'stable']: |
| + return (first, second) |
| + else: |
| + return (self._default_branch, path) |
| + |
| + def GetBranchNumberForChannelName(self, channel_name): |
| + """Returns an empty string if the branch number cannot be found. |
| + Throws exception on network errors. |
| + """ |
| + if channel_name in ['trunk', 'local']: |
| + return channel_name |
| + |
| + fetch_data = self._memcache.Get(self._base_path, |
| + memcache.MEMCACHE_BRANCH_UTILITY) |
| + if fetch_data is None: |
| + fetch_data = self._fetcher.Fetch(self._base_path).content |
| + # Cache for 24 hours. |
| + self._memcache.Set(self._base_path, |
| + fetch_data, |
| + memcache.MEMCACHE_BRANCH_UTILITY, |
| + 86400) |
| + |
| + version_json = json.loads(fetch_data) |
| + branch_numbers = {} |
| + for entry in version_json: |
| + if entry['os'] not in ['win', 'linux', 'mac', 'cros']: |
| continue |
| - if version['true_branch'] not in branch_numbers: |
| - branch_numbers[version['true_branch']] = 0 |
| - else: |
| - branch_numbers[version['true_branch']] += 1 |
| - |
| - sorted_list = [x for x in branch_numbers.iteritems()] |
| - sorted_list.sort(key = lambda x: x[1]) |
| - sorted_list.reverse() |
| - |
| - branch_number, _ = sorted_list[0] |
| - |
| - return branch_number |
| + for version in entry['versions']: |
| + if version['channel'] != channel_name: |
| + continue |
| + if version['true_branch'] not in branch_numbers: |
| + branch_numbers[version['true_branch']] = 0 |
| + else: |
| + branch_numbers[version['true_branch']] += 1 |
| + |
| + sorted_branches = sorted(branch_numbers.iteritems(), |
| + None, |
| + operator.itemgetter(1), |
| + True) |
| + return sorted_branches[0][0] |
|
not at google - send to devlin
2012/07/19 03:55:19
can you cache this value rather than the unparsed
cduvall
2012/07/19 17:18:28
Done.
|