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