| 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 object_store |
| 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, object_store): |
| 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._object_store = object_store |
| 16 | 16 |
| 17 def GetAllBranchNumbers(self): | 17 def GetAllBranchNumbers(self): |
| 18 return [self.GetBranchNumberForChannelName(branch) | 18 return [self.GetBranchNumberForChannelName(branch) |
| 19 for branch in ['dev', 'beta', 'stable', 'trunk', 'local']] | 19 for branch in ['dev', 'beta', 'stable', 'trunk', 'local']] |
| 20 | 20 |
| 21 def SplitChannelNameFromPath(self, path): | 21 def SplitChannelNameFromPath(self, path): |
| 22 try: | 22 try: |
| 23 first, second = path.split('/', 1) | 23 first, second = path.split('/', 1) |
| 24 except ValueError: | 24 except ValueError: |
| 25 first = path | 25 first = path |
| 26 second = '' | 26 second = '' |
| 27 if first in ['trunk', 'dev', 'beta', 'stable']: | 27 if first in ['trunk', 'dev', 'beta', 'stable']: |
| 28 return (first, second) | 28 return (first, second) |
| 29 else: | 29 else: |
| 30 return (self._default_branch, path) | 30 return (self._default_branch, path) |
| 31 | 31 |
| 32 def GetBranchNumberForChannelName(self, channel_name): | 32 def GetBranchNumberForChannelName(self, channel_name): |
| 33 """Returns the branch number for a channel name. If the |channel_name| is | 33 """Returns the branch number for a channel name. If the |channel_name| is |
| 34 'trunk' or 'local', then |channel_name| will be returned unchanged. These | 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 | 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 | 36 branches and should be handled differently. 'local' is also a special branch |
| 37 for development that should be handled differently. | 37 for development that should be handled differently. |
| 38 """ | 38 """ |
| 39 if channel_name in ['trunk', 'local']: | 39 if channel_name in ['trunk', 'local']: |
| 40 return channel_name | 40 return channel_name |
| 41 | 41 |
| 42 branch_number = self._memcache.Get(channel_name + '.' + self._base_path, | 42 branch_number = self._object_store.Get(channel_name + '.' + self._base_path, |
| 43 memcache.MEMCACHE_BRANCH_UTILITY) | 43 object_store.BRANCH_UTILITY, |
| 44 time=86400).Get() |
| 44 if branch_number is not None: | 45 if branch_number is not None: |
| 45 return branch_number | 46 return branch_number |
| 46 | 47 |
| 47 fetch_data = self._fetcher.Fetch(self._base_path).content | 48 fetch_data = self._fetcher.Fetch(self._base_path).content |
| 48 version_json = json.loads(fetch_data) | 49 version_json = json.loads(fetch_data) |
| 49 branch_numbers = {} | 50 branch_numbers = {} |
| 50 for entry in version_json: | 51 for entry in version_json: |
| 51 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: | 52 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: |
| 52 continue | 53 continue |
| 53 for version in entry['versions']: | 54 for version in entry['versions']: |
| 54 if version['channel'] != channel_name: | 55 if version['channel'] != channel_name: |
| 55 continue | 56 continue |
| 56 if version['true_branch'] not in branch_numbers: | 57 if version['true_branch'] not in branch_numbers: |
| 57 branch_numbers[version['true_branch']] = 0 | 58 branch_numbers[version['true_branch']] = 0 |
| 58 else: | 59 else: |
| 59 branch_numbers[version['true_branch']] += 1 | 60 branch_numbers[version['true_branch']] += 1 |
| 60 | 61 |
| 61 sorted_branches = sorted(branch_numbers.iteritems(), | 62 sorted_branches = sorted(branch_numbers.iteritems(), |
| 62 None, | 63 None, |
| 63 operator.itemgetter(1), | 64 operator.itemgetter(1), |
| 64 True) | 65 True) |
| 65 # Cache for 24 hours. | 66 # Cache for 24 hours. |
| 66 self._memcache.Set(channel_name + '.' + self._base_path, | 67 self._object_store.Set(channel_name + '.' + self._base_path, |
| 67 sorted_branches[0][0], | 68 sorted_branches[0][0], |
| 68 memcache.MEMCACHE_BRANCH_UTILITY, | 69 object_store.BRANCH_UTILITY, |
| 69 time=86400) | 70 time=86400) |
| 70 | 71 |
| 71 return sorted_branches[0][0] | 72 return sorted_branches[0][0] |
| OLD | NEW |