Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import logging | |
| 6 | |
| 7 _APP_YAML_CONTAINER = ''' | |
| 8 application: chrome-apps-doc | |
| 9 version: %s | |
| 10 runtime: python27 | |
| 11 api_version: 1 | |
| 12 threadsafe: false | |
| 13 ''' | |
| 14 | |
| 15 class AppYamlHelper(object): | |
| 16 '''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. | |
| 18 ''' | |
| 19 class Delegate(object): | |
| 20 def GetHostFileSystemForRevision(self, revision): | |
| 21 '''Revision may not be None. | |
| 22 ''' | |
| 23 raise NotImplementedError() | |
| 24 | |
| 25 def __init__(self, | |
| 26 app_yaml_path, | |
| 27 file_system_at_head, | |
| 28 delegate, | |
| 29 object_store_creator): | |
| 30 self._app_yaml_path = app_yaml_path | |
| 31 self._file_system_at_head = file_system_at_head | |
| 32 self._delegate = delegate | |
| 33 self._store = object_store_creator.Create( | |
| 34 AppYamlHelper, | |
| 35 category=file_system_at_head.GetIdentity(), | |
| 36 start_empty=False) | |
| 37 | |
| 38 @staticmethod | |
| 39 def ExtractVersion(app_yaml): | |
| 40 '''Extracts the 'version' key from the contents of an app.yaml file. | |
| 41 ''' | |
| 42 # We could properly parse this using a yaml library but Python doesn't have | |
| 43 # one built in so whatevs. | |
| 44 version_key = 'version:' | |
| 45 version_line = [line for line in app_yaml.split('\n') | |
| 46 if line.startswith(version_key)][0] | |
| 47 return version_line[len(version_key):].strip() | |
| 48 | |
| 49 @staticmethod | |
| 50 def IsGreater(lhs, rhs): | |
| 51 '''Return whether the app.yaml version |lhs| > |rhs|. This is tricky | |
| 52 because versions are typically not numbers but rather 2-0-9, 2-0-12, | |
| 53 2-1-0, etc - and 2-1-0 > 2-0-10 > 2-0-9. | |
| 54 ''' | |
| 55 lhs_parts = lhs.replace('-', '.').split('.') | |
|
方觉(Fang Jue)
2013/05/10 08:49:17
split('-')?
not at google - send to devlin
2013/05/10 17:49:58
I figure somebody might switch to . rather than -
| |
| 56 rhs_parts = rhs.replace('-', '.').split('.') | |
| 57 while lhs_parts and rhs_parts: | |
| 58 lhs_msb = int(lhs_parts.pop(0)) | |
| 59 rhs_msb = int(rhs_parts.pop(0)) | |
| 60 if lhs_msb != rhs_msb: | |
| 61 return lhs_msb > rhs_msb | |
| 62 return len(lhs) > len(rhs) | |
| 63 | |
| 64 @staticmethod | |
| 65 def GenerateAppYaml(version): | |
| 66 '''Probably only useful for tests. | |
| 67 ''' | |
| 68 return _APP_YAML_CONTAINER % version | |
| 69 | |
| 70 def IsUpToDate(self, app_version): | |
| 71 '''Returns True if the |app_version| is up to date with respect to the one | |
| 72 checked into the host file system. | |
| 73 ''' | |
| 74 checked_in_app_version = AppYamlHelper.ExtractVersion( | |
| 75 self._file_system_at_head.ReadSingle(self._app_yaml_path)) | |
| 76 if app_version == checked_in_app_version: | |
| 77 return True | |
| 78 if AppYamlHelper.IsGreater(app_version, checked_in_app_version): | |
| 79 logging.warning( | |
| 80 'Server is too new! Checked in %s < currently running %s' % ( | |
| 81 checked_in_app_version, app_version)) | |
| 82 return True | |
| 83 return False | |
| 84 | |
| 85 def GetFirstRevisionGreaterThan(self, app_version): | |
| 86 '''Finds the first revision that the version in app.yaml was greater than | |
| 87 |app_version|. | |
| 88 | |
| 89 WARNING: if there is no such revision (e.g. the app is up to date, or | |
| 90 *oops* the app is even newer) then this will throw a ValueError. Use | |
| 91 IsUpToDate to validate the input before calling this method. | |
| 92 ''' | |
| 93 stored = self._store.Get(app_version).Get() | |
| 94 if stored is None: | |
| 95 stored = self._GetFirstRevisionGreaterThanImpl(app_version) | |
| 96 assert stored is not None | |
| 97 self._store.Set(app_version, stored) | |
| 98 return stored | |
| 99 | |
| 100 def _GetFirstRevisionGreaterThanImpl(self, app_version): | |
| 101 def get_app_yaml_revision(file_system): | |
| 102 return int(file_system.Stat(self._app_yaml_path).version) | |
| 103 | |
| 104 def has_greater_app_version(file_system): | |
| 105 app_version_in_file_system = AppYamlHelper.ExtractVersion( | |
| 106 file_system.ReadSingle(self._app_yaml_path)) | |
| 107 return AppYamlHelper.IsGreater(app_version_in_file_system, app_version) | |
| 108 | |
| 109 found = None | |
| 110 next_file_system = self._file_system_at_head | |
| 111 | |
| 112 while has_greater_app_version(next_file_system): | |
| 113 found = get_app_yaml_revision(next_file_system) | |
| 114 # Back up a revision then find when app.yaml was last updated before then. | |
| 115 if found == 0: | |
| 116 logging.warning('All revisions are greater than %s' % app_version) | |
| 117 return 0 | |
| 118 next_file_system = self._delegate.GetHostFileSystemForRevision( | |
| 119 found - 1) | |
| 120 | |
| 121 if found is None: | |
| 122 raise ValueError('All revisions are less than %s' % app_version) | |
| 123 return found | |
| OLD | NEW |