| 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 import logging |
| 6 | 7 |
| 7 import object_store | 8 import object_store |
| 8 import operator | 9 import operator |
| 9 | 10 |
| 10 class BranchUtility(object): | 11 class BranchUtility(object): |
| 11 def __init__(self, base_path, default_branches, fetcher, object_store): | 12 def __init__(self, base_path, default_branches, fetcher, object_store): |
| 12 self._base_path = base_path | 13 self._base_path = base_path |
| 13 self._default_branches = default_branches | 14 self._default_branches = default_branches |
| 14 self._fetcher = fetcher | 15 self._fetcher = fetcher |
| 15 self._object_store = object_store | 16 self._object_store = object_store |
| (...skipping 28 matching lines...) Expand all Loading... |
| 44 for development that should be handled differently. | 45 for development that should be handled differently. |
| 45 """ | 46 """ |
| 46 if channel_name in ['trunk', 'local']: | 47 if channel_name in ['trunk', 'local']: |
| 47 return channel_name | 48 return channel_name |
| 48 | 49 |
| 49 branch_number = self._object_store.Get(channel_name + '.' + self._base_path, | 50 branch_number = self._object_store.Get(channel_name + '.' + self._base_path, |
| 50 object_store.BRANCH_UTILITY).Get() | 51 object_store.BRANCH_UTILITY).Get() |
| 51 if branch_number is not None: | 52 if branch_number is not None: |
| 52 return branch_number | 53 return branch_number |
| 53 | 54 |
| 54 fetch_data = self._fetcher.Fetch(self._base_path).content | 55 try: |
| 55 version_json = json.loads(fetch_data) | 56 version_json = json.loads(self._fetcher.Fetch(self._base_path).content) |
| 57 except Exception as e: |
| 58 # This can happen if omahaproxy is misbehaving, which we've seen before. |
| 59 # Quick hack fix: just serve from trunk until it's fixed. |
| 60 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' |
| 61 'Falling back to "trunk".' % e) |
| 62 return 'trunk' |
| 63 |
| 56 branch_numbers = {} | 64 branch_numbers = {} |
| 57 for entry in version_json: | 65 for entry in version_json: |
| 58 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: | 66 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: |
| 59 continue | 67 continue |
| 60 for version in entry['versions']: | 68 for version in entry['versions']: |
| 61 if version['channel'] != channel_name: | 69 if version['channel'] != channel_name: |
| 62 continue | 70 continue |
| 63 branch = version['version'].split('.')[2] | 71 branch = version['version'].split('.')[2] |
| 64 if branch not in branch_numbers: | 72 if branch not in branch_numbers: |
| 65 branch_numbers[branch] = 0 | 73 branch_numbers[branch] = 0 |
| 66 else: | 74 else: |
| 67 branch_numbers[branch] += 1 | 75 branch_numbers[branch] += 1 |
| 68 | 76 |
| 69 sorted_branches = sorted(branch_numbers.iteritems(), | 77 sorted_branches = sorted(branch_numbers.iteritems(), |
| 70 None, | 78 None, |
| 71 operator.itemgetter(1), | 79 operator.itemgetter(1), |
| 72 True) | 80 True) |
| 73 self._object_store.Set(channel_name + '.' + self._base_path, | 81 self._object_store.Set(channel_name + '.' + self._base_path, |
| 74 sorted_branches[0][0], | 82 sorted_branches[0][0], |
| 75 object_store.BRANCH_UTILITY) | 83 object_store.BRANCH_UTILITY) |
| 76 | 84 |
| 77 return sorted_branches[0][0] | 85 return sorted_branches[0][0] |
| OLD | NEW |