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 if data_type == 'branch': |
58 return 'trunk' | |
59 elif data_type == 'version': | |
60 return '0' | |
43 | 61 |
44 branch_number = self._object_store.Get(channel_name + '.' + self._base_path, | 62 data = self._object_store.Get( |
45 object_store.BRANCH_UTILITY).Get() | 63 '%s.%s.%s' % (channel_name, self._base_path, data_type), |
46 if branch_number is not None: | 64 object_store.BRANCH_UTILITY).Get() |
47 return branch_number | 65 if data is not None: |
66 return data | |
48 | 67 |
49 try: | 68 try: |
50 version_json = json.loads(self._fetcher.Fetch(self._base_path).content) | 69 version_json = json.loads(self._fetcher.Fetch(self._base_path).content) |
51 except Exception as e: | 70 except Exception as e: |
52 # This can happen if omahaproxy is misbehaving, which we've seen before. | 71 # This can happen if omahaproxy is misbehaving, which we've seen before. |
53 # Quick hack fix: just serve from trunk until it's fixed. | 72 # Quick hack fix: just serve from trunk until it's fixed. |
54 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' | 73 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' |
55 'Falling back to "trunk".' % e) | 74 'Falling back to "trunk".' % e) |
56 return 'trunk' | 75 if data_type == 'branch': |
76 return 'trunk' | |
77 elif data_type == 'version': | |
78 return '0' | |
57 | 79 |
58 branch_numbers = {} | 80 numbers = {} |
59 for entry in version_json: | 81 for entry in version_json: |
60 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: | 82 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: |
61 continue | 83 continue |
62 for version in entry['versions']: | 84 for version in entry['versions']: |
63 if version['channel'] != channel_name: | 85 if version['channel'] != channel_name: |
64 continue | 86 continue |
65 branch = version['version'].split('.')[2] | 87 if data_type == 'branch': |
66 if branch not in branch_numbers: | 88 number = version['version'].split('.')[2] |
67 branch_numbers[branch] = 0 | 89 elif data_type == 'version': |
90 number = version['version'].split('.')[0] | |
91 if number not in numbers: | |
92 numbers[number] = 0 | |
68 else: | 93 else: |
69 branch_numbers[branch] += 1 | 94 numbers[number] += 1 |
70 | 95 sorted_numbers = sorted(numbers.iteritems(), |
71 sorted_branches = sorted(branch_numbers.iteritems(), | 96 None, |
72 None, | 97 operator.itemgetter(1), |
73 operator.itemgetter(1), | 98 True) |
74 True) | 99 self._object_store.Set(channel_name + '.' + self._base_path + |
cduvall
2013/03/25 22:59:43
Use %s style string
epeterson
2013/03/27 22:36:09
Done.
| |
75 self._object_store.Set(channel_name + '.' + self._base_path, | 100 '.' + data_type, |
76 sorted_branches[0][0], | 101 sorted_numbers[0][0], |
77 object_store.BRANCH_UTILITY) | 102 object_store.BRANCH_UTILITY) |
78 | 103 |
79 return sorted_branches[0][0] | 104 return sorted_numbers[0][0] |
OLD | NEW |