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 logging | 5 import logging |
6 import re | 6 import re |
7 import json | 7 import json |
8 | 8 |
9 class BranchUtility(object): | 9 class BranchUtility(object): |
10 """Utility class for dealing with different doc branches. | 10 """Utility class for dealing with different doc branches. |
11 """ | 11 """ |
12 def __init__(self, urlfetch): | 12 def __init__(self, urlfetch): |
13 self.omaha_proxy_url = 'http://omahaproxy.appspot.com/json' | 13 self.omaha_proxy_url = 'http://omahaproxy.appspot.com/json' |
14 self.urlfetch = urlfetch | 14 self.urlfetch = urlfetch |
15 | 15 |
16 def SetURL(self, url): | 16 def SetURL(self, url): |
17 self.omaha_proxy_url = url | 17 self.omaha_proxy_url = url |
18 | 18 |
19 def GetChannelNameFromPath(self, path): | 19 def GetChannelNameFromPath(self, path): |
20 first_part = path.split('/')[0] | 20 first_part = path.split('/')[0] |
21 if first_part in ['trunk', 'dev', 'beta', 'stable']: | 21 if first_part in ['trunk', 'dev', 'beta', 'stable', 'local']: |
22 return first_part | 22 return first_part |
23 else: | 23 else: |
24 return 'stable' | 24 return 'stable' |
25 | 25 |
26 def GetBranchNumberForChannelName(self, channel_name): | 26 def GetBranchNumberForChannelName(self, channel_name): |
27 """Returns an empty string if the branch number cannot be found. | 27 """Returns an empty string if the branch number cannot be found. |
28 Throws exception on network errors. | 28 Throws exception on network errors. |
29 """ | 29 """ |
30 if channel_name == 'trunk': | 30 if channel_name == 'trunk' or channel_name == 'local': |
31 return 'trunk' | 31 return channel_name |
32 | 32 |
33 fetch_data = self.urlfetch.fetch(self.omaha_proxy_url) | 33 fetch_data = self.urlfetch.fetch(self.omaha_proxy_url) |
34 if fetch_data.content == '': | 34 if fetch_data.content == '': |
35 raise Exception('Fetch returned zero results.') | 35 raise Exception('Fetch returned zero results.') |
36 | 36 |
37 version_json = json.loads(fetch_data.content) | 37 version_json = json.loads(fetch_data.content) |
38 branch_numbers = {} | 38 branch_numbers = {} |
39 for entry in version_json: | 39 for entry in version_json: |
40 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: | 40 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: |
41 continue | 41 continue |
42 for version in entry['versions']: | 42 for version in entry['versions']: |
43 if version['channel'] != channel_name: | 43 if version['channel'] != channel_name: |
44 continue | 44 continue |
45 if version['true_branch'] not in branch_numbers: | 45 if version['true_branch'] not in branch_numbers: |
46 branch_numbers[version['true_branch']] = 0 | 46 branch_numbers[version['true_branch']] = 0 |
47 else: | 47 else: |
48 branch_numbers[version['true_branch']] += 1 | 48 branch_numbers[version['true_branch']] += 1 |
49 | 49 |
50 sorted_list = [x for x in branch_numbers.iteritems()] | 50 sorted_list = [x for x in branch_numbers.iteritems()] |
51 sorted_list.sort(key = lambda x: x[1]) | 51 sorted_list.sort(key = lambda x: x[1]) |
52 sorted_list.reverse() | 52 sorted_list.reverse() |
53 | 53 |
54 branch_number, _ = sorted_list[0] | 54 branch_number, _ = sorted_list[0] |
55 | 55 |
56 return branch_number | 56 return branch_number |
OLD | NEW |