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, fetcher, object_store_creator): |
14 self._fetch_url = fetch_url | 14 self._fetch_url = fetch_url |
15 self._fetcher = fetcher | 15 self._fetcher = fetcher |
16 # BranchUtility is obviously cross-channel, so set the channel to None. | 16 # BranchUtility is obviously cross-channel, so set the channel to None. |
17 self._object_store = object_store_creator.Create(BranchUtility, | 17 self._object_store = object_store_creator.Create(BranchUtility, |
18 channel=None) | 18 channel=None) |
19 | 19 |
20 @staticmethod | 20 @staticmethod |
21 def GetAllChannelNames(): | |
22 return ('stable', 'beta', 'dev', 'trunk') | |
23 | |
24 def GetAllBranchNumbers(self): | |
25 return [(branch, self.GetChannelInfoForChannelName(branch).branch) | |
not at google - send to devlin
2013/05/15 08:15:00
This GetChannelInfoForChannelName doesn't actually
epeterson
2013/06/02 00:25:50
Done.
| |
26 for branch in BranchUtility.GetAllChannelNames()] | |
not at google - send to devlin
2013/05/15 08:15:00
channel in
epeterson
2013/06/02 00:25:50
Done.
| |
27 | |
28 def GetAllVersionNumbers(self): | |
29 return [self.GetChannelInfoForChannelName(branch).version | |
30 for branch in self.GetAllChannelNames()] | |
not at google - send to devlin
2013/05/15 08:15:00
channel in
epeterson
2013/06/02 00:25:50
Done.
| |
31 | |
32 def GetChannelInfoForAllChannels(self): | |
not at google - send to devlin
2013/05/15 08:15:00
can be just GetAllChannelInfo, shorter.
epeterson
2013/06/02 00:25:50
Done.
| |
33 return [self.GetChannelInfoForChannelName(branch) | |
34 for branch in self.GetAllChannelNames()] | |
not at google - send to devlin
2013/05/15 08:15:00
channel in
epeterson
2013/06/02 00:25:50
Done.
| |
35 | |
36 @staticmethod | |
37 def NewestChannel(channels): | |
38 for channel in reversed(BranchUtility.GetAllChannelNames()): | |
39 if channel in channels: | |
40 return channel | |
41 | |
42 @staticmethod | |
21 def Create(object_store_creator): | 43 def Create(object_store_creator): |
22 return BranchUtility(url_constants.OMAHA_PROXY_URL, | 44 return BranchUtility(url_constants.OMAHA_PROXY_URL, |
23 AppEngineUrlFetcher(), | 45 AppEngineUrlFetcher(), |
24 object_store_creator) | 46 object_store_creator) |
25 | 47 |
26 @staticmethod | 48 @staticmethod |
27 def GetAllChannelNames(): | |
28 return ['stable', 'beta', 'dev', 'trunk'] | |
29 | |
30 @staticmethod | |
31 def SplitChannelNameFromPath(path): | 49 def SplitChannelNameFromPath(path): |
32 """Splits the channel name out of |path|, returning the tuple | 50 """Splits the channel name out of |path|, returning the tuple |
33 (channel_name, real_path). If the channel cannot be determined then returns | 51 (channel_name, real_path). If the channel cannot be determined then returns |
34 (None, path). | 52 (None, path). |
35 """ | 53 """ |
36 if '/' in path: | 54 if '/' in path: |
37 first, second = path.split('/', 1) | 55 first, second = path.split('/', 1) |
38 else: | 56 else: |
39 first, second = (path, '') | 57 first, second = (path, '') |
40 if first in ['trunk', 'dev', 'beta', 'stable']: | 58 if first in BranchUtility.GetAllChannelNames(): |
41 return (first, second) | 59 return (first, second) |
42 return (None, path) | 60 return (None, path) |
43 | 61 |
44 def GetBranchForChannel(self, channel_name): | 62 def GetChannelInfo(self, channel): |
45 """Returns the branch number for a channel name. | 63 class ChannelInfo(object): |
64 def __init__(self, name, branch, version): | |
65 self.name = name | |
66 self.branch = branch | |
67 self.version = version | |
68 | |
69 return ChannelInfo(channel, | |
70 self._ExtractFromVersionJson(channel, 'branch'), | |
71 self._ExtractFromVersionJson(channel, 'version')) | |
72 | |
73 def _ExtractFromVersionJson(self, channel_name, data_type): | |
74 """Returns the branch or version number for a channel name. | |
46 """ | 75 """ |
47 if channel_name == 'trunk': | 76 if channel_name == 'trunk': |
48 return 'trunk' | 77 return 'trunk' |
49 | 78 |
50 branch_number = self._object_store.Get(channel_name).Get() | 79 data = self._object_store.Get('%s.%s' % (channel_name, data_type)).Get() |
51 if branch_number is not None: | 80 if data is not None: |
52 return branch_number | 81 return data |
53 | 82 |
54 try: | 83 try: |
55 version_json = json.loads(self._fetcher.Fetch(self._fetch_url).content) | 84 version_json = json.loads(self._fetcher.Fetch(self._fetch_url).content) |
56 except Exception as e: | 85 except Exception as e: |
57 # This can happen if omahaproxy is misbehaving, which we've seen before. | 86 # This can happen if omahaproxy is misbehaving, which we've seen before. |
58 # Quick hack fix: just serve from trunk until it's fixed. | 87 # Quick hack fix: just serve from trunk until it's fixed. |
59 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' | 88 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' |
60 'Falling back to "trunk".' % e) | 89 'Falling back to "trunk".' % e) |
61 return 'trunk' | 90 return 'trunk' |
62 | 91 |
63 branch_numbers = {} | 92 numbers = {} |
64 for entry in version_json: | 93 for entry in version_json: |
65 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: | 94 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: |
66 continue | 95 continue |
67 for version in entry['versions']: | 96 for version in entry['versions']: |
68 if version['channel'] != channel_name: | 97 if version['channel'] != channel_name: |
69 continue | 98 continue |
70 branch = version['version'].split('.')[2] | 99 if data_type == 'branch': |
71 if branch not in branch_numbers: | 100 number = version['version'].split('.')[2] |
72 branch_numbers[branch] = 0 | 101 elif data_type == 'version': |
102 number = version['version'].split('.')[0] | |
103 if number not in numbers: | |
104 numbers[number] = 0 | |
73 else: | 105 else: |
74 branch_numbers[branch] += 1 | 106 numbers[number] += 1 |
75 | 107 |
76 sorted_branches = sorted(branch_numbers.iteritems(), | 108 sorted_numbers = sorted(numbers.iteritems(), |
77 None, | 109 None, |
78 operator.itemgetter(1), | 110 operator.itemgetter(1), |
79 True) | 111 True) |
80 self._object_store.Set(channel_name, sorted_branches[0][0]) | 112 self._object_store.Set('%s.%s' % (channel_name, data_type), |
113 sorted_numbers[0][0]) | |
81 | 114 |
82 return sorted_branches[0][0] | 115 return sorted_numbers[0][0] |
OLD | NEW |