| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 logging | 5 import logging |
| 6 | 6 |
| 7 from extensions_paths import APP_YAML |
| 8 |
| 9 |
| 7 _APP_YAML_CONTAINER = ''' | 10 _APP_YAML_CONTAINER = ''' |
| 8 application: chrome-apps-doc | 11 application: chrome-apps-doc |
| 9 version: %s | 12 version: %s |
| 10 runtime: python27 | 13 runtime: python27 |
| 11 api_version: 1 | 14 api_version: 1 |
| 12 threadsafe: false | 15 threadsafe: false |
| 13 ''' | 16 ''' |
| 14 | 17 |
| 18 |
| 15 class AppYamlHelper(object): | 19 class AppYamlHelper(object): |
| 16 '''Parses the app.yaml file, and is able to step back in the host file | 20 '''Parses the app.yaml file, and is able to step back in the host file |
| 17 system's revision history to find when it changed to some given version. | 21 system's revision history to find when it changed to some given version. |
| 18 ''' | 22 ''' |
| 19 def __init__(self, | 23 def __init__(self, |
| 20 app_yaml_path, | |
| 21 object_store_creator, | 24 object_store_creator, |
| 22 host_file_system_provider): | 25 host_file_system_provider): |
| 23 self._app_yaml_path = app_yaml_path | |
| 24 self._store = object_store_creator.Create( | 26 self._store = object_store_creator.Create( |
| 25 AppYamlHelper, | 27 AppYamlHelper, |
| 26 category=host_file_system_provider.GetTrunk().GetIdentity(), | 28 category=host_file_system_provider.GetTrunk().GetIdentity(), |
| 27 start_empty=False) | 29 start_empty=False) |
| 28 self._host_file_system_provider = host_file_system_provider | 30 self._host_file_system_provider = host_file_system_provider |
| 29 | 31 |
| 30 @staticmethod | 32 @staticmethod |
| 31 def ExtractVersion(app_yaml, key='version'): | 33 def ExtractVersion(app_yaml, key='version'): |
| 32 '''Extracts the 'version' key from the contents of an app.yaml file. | 34 '''Extracts the 'version' key from the contents of an app.yaml file. |
| 33 Allow overriding the key to parse e.g. the cron file ('target'). | 35 Allow overriding the key to parse e.g. the cron file ('target'). |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 def GenerateAppYaml(version): | 67 def GenerateAppYaml(version): |
| 66 '''Probably only useful for tests. | 68 '''Probably only useful for tests. |
| 67 ''' | 69 ''' |
| 68 return _APP_YAML_CONTAINER % version | 70 return _APP_YAML_CONTAINER % version |
| 69 | 71 |
| 70 def IsUpToDate(self, app_version): | 72 def IsUpToDate(self, app_version): |
| 71 '''Returns True if the |app_version| is up to date with respect to the one | 73 '''Returns True if the |app_version| is up to date with respect to the one |
| 72 checked into the host file system. | 74 checked into the host file system. |
| 73 ''' | 75 ''' |
| 74 checked_in_app_version = AppYamlHelper.ExtractVersion( | 76 checked_in_app_version = AppYamlHelper.ExtractVersion( |
| 75 self._host_file_system_provider.GetTrunk().ReadSingle( | 77 self._host_file_system_provider.GetTrunk().ReadSingle(APP_YAML).Get()) |
| 76 self._app_yaml_path).Get()) | |
| 77 if app_version == checked_in_app_version: | 78 if app_version == checked_in_app_version: |
| 78 return True | 79 return True |
| 79 if AppYamlHelper.IsGreater(app_version, checked_in_app_version): | 80 if AppYamlHelper.IsGreater(app_version, checked_in_app_version): |
| 80 logging.warning( | 81 logging.warning( |
| 81 'Server is too new! Checked in %s < currently running %s' % ( | 82 'Server is too new! Checked in %s < currently running %s' % ( |
| 82 checked_in_app_version, app_version)) | 83 checked_in_app_version, app_version)) |
| 83 return True | 84 return True |
| 84 return False | 85 return False |
| 85 | 86 |
| 86 def GetFirstRevisionGreaterThan(self, app_version): | 87 def GetFirstRevisionGreaterThan(self, app_version): |
| 87 '''Finds the first revision that the version in app.yaml was greater than | 88 '''Finds the first revision that the version in app.yaml was greater than |
| 88 |app_version|. | 89 |app_version|. |
| 89 | 90 |
| 90 WARNING: if there is no such revision (e.g. the app is up to date, or | 91 WARNING: if there is no such revision (e.g. the app is up to date, or |
| 91 *oops* the app is even newer) then this will throw a ValueError. Use | 92 *oops* the app is even newer) then this will throw a ValueError. Use |
| 92 IsUpToDate to validate the input before calling this method. | 93 IsUpToDate to validate the input before calling this method. |
| 93 ''' | 94 ''' |
| 94 stored = self._store.Get(app_version).Get() | 95 stored = self._store.Get(app_version).Get() |
| 95 if stored is None: | 96 if stored is None: |
| 96 stored = self._GetFirstRevisionGreaterThanImpl(app_version) | 97 stored = self._GetFirstRevisionGreaterThanImpl(app_version) |
| 97 assert stored is not None | 98 assert stored is not None |
| 98 self._store.Set(app_version, stored) | 99 self._store.Set(app_version, stored) |
| 99 return stored | 100 return stored |
| 100 | 101 |
| 101 def _GetFirstRevisionGreaterThanImpl(self, app_version): | 102 def _GetFirstRevisionGreaterThanImpl(self, app_version): |
| 102 def get_app_yaml_revision(file_system): | 103 def get_app_yaml_revision(file_system): |
| 103 return int(file_system.Stat(self._app_yaml_path).version) | 104 return int(file_system.Stat(APP_YAML).version) |
| 104 | 105 |
| 105 def has_greater_app_version(file_system): | 106 def has_greater_app_version(file_system): |
| 106 app_version_in_file_system = AppYamlHelper.ExtractVersion( | 107 app_version_in_file_system = AppYamlHelper.ExtractVersion( |
| 107 file_system.ReadSingle(self._app_yaml_path).Get()) | 108 file_system.ReadSingle(APP_YAML).Get()) |
| 108 return AppYamlHelper.IsGreater(app_version_in_file_system, app_version) | 109 return AppYamlHelper.IsGreater(app_version_in_file_system, app_version) |
| 109 | 110 |
| 110 found = None | 111 found = None |
| 111 next_file_system = self._host_file_system_provider.GetTrunk() | 112 next_file_system = self._host_file_system_provider.GetTrunk() |
| 112 | 113 |
| 113 while has_greater_app_version(next_file_system): | 114 while has_greater_app_version(next_file_system): |
| 114 found = get_app_yaml_revision(next_file_system) | 115 found = get_app_yaml_revision(next_file_system) |
| 115 # Back up a revision then find when app.yaml was last updated before then. | 116 # Back up a revision then find when app.yaml was last updated before then. |
| 116 if found == 0: | 117 if found == 0: |
| 117 logging.warning('All revisions are greater than %s' % app_version) | 118 logging.warning('All revisions are greater than %s' % app_version) |
| 118 return 0 | 119 return 0 |
| 119 next_file_system = self._host_file_system_provider.GetTrunk( | 120 next_file_system = self._host_file_system_provider.GetTrunk( |
| 120 revision=found - 1) | 121 revision=found - 1) |
| 121 | 122 |
| 122 if found is None: | 123 if found is None: |
| 123 raise ValueError('All revisions are less than %s' % app_version) | 124 raise ValueError('All revisions are less than %s' % app_version) |
| 124 return found | 125 return found |
| OLD | NEW |