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