| Index: chrome/common/extensions/docs/server2/branch_utility.py
|
| diff --git a/chrome/common/extensions/docs/server2/branch_utility.py b/chrome/common/extensions/docs/server2/branch_utility.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a395fc45c8b34c98099fa37f170799e28e9e2fc3
|
| --- /dev/null
|
| +++ b/chrome/common/extensions/docs/server2/branch_utility.py
|
| @@ -0,0 +1,56 @@
|
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +import logging
|
| +import re
|
| +import json
|
| +
|
| +class BranchUtility(object):
|
| + """Utility class for dealing with different doc branches.
|
| + """
|
| + def __init__(self, urlfetch):
|
| + self.omaha_proxy_url = 'http://omahaproxy.appspot.com/json'
|
| + self.urlfetch = urlfetch
|
| +
|
| + def SetURL(self, url):
|
| + self.omaha_proxy_url = url
|
| +
|
| + def GetChannelNameFromPath(self, path):
|
| + first_part = path.split('/')[0]
|
| + if first_part in ['trunk', 'dev', 'beta', 'stable']:
|
| + return first_part
|
| + else:
|
| + return 'stable'
|
| +
|
| + def GetBranchNumberForChannelName(self, channel_name):
|
| + """Returns an empty string if the branch number cannot be found.
|
| + Throws exception on network errors.
|
| + """
|
| + if channel_name == 'trunk':
|
| + return 'trunk'
|
| +
|
| + fetch_data = self.urlfetch.fetch(self.omaha_proxy_url)
|
| + if fetch_data.content == '':
|
| + raise Exception('Fetch returned zero results.')
|
| +
|
| + version_json = json.loads(fetch_data.content)
|
| + branch_numbers = {}
|
| + for entry in version_json:
|
| + if entry['os'] not in ['win', 'linux', 'mac', 'cros']:
|
| + continue
|
| + for version in entry['versions']:
|
| + if version['channel'] != channel_name:
|
| + continue
|
| + if version['true_branch'] not in branch_numbers:
|
| + branch_numbers[version['true_branch']] = 0
|
| + else:
|
| + branch_numbers[version['true_branch']] += 1
|
| +
|
| + sorted_list = [x for x in branch_numbers.iteritems()]
|
| + sorted_list.sort(key = lambda x: x[1])
|
| + sorted_list.reverse()
|
| +
|
| + branch_number, _ = sorted_list[0]
|
| +
|
| + return branch_number
|
|
|