OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import logging |
| 6 import re |
| 7 import json |
| 8 |
| 9 class BranchUtility(object): |
| 10 """Utility class for dealing with different doc branches. |
| 11 """ |
| 12 def __init__(self, urlfetch): |
| 13 self.omaha_proxy_url = 'http://omahaproxy.appspot.com/json' |
| 14 self.urlfetch = urlfetch |
| 15 |
| 16 def SetURL(self, url): |
| 17 self.omaha_proxy_url = url |
| 18 |
| 19 def GetChannelNameFromPath(self, path): |
| 20 first_part = path.split('/')[0] |
| 21 if first_part in ['trunk', 'dev', 'beta', 'stable']: |
| 22 return first_part |
| 23 else: |
| 24 return 'stable' |
| 25 |
| 26 def GetBranchNumberForChannelName(self, channel_name): |
| 27 """Returns an empty string if the branch number cannot be found. |
| 28 Throws exception on network errors. |
| 29 """ |
| 30 if channel_name == 'trunk': |
| 31 return 'trunk' |
| 32 |
| 33 fetch_data = self.urlfetch.fetch(self.omaha_proxy_url) |
| 34 if fetch_data.content == '': |
| 35 raise Exception('Fetch returned zero results.') |
| 36 |
| 37 version_json = json.loads(fetch_data.content) |
| 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 |