| 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 import logging |
| 7 import operator | 7 import operator |
| 8 | 8 |
| 9 from object_store_creator import ObjectStoreCreator | 9 from object_store_creator import ObjectStoreCreator |
| 10 | 10 |
| 11 class BranchUtility(object): | 11 class BranchUtility(object): |
| 12 def __init__(self, fetch_url, fetcher, object_store=None): | 12 def __init__(self, fetch_url, fetcher, object_store=None): |
| 13 self._fetch_url = fetch_url | 13 self._fetch_url = fetch_url |
| 14 self._fetcher = fetcher | 14 self._fetcher = fetcher |
| 15 if object_store is None: | 15 if object_store is None: |
| 16 object_store = ObjectStoreCreator(BranchUtility).Create() | 16 object_store = ObjectStoreCreator(BranchUtility).Create() |
| 17 self._object_store = object_store | 17 self._object_store = object_store |
| 18 | 18 |
| 19 @staticmethod | 19 @staticmethod |
| 20 def GetAllBranchNames(): | 20 def GetAllBranchNames(): |
| 21 return ['stable', 'beta', 'dev', 'trunk'] | 21 return ['stable', 'beta', 'dev', 'trunk'] |
| 22 | 22 |
| 23 def GetAllBranchNumbers(self): | 23 def GetAllBranchNumbers(self): |
| 24 return [(branch, self.GetBranchNumberForChannelName(branch)) | 24 return [(branch, self.GetChannelInfoForChannelName(branch)['branch']) |
| 25 for branch in BranchUtility.GetAllBranchNames()] | 25 for branch in BranchUtility.GetAllBranchNames()] |
| 26 | 26 |
| 27 def GetAllVersionNumbers(self): |
| 28 return [self.GetChannelInfoForChannelName(branch)['version'] |
| 29 for branch in self.GetAllBranchNames()] |
| 30 |
| 31 def GetChannelInfoForAllChannels(self): |
| 32 return [self.GetChannelInfoForChannelName(branch) |
| 33 for branch in self.GetAllBranchNames()] |
| 34 |
| 27 @staticmethod | 35 @staticmethod |
| 28 def SplitChannelNameFromPath(path): | 36 def SplitChannelNameFromPath(path): |
| 29 """Splits the channel name out of |path|, returning the tuple | 37 """Splits the channel name out of |path|, returning the tuple |
| 30 (channel_name, real_path). If the channel cannot be determined then returns | 38 (channel_name, real_path). If the channel cannot be determined then returns |
| 31 (None, path). | 39 (None, path). |
| 32 """ | 40 """ |
| 33 if '/' in path: | 41 if '/' in path: |
| 34 first, second = path.split('/', 1) | 42 first, second = path.split('/', 1) |
| 35 else: | 43 else: |
| 36 first, second = (path, '') | 44 first, second = (path, '') |
| 37 if first in ['trunk', 'dev', 'beta', 'stable']: | 45 if first in ['trunk', 'dev', 'beta', 'stable']: |
| 38 return (first, second) | 46 return (first, second) |
| 39 return (None, path) | 47 return (None, path) |
| 40 | 48 |
| 41 def GetBranchNumberForChannelName(self, channel_name): | 49 def GetChannelInfoForChannelName(self, channel): |
| 42 """Returns the branch number for a channel name. | 50 return { |
| 51 'name': channel, |
| 52 'branch': self._ExtractFromVersionJson(channel, 'branch'), |
| 53 'version': self._ExtractFromVersionJson(channel, 'version') |
| 54 } |
| 55 |
| 56 def _ExtractFromVersionJson(self, channel_name, data_type): |
| 57 """Returns the branch or version number for a channel name. |
| 43 """ | 58 """ |
| 44 if channel_name == 'trunk': | 59 if channel_name == 'trunk': |
| 45 return 'trunk' | 60 return 'trunk' |
| 46 | 61 |
| 47 branch_number = self._object_store.Get(channel_name).Get() | 62 data = self._object_store.Get('%s.%s' % (channel_name, data_type)).Get() |
| 48 if branch_number is not None: | 63 if data is not None: |
| 49 return branch_number | 64 return data |
| 50 | 65 |
| 51 try: | 66 try: |
| 52 version_json = json.loads(self._fetcher.Fetch(self._fetch_url).content) | 67 version_json = json.loads(self._fetcher.Fetch(self._fetch_url).content) |
| 53 except Exception as e: | 68 except Exception as e: |
| 54 # This can happen if omahaproxy is misbehaving, which we've seen before. | 69 # This can happen if omahaproxy is misbehaving, which we've seen before. |
| 55 # Quick hack fix: just serve from trunk until it's fixed. | 70 # Quick hack fix: just serve from trunk until it's fixed. |
| 56 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' | 71 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' |
| 57 'Falling back to "trunk".' % e) | 72 'Falling back to "trunk".' % e) |
| 58 return 'trunk' | 73 return 'trunk' |
| 59 | 74 |
| 60 branch_numbers = {} | 75 numbers = {} |
| 61 for entry in version_json: | 76 for entry in version_json: |
| 62 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: | 77 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: |
| 63 continue | 78 continue |
| 64 for version in entry['versions']: | 79 for version in entry['versions']: |
| 65 if version['channel'] != channel_name: | 80 if version['channel'] != channel_name: |
| 66 continue | 81 continue |
| 67 branch = version['version'].split('.')[2] | 82 if data_type == 'branch': |
| 68 if branch not in branch_numbers: | 83 number = version['version'].split('.')[2] |
| 69 branch_numbers[branch] = 0 | 84 elif data_type == 'version': |
| 85 number = version['version'].split('.')[0] |
| 86 if number not in numbers: |
| 87 numbers[number] = 0 |
| 70 else: | 88 else: |
| 71 branch_numbers[branch] += 1 | 89 numbers[number] += 1 |
| 72 | 90 |
| 73 sorted_branches = sorted(branch_numbers.iteritems(), | 91 sorted_numbers = sorted(numbers.iteritems(), |
| 74 None, | 92 None, |
| 75 operator.itemgetter(1), | 93 operator.itemgetter(1), |
| 76 True) | 94 True) |
| 77 self._object_store.Set(channel_name, sorted_branches[0][0]) | 95 self._object_store.Set('%s.%s' % (channel_name, data_type), |
| 96 sorted_numbers[0][0]) |
| 78 | 97 |
| 79 return sorted_branches[0][0] | 98 return sorted_numbers[0][0] |
| OLD | NEW |