Chromium Code Reviews| 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 | 7 from extensions_paths import APP_YAML |
| 8 | 8 |
| 9 | 9 |
| 10 _APP_YAML_CONTAINER = ''' | 10 _APP_YAML_CONTAINER = ''' |
| 11 application: chrome-apps-doc | 11 application: chrome-apps-doc |
| 12 version: %s | 12 version: %s |
| 13 runtime: python27 | 13 runtime: python27 |
| 14 api_version: 1 | 14 api_version: 1 |
| 15 threadsafe: false | 15 threadsafe: false |
| 16 ''' | 16 ''' |
| 17 | 17 |
| 18 | 18 |
| 19 class AppYamlHelper(object): | 19 class AppYamlHelper(object): |
| 20 '''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 |
| 21 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. |
| 22 ''' | 22 ''' |
| 23 def __init__(self, | 23 def __init__(self, |
| 24 object_store_creator, | 24 object_store_creator, |
| 25 host_file_system_provider): | 25 host_file_system_provider): |
| 26 self._store = object_store_creator.Create( | 26 self._store = object_store_creator.Create( |
| 27 AppYamlHelper, | 27 AppYamlHelper, |
| 28 category=host_file_system_provider.GetTrunk().GetIdentity(), | 28 category=host_file_system_provider.GetMaster().GetIdentity(), |
| 29 start_empty=False) | 29 start_empty=False) |
| 30 self._host_file_system_provider = host_file_system_provider | 30 self._host_file_system_provider = host_file_system_provider |
| 31 | 31 |
| 32 @staticmethod | 32 @staticmethod |
| 33 def ExtractVersion(app_yaml, key='version'): | 33 def ExtractVersion(app_yaml, key='version'): |
| 34 '''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. |
| 35 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'). |
| 36 ''' | 36 ''' |
| 37 # We could properly parse this using a yaml library but Python doesn't have | 37 # We could properly parse this using a yaml library but Python doesn't have |
| 38 # one built in so whatevs. | 38 # one built in so whatevs. |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 67 def GenerateAppYaml(version): | 67 def GenerateAppYaml(version): |
| 68 '''Probably only useful for tests. | 68 '''Probably only useful for tests. |
| 69 ''' | 69 ''' |
| 70 return _APP_YAML_CONTAINER % version | 70 return _APP_YAML_CONTAINER % version |
| 71 | 71 |
| 72 def IsUpToDate(self, app_version): | 72 def IsUpToDate(self, app_version): |
| 73 '''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 |
| 74 checked into the host file system. | 74 checked into the host file system. |
| 75 ''' | 75 ''' |
| 76 checked_in_app_version = AppYamlHelper.ExtractVersion( | 76 checked_in_app_version = AppYamlHelper.ExtractVersion( |
| 77 self._host_file_system_provider.GetTrunk().ReadSingle(APP_YAML).Get()) | 77 self._host_file_system_provider.GetMaster().ReadSingle(APP_YAML).Get()) |
| 78 if app_version == checked_in_app_version: | 78 if app_version == checked_in_app_version: |
| 79 return True | 79 return True |
| 80 if AppYamlHelper.IsGreater(app_version, checked_in_app_version): | 80 if AppYamlHelper.IsGreater(app_version, checked_in_app_version): |
| 81 logging.warning( | 81 logging.warning( |
| 82 'Server is too new! Checked in %s < currently running %s' % ( | 82 'Server is too new! Checked in %s < currently running %s' % ( |
| 83 checked_in_app_version, app_version)) | 83 checked_in_app_version, app_version)) |
| 84 return True | 84 return True |
| 85 return False | 85 return False |
| 86 | 86 |
| 87 def GetFirstRevisionGreaterThan(self, app_version): | 87 def GetFirstRevisionGreaterThan(self, app_version): |
| 88 '''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 |
| 89 |app_version|. | 89 |app_version|. |
| 90 | 90 |
| 91 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 |
| 92 *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 |
| 93 IsUpToDate to validate the input before calling this method. | 93 IsUpToDate to validate the input before calling this method. |
| 94 ''' | 94 ''' |
| 95 stored = self._store.Get(app_version).Get() | 95 stored = self._store.Get(app_version).Get() |
| 96 if stored is None: | 96 if stored is None: |
| 97 stored = self._GetFirstRevisionGreaterThanImpl(app_version) | 97 stored = self._GetFirstRevisionGreaterThanImpl(app_version) |
| 98 assert stored is not None | 98 assert stored is not None |
| 99 self._store.Set(app_version, stored) | 99 self._store.Set(app_version, stored) |
| 100 return stored | 100 return stored |
| 101 | 101 |
| 102 def _GetFirstRevisionGreaterThanImpl(self, app_version): | 102 def _GetFirstRevisionGreaterThanImpl(self, app_version): |
| 103 # XXX(rockot): Tricky. The 'version' of app.yaml coming from | |
| 104 # GitilesFileSystem is a blob ID. | |
| 103 def get_app_yaml_revision(file_system): | 105 def get_app_yaml_revision(file_system): |
| 104 return int(file_system.Stat(APP_YAML).version) | 106 return int(file_system.Stat(APP_YAML).version) |
| 105 | 107 |
| 106 def has_greater_app_version(file_system): | 108 def has_greater_app_version(file_system): |
| 107 app_version_in_file_system = AppYamlHelper.ExtractVersion( | 109 app_version_in_file_system = AppYamlHelper.ExtractVersion( |
| 108 file_system.ReadSingle(APP_YAML).Get()) | 110 file_system.ReadSingle(APP_YAML).Get()) |
| 109 return AppYamlHelper.IsGreater(app_version_in_file_system, app_version) | 111 return AppYamlHelper.IsGreater(app_version_in_file_system, app_version) |
| 110 | 112 |
| 111 found = None | 113 found = None |
| 112 next_file_system = self._host_file_system_provider.GetTrunk() | 114 next_file_system = self._host_file_system_provider.GetMaster() |
| 113 | 115 |
| 114 while has_greater_app_version(next_file_system): | 116 while has_greater_app_version(next_file_system): |
| 115 found = get_app_yaml_revision(next_file_system) | 117 found = get_app_yaml_revision(next_file_system) |
| 116 # Back up a revision then find when app.yaml was last updated before then. | 118 # Back up a revision then find when app.yaml was last updated before then. |
| 117 if found == 0: | 119 if found == 0: |
| 118 logging.warning('All revisions are greater than %s' % app_version) | 120 logging.warning('All revisions are greater than %s' % app_version) |
| 119 return 0 | 121 return 0 |
| 120 next_file_system = self._host_file_system_provider.GetTrunk( | 122 next_file_system = self._host_file_system_provider.GetMaster( |
| 121 revision=found - 1) | 123 commit=next_file_system.GetPreviousCommitID().Get()) |
|
ahernandez
2014/08/21 17:48:02
This is the only use case for GetPreviousCommitID(
| |
| 122 | 124 |
| 123 if found is None: | 125 if found is None: |
| 124 raise ValueError('All revisions are less than %s' % app_version) | 126 raise ValueError('All revisions are less than %s' % app_version) |
| 125 return found | 127 return found |
| OLD | NEW |