| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import json | 5 import json |
| 6 | 6 |
| 7 OMAHA_PROXY_URL = 'http://omahaproxy.appspot.com/json' | 7 import appengine_memcache as memcache |
| 8 import operator |
| 8 | 9 |
| 9 def SplitChannelNameFromPath(path, default='stable'): | 10 class BranchUtility(object): |
| 10 try: | 11 def __init__(self, base_path, default_branch, fetcher, memcache): |
| 11 first, second = path.split('/', 1) | 12 self._base_path = base_path |
| 12 except ValueError: | 13 self._default_branch = default_branch |
| 13 first = path | 14 self._fetcher = fetcher |
| 14 second ='' | 15 self._memcache = memcache |
| 15 if first in ['trunk', 'dev', 'beta', 'stable']: | |
| 16 return (first, second) | |
| 17 else: | |
| 18 return (default, path) | |
| 19 | 16 |
| 20 def GetBranchNumberForChannelName(channel_name, | 17 def SplitChannelNameFromPath(self, path): |
| 21 urlfetch, | 18 try: |
| 22 base_path=OMAHA_PROXY_URL): | 19 first, second = path.split('/', 1) |
| 23 """Returns an empty string if the branch number cannot be found. | 20 except ValueError: |
| 24 Throws exception on network errors. | 21 first = path |
| 25 """ | 22 second = '' |
| 26 if channel_name == 'trunk' or channel_name == 'local': | 23 if first in ['trunk', 'dev', 'beta', 'stable']: |
| 27 return channel_name | 24 return (first, second) |
| 25 else: |
| 26 return (self._default_branch, path) |
| 28 | 27 |
| 29 fetch_data = urlfetch.fetch(base_path) | 28 def GetBranchNumberForChannelName(self, channel_name): |
| 30 if fetch_data.content == '': | 29 """Returns an empty string if the branch number cannot be found. |
| 31 raise Exception('Fetch returned zero results.') | 30 Throws exception on network errors. |
| 31 """ |
| 32 if channel_name in ['trunk', 'local']: |
| 33 return channel_name |
| 32 | 34 |
| 33 version_json = json.loads(fetch_data.content) | 35 branch_number = self._memcache.Get(channel_name + '.' + self._base_path, |
| 34 branch_numbers = {} | 36 memcache.MEMCACHE_BRANCH_UTILITY) |
| 35 for entry in version_json: | 37 if branch_number is not None: |
| 36 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: | 38 return branch_number |
| 37 continue | 39 |
| 38 for version in entry['versions']: | 40 fetch_data = self._fetcher.Fetch(self._base_path).content |
| 39 if version['channel'] != channel_name: | 41 version_json = json.loads(fetch_data) |
| 42 branch_numbers = {} |
| 43 for entry in version_json: |
| 44 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: |
| 40 continue | 45 continue |
| 41 if version['true_branch'] not in branch_numbers: | 46 for version in entry['versions']: |
| 42 branch_numbers[version['true_branch']] = 0 | 47 if version['channel'] != channel_name: |
| 43 else: | 48 continue |
| 44 branch_numbers[version['true_branch']] += 1 | 49 if version['true_branch'] not in branch_numbers: |
| 50 branch_numbers[version['true_branch']] = 0 |
| 51 else: |
| 52 branch_numbers[version['true_branch']] += 1 |
| 45 | 53 |
| 46 sorted_list = [x for x in branch_numbers.iteritems()] | 54 sorted_branches = sorted(branch_numbers.iteritems(), |
| 47 sorted_list.sort(key = lambda x: x[1]) | 55 None, |
| 48 sorted_list.reverse() | 56 operator.itemgetter(1), |
| 57 True) |
| 58 # Cache for 24 hours. |
| 59 self._memcache.Set(channel_name + '.' + self._base_path, |
| 60 sorted_branches[0][0], |
| 61 memcache.MEMCACHE_BRANCH_UTILITY, |
| 62 86400) |
| 49 | 63 |
| 50 branch_number, _ = sorted_list[0] | 64 return sorted_branches[0][0] |
| 51 | |
| 52 return branch_number | |
| OLD | NEW |