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..16704e52794db6a040a8da2004ba63332639a57b |
--- /dev/null |
+++ b/chrome/common/extensions/docs/server2/branch_utility.py |
@@ -0,0 +1,55 @@ |
+# 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.url = 'http://omahaproxy.appspot.com/json' |
Aaron Boodman
2012/05/24 06:10:23
Nit: omaha_proxy_url ?
cduvall
2012/05/25 20:02:23
Done.
|
+ self.urlfetch = urlfetch |
+ |
+ def SetURL(self, url): |
+ self.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. |
Aaron Boodman
2012/05/24 06:10:23
Add a commment: This can throw in case of network
cduvall
2012/05/25 20:02:23
Done.
|
+ """ |
+ if channel_name == 'trunk': |
+ return 'trunk' |
+ |
+ fetch_data = self.urlfetch.fetch(self.url) |
+ |
+ 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()] |
Aaron Boodman
2012/05/24 06:10:23
cute
|
+ sorted_list.sort(key = lambda x: x[1]) |
+ sorted_list.reverse() |
+ |
+ branch_number = '' |
+ if len(sorted_list) > 0: |
Aaron Boodman
2012/05/24 06:10:23
I think you should throw earlier if you get zero r
cduvall
2012/05/25 20:02:23
Done.
|
+ branch_number, _ = sorted_list[0] |
+ |
+ return branch_number |