OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import json |
| 6 import logging |
| 7 |
| 8 import object_store |
| 9 |
| 10 class ChromeVersionDataSource: |
| 11 """Generates and stores API data sources corresponding to the latest branch |
| 12 number for a given version of Chrome. |
| 13 |
| 14 Accesses data found on omahaproxy in order to link branch numbers to version |
| 15 numbers. |
| 16 """ |
| 17 |
| 18 def __init__(self, |
| 19 base_path, |
| 20 fetcher, |
| 21 object_store, |
| 22 create_api_data_source_for_branch): |
| 23 self._base_path = base_path |
| 24 self._fetcher = fetcher |
| 25 self._object_store = object_store |
| 26 self._create_api_data_source_for_branch = create_api_data_source_for_branch |
| 27 |
| 28 def _GetBranchNumberForVersion(self, version_number): |
| 29 """Returns the most recent branch number for a given chrome version number |
| 30 using data stored on omahaproxy (see url_constants) |
| 31 """ |
| 32 if version_number == 'trunk': |
| 33 return 'trunk' |
| 34 |
| 35 branch = self._object_store.Get( |
| 36 version_number, |
| 37 object_store.CHROME_VERSION_DATA_SOURCE).Get() |
| 38 |
| 39 if branch is not None: |
| 40 return branch |
| 41 |
| 42 try: |
| 43 version_json = json.loads(self._fetcher.Fetch(self._base_path).content) |
| 44 except Exception as e: |
| 45 # If omahaproxy is having problems, report it. |
| 46 logging.error('Could not fetch data at Omaha Proxy: %s' % (e)) |
| 47 return None |
| 48 |
| 49 # Here, entry['title'] looks like: 'title - version#.#.branch#.#' |
| 50 for entry in version_json['events']: |
| 51 version_title = entry['title'].split(' - ')[1].split('.') |
| 52 if version_title[0] == version_number: |
| 53 self._object_store.Set(version_number, |
| 54 version_title[2], |
| 55 object_store.CHROME_VERSION_DATA_SOURCE) |
| 56 return version_title[2] |
| 57 return None |
| 58 |
| 59 def GetLatestVersionNumber(self): |
| 60 try: |
| 61 version_json = json.loads(self._fetcher.Fetch(self._base_path).content) |
| 62 except Exception as e: |
| 63 logging.error('Could not fetch data at Omaha Proxy: %s' % (e)) |
| 64 return None |
| 65 |
| 66 latest_version = 0 |
| 67 for entry in version_json['events']: |
| 68 version_title = entry['title'].split(' - ')[1].split('.') |
| 69 version = int(version_title[0]) |
| 70 if version > latest_version: |
| 71 latest_version = version |
| 72 return latest_version |
| 73 |
| 74 def GetDataSourceForVersion(self, version_number): |
| 75 """Returns an api data source for the most recent branch number |
| 76 corresponding to a given version number |
| 77 """ |
| 78 branch_number = self._GetBranchNumberForVersion(version_number) |
| 79 branch_api_data_source = self._create_api_data_source_for_branch( |
| 80 branch_number) |
| 81 return branch_api_data_source |
OLD | NEW |