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