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 appengine_url_fetcher import AppEngineUrlFetcher | 9 from appengine_url_fetcher import AppEngineUrlFetcher |
10 import url_constants | 10 import url_constants |
11 | 11 |
12 class BranchUtility(object): | 12 class BranchUtility(object): |
13 def __init__(self, fetch_url, fetcher, object_store_creator): | 13 def __init__(self, fetch_url, history_url, fetcher, object_store_creator): |
14 self._fetch_url = fetch_url | |
15 self._fetcher = fetcher | 14 self._fetcher = fetcher |
16 # BranchUtility is obviously cross-channel, so set the channel to None. | 15 # BranchUtility is obviously cross-channel, so set the channel to None. |
17 self._object_store = object_store_creator.Create(BranchUtility, | 16 self._branch_object_store = object_store_creator.Create(BranchUtility, |
18 channel=None) | 17 category='branch') |
18 self._version_object_store = object_store_creator.Create(BranchUtility, | |
19 category='version') | |
20 self._fetch_result = self._fetcher.FetchAsync(fetch_url) | |
21 self._history_result = self._fetcher.FetchAsync(history_url) | |
epeterson
2013/06/02 00:25:50
Save a future with the fetch result, and call .Get
| |
22 | |
23 @staticmethod | |
24 def GetAllChannelNames(): | |
25 return ('stable', 'beta', 'dev', 'trunk') | |
26 | |
27 @staticmethod | |
28 def NewestChannel(channels): | |
29 for channel in reversed(BranchUtility.GetAllChannelNames()): | |
30 if channel in channels: | |
31 return channel | |
19 | 32 |
20 @staticmethod | 33 @staticmethod |
21 def Create(object_store_creator): | 34 def Create(object_store_creator): |
22 return BranchUtility(url_constants.OMAHA_PROXY_URL, | 35 return BranchUtility(url_constants.OMAHA_PROXY_URL, |
36 url_constants.OMAHA_DEV_HISTORY, | |
23 AppEngineUrlFetcher(), | 37 AppEngineUrlFetcher(), |
24 object_store_creator) | 38 object_store_creator) |
25 | 39 |
26 @staticmethod | 40 @staticmethod |
27 def GetAllChannelNames(): | |
28 return ['stable', 'beta', 'dev', 'trunk'] | |
29 | |
30 @staticmethod | |
31 def SplitChannelNameFromPath(path): | 41 def SplitChannelNameFromPath(path): |
32 """Splits the channel name out of |path|, returning the tuple | 42 """Splits the channel name out of |path|, returning the tuple |
33 (channel_name, real_path). If the channel cannot be determined then returns | 43 (channel_name, real_path). If the channel cannot be determined then returns |
34 (None, path). | 44 (None, path). |
35 """ | 45 """ |
36 if '/' in path: | 46 if '/' in path: |
37 first, second = path.split('/', 1) | 47 first, second = path.split('/', 1) |
38 else: | 48 else: |
39 first, second = (path, '') | 49 first, second = (path, '') |
40 if first in ['trunk', 'dev', 'beta', 'stable']: | 50 if first in BranchUtility.GetAllChannelNames(): |
41 return (first, second) | 51 return (first, second) |
42 return (None, path) | 52 return (None, path) |
43 | 53 |
44 def GetBranchForChannel(self, channel_name): | 54 def GetAllBranchNumbers(self): |
45 """Returns the branch number for a channel name. | 55 return [(channel, self.GetChannelInfo(channel).branch) |
56 for channel in BranchUtility.GetAllChannelNames()] | |
57 | |
58 def GetAllVersionNumbers(self): | |
59 return [self.GetChannelInfo(channel).version | |
60 for channel in BranchUtility.GetAllChannelNames()] | |
61 | |
62 def GetAllChannelInfo(self): | |
63 return [self.GetChannelInfo(channel) | |
64 for channel in BranchUtility.GetAllChannelNames()] | |
65 | |
66 | |
67 def GetChannelInfo(self, channel): | |
68 class ChannelInfo(object): | |
69 def __init__(self, channel, branch, version): | |
70 self.channel = channel | |
71 self.branch = branch | |
72 self.version = version | |
73 | |
74 return ChannelInfo(channel, | |
75 self._ExtractFromVersionJson(channel, 'branch'), | |
76 self._ExtractFromVersionJson(channel, 'version')) | |
77 | |
78 def _ExtractFromVersionJson(self, channel_name, data_type): | |
79 """Returns the branch or version number for a channel name. | |
46 """ | 80 """ |
47 if channel_name == 'trunk': | 81 if channel_name == 'trunk': |
48 return 'trunk' | 82 return 'trunk' |
49 | 83 |
50 branch_number = self._object_store.Get(channel_name).Get() | 84 if data_type == 'branch': |
51 if branch_number is not None: | 85 data = self._branch_object_store.Get(channel_name).Get() |
52 return branch_number | 86 elif data_type == 'version': |
87 data = self._version_object_store.Get(channel_name).Get() | |
epeterson
2013/06/02 00:25:50
Using two separate object_store with different cat
| |
88 | |
89 if data is not None: | |
90 return data | |
53 | 91 |
54 try: | 92 try: |
55 version_json = json.loads(self._fetcher.Fetch(self._fetch_url).content) | 93 version_json = json.loads(self._fetch_result.Get().content) |
56 except Exception as e: | 94 except Exception as e: |
57 # This can happen if omahaproxy is misbehaving, which we've seen before. | 95 # This can happen if omahaproxy is misbehaving, which we've seen before. |
58 # Quick hack fix: just serve from trunk until it's fixed. | 96 # Quick hack fix: just serve from trunk until it's fixed. |
59 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' | 97 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' |
60 'Falling back to "trunk".' % e) | 98 'Falling back to "trunk".' % e) |
61 return 'trunk' | 99 return 'trunk' |
62 | 100 |
63 branch_numbers = {} | 101 numbers = {} |
64 for entry in version_json: | 102 for entry in version_json: |
65 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: | 103 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: |
66 continue | 104 continue |
67 for version in entry['versions']: | 105 for version in entry['versions']: |
68 if version['channel'] != channel_name: | 106 if version['channel'] != channel_name: |
69 continue | 107 continue |
70 branch = version['version'].split('.')[2] | 108 if data_type == 'branch': |
71 if branch not in branch_numbers: | 109 number = version['version'].split('.')[2] |
72 branch_numbers[branch] = 0 | 110 elif data_type == 'version': |
111 number = version['version'].split('.')[0] | |
112 if number not in numbers: | |
113 numbers[number] = 0 | |
73 else: | 114 else: |
74 branch_numbers[branch] += 1 | 115 numbers[number] += 1 |
75 | 116 |
76 sorted_branches = sorted(branch_numbers.iteritems(), | 117 sorted_numbers = sorted(numbers.iteritems(), |
77 None, | 118 None, |
78 operator.itemgetter(1), | 119 operator.itemgetter(1), |
79 True) | 120 True) |
80 self._object_store.Set(channel_name, sorted_branches[0][0]) | 121 if data_type == 'branch': |
122 self._branch_object_store.Set(channel_name, int(sorted_numbers[0][0])) | |
123 elif data_type == 'version': | |
124 self._version_object_store.Set(channel_name, int(sorted_numbers[0][0])) | |
81 | 125 |
82 return sorted_branches[0][0] | 126 return int(sorted_numbers[0][0]) |
127 | |
128 def GetBranchNumberForVersion(self, version): | |
epeterson
2013/06/02 00:25:50
ChromeVersionUtility only had two methods left, an
| |
129 """Returns the most recent branch number for a given chrome version number | |
130 using data stored on omahaproxy (see url_constants). | |
131 """ | |
132 if version == 'trunk': | |
133 return 'trunk' | |
134 | |
135 branch = self._branch_object_store.Get(version).Get() | |
136 if branch is not None: | |
137 return branch | |
138 version_json = json.loads(self._history_result.Get().content) | |
139 for entry in version_json['events']: | |
140 # Here, entry['title'] looks like: 'title - version#.#.branch#.#' | |
141 version_title = entry['title'].split(' - ')[1].split('.') | |
142 if version_title[0] == str(version): | |
143 self._branch_object_store.Set(str(version), version_title[2]) | |
144 return int(version_title[2]) | |
145 raise ValueError( | |
146 'A branch number for the given version could not be determined.') | |
147 | |
148 def GetLatestVersionNumber(self): | |
149 """Returns the most recent version number found using data stored on | |
150 omahaproxy. | |
151 """ | |
152 latest_version = self._version_object_store.Get('latest').Get() | |
epeterson
2013/06/02 00:25:50
Using an object_store here now, this was brought u
| |
153 if latest_version is not None: | |
154 return latest_version | |
155 | |
156 version_json = json.loads(self._history_result.Get().content) | |
157 latest_version = 0 | |
158 for entry in version_json['events']: | |
159 version_title = entry['title'].split(' - ')[1].split('.') | |
160 version = int(version_title[0]) | |
161 if version > latest_version: | |
162 latest_version = version | |
163 | |
164 self._version_object_store.Set('latest', latest_version) | |
165 return latest_version | |
OLD | NEW |