Chromium Code Reviews| 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 |
| index 0dd43d102637f061a065074980889ebebb71a404..c5bf0a630da30c696dd3099e5224a52ca76cf0f7 100644 |
| --- a/chrome/common/extensions/docs/server2/branch_utility.py |
| +++ b/chrome/common/extensions/docs/server2/branch_utility.py |
| @@ -10,24 +10,34 @@ from appengine_url_fetcher import AppEngineUrlFetcher |
| import url_constants |
| class BranchUtility(object): |
| - def __init__(self, fetch_url, fetcher, object_store_creator): |
| - self._fetch_url = fetch_url |
| + def __init__(self, fetch_url, history_url, fetcher, object_store_creator): |
| self._fetcher = fetcher |
| # BranchUtility is obviously cross-channel, so set the channel to None. |
| - self._object_store = object_store_creator.Create(BranchUtility, |
| - channel=None) |
| + self._branch_object_store = object_store_creator.Create(BranchUtility, |
| + category='branch') |
| + self._version_object_store = object_store_creator.Create(BranchUtility, |
| + category='version') |
| + self._fetch_result = self._fetcher.FetchAsync(fetch_url) |
| + self._history_result = self._fetcher.FetchAsync(history_url) |
|
epeterson
2013/06/02 00:25:50
Save a future with the fetch result, and call .Get
|
| + |
| + @staticmethod |
| + def GetAllChannelNames(): |
| + return ('stable', 'beta', 'dev', 'trunk') |
| + |
| + @staticmethod |
| + def NewestChannel(channels): |
| + for channel in reversed(BranchUtility.GetAllChannelNames()): |
| + if channel in channels: |
| + return channel |
| @staticmethod |
| def Create(object_store_creator): |
| return BranchUtility(url_constants.OMAHA_PROXY_URL, |
| + url_constants.OMAHA_DEV_HISTORY, |
| AppEngineUrlFetcher(), |
| object_store_creator) |
| @staticmethod |
| - def GetAllChannelNames(): |
| - return ['stable', 'beta', 'dev', 'trunk'] |
| - |
| - @staticmethod |
| def SplitChannelNameFromPath(path): |
| """Splits the channel name out of |path|, returning the tuple |
| (channel_name, real_path). If the channel cannot be determined then returns |
| @@ -37,22 +47,50 @@ class BranchUtility(object): |
| first, second = path.split('/', 1) |
| else: |
| first, second = (path, '') |
| - if first in ['trunk', 'dev', 'beta', 'stable']: |
| + if first in BranchUtility.GetAllChannelNames(): |
| return (first, second) |
| return (None, path) |
| - def GetBranchForChannel(self, channel_name): |
| - """Returns the branch number for a channel name. |
| + def GetAllBranchNumbers(self): |
| + return [(channel, self.GetChannelInfo(channel).branch) |
| + for channel in BranchUtility.GetAllChannelNames()] |
| + |
| + def GetAllVersionNumbers(self): |
| + return [self.GetChannelInfo(channel).version |
| + for channel in BranchUtility.GetAllChannelNames()] |
| + |
| + def GetAllChannelInfo(self): |
| + return [self.GetChannelInfo(channel) |
| + for channel in BranchUtility.GetAllChannelNames()] |
| + |
| + |
| + def GetChannelInfo(self, channel): |
| + class ChannelInfo(object): |
| + def __init__(self, channel, branch, version): |
| + self.channel = channel |
| + self.branch = branch |
| + self.version = version |
| + |
| + return ChannelInfo(channel, |
| + self._ExtractFromVersionJson(channel, 'branch'), |
| + self._ExtractFromVersionJson(channel, 'version')) |
| + |
| + def _ExtractFromVersionJson(self, channel_name, data_type): |
| + """Returns the branch or version number for a channel name. |
| """ |
| if channel_name == 'trunk': |
| return 'trunk' |
| - branch_number = self._object_store.Get(channel_name).Get() |
| - if branch_number is not None: |
| - return branch_number |
| + if data_type == 'branch': |
| + data = self._branch_object_store.Get(channel_name).Get() |
| + elif data_type == 'version': |
| + data = self._version_object_store.Get(channel_name).Get() |
|
epeterson
2013/06/02 00:25:50
Using two separate object_store with different cat
|
| + |
| + if data is not None: |
| + return data |
| try: |
| - version_json = json.loads(self._fetcher.Fetch(self._fetch_url).content) |
| + version_json = json.loads(self._fetch_result.Get().content) |
| except Exception as e: |
| # This can happen if omahaproxy is misbehaving, which we've seen before. |
| # Quick hack fix: just serve from trunk until it's fixed. |
| @@ -60,23 +98,68 @@ class BranchUtility(object): |
| 'Falling back to "trunk".' % e) |
| return 'trunk' |
| - branch_numbers = {} |
| + 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 |
| - branch = version['version'].split('.')[2] |
| - if branch not in branch_numbers: |
| - branch_numbers[branch] = 0 |
| + if data_type == 'branch': |
| + number = version['version'].split('.')[2] |
| + elif data_type == 'version': |
| + number = version['version'].split('.')[0] |
| + if number not in numbers: |
| + numbers[number] = 0 |
| else: |
| - branch_numbers[branch] += 1 |
| + numbers[number] += 1 |
| + |
| + sorted_numbers = sorted(numbers.iteritems(), |
| + None, |
| + operator.itemgetter(1), |
| + True) |
| + if data_type == 'branch': |
| + self._branch_object_store.Set(channel_name, int(sorted_numbers[0][0])) |
| + elif data_type == 'version': |
| + self._version_object_store.Set(channel_name, int(sorted_numbers[0][0])) |
| + |
| + return int(sorted_numbers[0][0]) |
| + |
| + def GetBranchNumberForVersion(self, version): |
|
epeterson
2013/06/02 00:25:50
ChromeVersionUtility only had two methods left, an
|
| + """Returns the most recent branch number for a given chrome version number |
| + using data stored on omahaproxy (see url_constants). |
| + """ |
| + if version == 'trunk': |
| + return 'trunk' |
| + |
| + branch = self._branch_object_store.Get(version).Get() |
| + if branch is not None: |
| + return branch |
| + version_json = json.loads(self._history_result.Get().content) |
| + for entry in version_json['events']: |
| + # Here, entry['title'] looks like: 'title - version#.#.branch#.#' |
| + version_title = entry['title'].split(' - ')[1].split('.') |
| + if version_title[0] == str(version): |
| + self._branch_object_store.Set(str(version), version_title[2]) |
| + return int(version_title[2]) |
| + raise ValueError( |
| + 'A branch number for the given version could not be determined.') |
| + |
| + def GetLatestVersionNumber(self): |
| + """Returns the most recent version number found using data stored on |
| + omahaproxy. |
| + """ |
| + latest_version = self._version_object_store.Get('latest').Get() |
|
epeterson
2013/06/02 00:25:50
Using an object_store here now, this was brought u
|
| + if latest_version is not None: |
| + return latest_version |
| - sorted_branches = sorted(branch_numbers.iteritems(), |
| - None, |
| - operator.itemgetter(1), |
| - True) |
| - self._object_store.Set(channel_name, sorted_branches[0][0]) |
| + version_json = json.loads(self._history_result.Get().content) |
| + latest_version = 0 |
| + for entry in version_json['events']: |
| + version_title = entry['title'].split(' - ')[1].split('.') |
| + version = int(version_title[0]) |
| + if version > latest_version: |
| + latest_version = version |
| - return sorted_branches[0][0] |
| + self._version_object_store.Set('latest', latest_version) |
| + return latest_version |