| 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 GetAllBranchNumbers(self): |
| 18 return [self.GetBranchNumberForChannelName(branch) |
| 19 for branch in ['dev', 'beta', 'stable', 'trunk', 'local']] |
| 20 |
| 17 def SplitChannelNameFromPath(self, path): | 21 def SplitChannelNameFromPath(self, path): |
| 18 try: | 22 try: |
| 19 first, second = path.split('/', 1) | 23 first, second = path.split('/', 1) |
| 20 except ValueError: | 24 except ValueError: |
| 21 first = path | 25 first = path |
| 22 second = '' | 26 second = '' |
| 23 if first in ['trunk', 'dev', 'beta', 'stable']: | 27 if first in ['trunk', 'dev', 'beta', 'stable']: |
| 24 return (first, second) | 28 return (first, second) |
| 25 else: | 29 else: |
| 26 return (self._default_branch, path) | 30 return (self._default_branch, path) |
| 27 | 31 |
| 28 def GetBranchNumberForChannelName(self, channel_name): | 32 def GetBranchNumberForChannelName(self, channel_name): |
| 29 """Returns an empty string if the branch number cannot be found. | 33 """Returns the branch number for a channel name. If the |channel_name| is |
| 30 Throws exception on network errors. | 34 'trunk' or 'local', then |channel_name| will be returned unchanged. These |
| 35 are returned unchanged because 'trunk' has a separate URL from the other |
| 36 branches and should be handled differently. 'local' is also a special branch |
| 37 for development that should be handled differently. |
| 31 """ | 38 """ |
| 32 if channel_name in ['trunk', 'local']: | 39 if channel_name in ['trunk', 'local']: |
| 33 return channel_name | 40 return channel_name |
| 34 | 41 |
| 35 branch_number = self._memcache.Get(channel_name + '.' + self._base_path, | 42 branch_number = self._memcache.Get(channel_name + '.' + self._base_path, |
| 36 memcache.MEMCACHE_BRANCH_UTILITY) | 43 memcache.MEMCACHE_BRANCH_UTILITY) |
| 37 if branch_number is not None: | 44 if branch_number is not None: |
| 38 return branch_number | 45 return branch_number |
| 39 | 46 |
| 40 fetch_data = self._fetcher.Fetch(self._base_path).content | 47 fetch_data = self._fetcher.Fetch(self._base_path).content |
| (...skipping 11 matching lines...) Expand all Loading... |
| 52 branch_numbers[version['true_branch']] += 1 | 59 branch_numbers[version['true_branch']] += 1 |
| 53 | 60 |
| 54 sorted_branches = sorted(branch_numbers.iteritems(), | 61 sorted_branches = sorted(branch_numbers.iteritems(), |
| 55 None, | 62 None, |
| 56 operator.itemgetter(1), | 63 operator.itemgetter(1), |
| 57 True) | 64 True) |
| 58 # Cache for 24 hours. | 65 # Cache for 24 hours. |
| 59 self._memcache.Set(channel_name + '.' + self._base_path, | 66 self._memcache.Set(channel_name + '.' + self._base_path, |
| 60 sorted_branches[0][0], | 67 sorted_branches[0][0], |
| 61 memcache.MEMCACHE_BRANCH_UTILITY, | 68 memcache.MEMCACHE_BRANCH_UTILITY, |
| 62 86400) | 69 time=86400) |
| 63 | 70 |
| 64 return sorted_branches[0][0] | 71 return sorted_branches[0][0] |
| OLD | NEW |