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..063cad522634bf8441a787c98c9dc2380e11d4be |
--- /dev/null |
+++ b/chrome/common/extensions/docs/server2/branch_utility.py |
@@ -0,0 +1,59 @@ |
+# 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 |
+ |
+from google.appengine.api import memcache |
+from google.appengine.api import urlfetch |
+ |
+DEFAULT_CACHE_TIME = 300 |
+ |
+class BranchUtility(object): |
+ """Utility class for dealing with different doc branches. |
+ """ |
+ |
+ def GetChannelNameFromURL(self, url): |
cduvall
2012/05/22 00:07:40
Can probably make this shorter, but works for now.
Aaron Boodman
2012/05/22 00:20:34
It should only count if it's the first url compone
cduvall
2012/05/22 01:23:55
Done.
|
+ if '/dev/' in url: |
+ return 'dev' |
+ if '/beta/' in url: |
+ return 'beta' |
+ if '/trunk/' in url: |
+ return 'trunk' |
+ return 'stable' |
+ |
+ def GetBranchNumberForChannelName(self, name): |
+ """Returns an empty string if the branch number cannot be found. |
+ """ |
+ if name == 'trunk': |
cduvall
2012/05/22 00:07:40
omahaproxy.appspot.com has no branch number for tr
Aaron Boodman
2012/05/22 00:20:34
There's no branch number for trunk because ... it'
cduvall
2012/05/22 01:23:55
Done.
|
+ return 'trunk' |
Aaron Boodman
2012/05/22 00:20:34
In this case I think you should throw an exception
cduvall
2012/05/22 01:23:55
I think its cleaner to handle trunk in here, since
|
+ result = memcache.get(name) |
Aaron Boodman
2012/05/22 00:20:34
We are going to be using memcache for all kinds of
cduvall
2012/05/22 01:23:55
Done.
|
+ if result is not None: |
+ return result |
+ logging.info('Branch number cache miss: ' + name) |
+ |
+ result = urlfetch.fetch( |
+ url="http://omahaproxy.appspot.com/json", |
+ method=urlfetch.GET) |
+ |
+ # Throw exception or something instead? |
+ if result.status_code != 200: |
Aaron Boodman
2012/05/22 00:20:34
Yeah, exception makes sense.
cduvall
2012/05/22 01:23:55
Done.
|
+ return '' |
+ |
+ version_json = json.loads(result.content) |
+ branch_number = None |
+ for entry in version_json: |
+ if entry['os'] != 'cf': |
cduvall
2012/05/22 00:07:40
Not sure what os: cf was, but assumed it was the d
Aaron Boodman
2012/05/22 00:20:34
It stands for 'chromeframe' - you can search what
cduvall
2012/05/22 01:23:55
Done.
|
+ continue |
+ for version in entry['versions']: |
Aaron Boodman
2012/05/22 00:20:34
Can you look at what version each platform has and
cduvall
2012/05/22 01:23:55
Done.
|
+ if version['channel'] == name: |
+ branch_number = version['true_branch'] |
+ |
+ if branch_number == None: |
+ return '' |
+ |
+ if not memcache.add(name, branch_number, DEFAULT_CACHE_TIME): |
+ logging.error('Memcache set failed.') |
+ return branch_number |