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