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 json | 5 import json |
6 | 6 |
7 import appengine_memcache as memcache | 7 import appengine_memcache as memcache |
8 import operator | 8 import operator |
9 | 9 |
10 class BranchUtility(object): | 10 class BranchUtility(object): |
11 def __init__(self, base_path, default_branch, fetcher, memcache): | 11 def __init__(self, base_path, default_branch, fetcher, memcache): |
12 self._base_path = base_path | 12 self._base_path = base_path |
13 self._default_branch = default_branch | 13 self._default_branch = default_branch |
14 self._fetcher = fetcher | 14 self._fetcher = fetcher |
15 self._memcache = memcache | 15 self._memcache = memcache |
16 | 16 |
17 def SplitChannelNameFromPath(self, path): | 17 def SplitChannelNameFromPath(self, path): |
18 prefix = '' | |
19 if path.startswith('extensions/'): | |
20 prefix = 'extensions/' | |
21 path = path[len('extensions/'):] | |
22 elif path.startswith('apps/'): | |
23 prefix = 'apps/' | |
24 path = path[len('apps/'):] | |
25 try: | 18 try: |
26 first, second = path.split('/', 1) | 19 first, second = path.split('/', 1) |
27 except ValueError: | 20 except ValueError: |
28 first = path | 21 first = path |
29 second = '' | 22 second = '' |
30 if first in ['trunk', 'dev', 'beta', 'stable']: | 23 if first in ['trunk', 'dev', 'beta', 'stable']: |
31 return (first, prefix + second) | 24 return (first, second) |
32 else: | 25 else: |
33 return (self._default_branch, prefix + path) | 26 return (self._default_branch, path) |
34 | 27 |
35 def GetBranchNumberForChannelName(self, channel_name): | 28 def GetBranchNumberForChannelName(self, channel_name): |
36 """Returns an empty string if the branch number cannot be found. | 29 """Returns an empty string if the branch number cannot be found. |
37 Throws exception on network errors. | 30 Throws exception on network errors. |
38 """ | 31 """ |
39 if channel_name in ['trunk', 'local']: | 32 if channel_name in ['trunk', 'local']: |
40 return channel_name | 33 return channel_name |
41 | 34 |
42 branch_number = self._memcache.Get(channel_name + '.' + self._base_path, | 35 branch_number = self._memcache.Get(channel_name + '.' + self._base_path, |
43 memcache.MEMCACHE_BRANCH_UTILITY) | 36 memcache.MEMCACHE_BRANCH_UTILITY) |
(...skipping 18 matching lines...) Expand all Loading... |
62 None, | 55 None, |
63 operator.itemgetter(1), | 56 operator.itemgetter(1), |
64 True) | 57 True) |
65 # Cache for 24 hours. | 58 # Cache for 24 hours. |
66 self._memcache.Set(channel_name + '.' + self._base_path, | 59 self._memcache.Set(channel_name + '.' + self._base_path, |
67 sorted_branches[0][0], | 60 sorted_branches[0][0], |
68 memcache.MEMCACHE_BRANCH_UTILITY, | 61 memcache.MEMCACHE_BRANCH_UTILITY, |
69 86400) | 62 86400) |
70 | 63 |
71 return sorted_branches[0][0] | 64 return sorted_branches[0][0] |
OLD | NEW |