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 _APP_YAML_CONTAINER = ''' | 7 _APP_YAML_CONTAINER = ''' |
8 application: chrome-apps-doc | 8 application: chrome-apps-doc |
9 version: %s | 9 version: %s |
10 runtime: python27 | 10 runtime: python27 |
11 api_version: 1 | 11 api_version: 1 |
12 threadsafe: false | 12 threadsafe: false |
13 ''' | 13 ''' |
14 | 14 |
15 class AppYamlHelper(object): | 15 class AppYamlHelper(object): |
16 '''Parses the app.yaml file, and is able to step back in the host file | 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. | 17 system's revision history to find when it changed to some given version. |
18 ''' | 18 ''' |
19 def __init__(self, | 19 def __init__(self, |
20 app_yaml_path, | 20 app_yaml_path, |
21 file_system_at_head, | |
22 object_store_creator, | 21 object_store_creator, |
23 host_file_system_creator): | 22 host_file_system_provider): |
24 self._app_yaml_path = app_yaml_path | 23 self._app_yaml_path = app_yaml_path |
25 self._file_system_at_head = file_system_at_head | |
26 self._store = object_store_creator.Create( | 24 self._store = object_store_creator.Create( |
27 AppYamlHelper, | 25 AppYamlHelper, |
28 category=file_system_at_head.GetIdentity(), | 26 category=host_file_system_provider.GetTrunk().GetIdentity(), |
29 start_empty=False) | 27 start_empty=False) |
30 self._host_file_system_creator = host_file_system_creator | 28 self._host_file_system_provider = host_file_system_provider |
31 | 29 |
32 @staticmethod | 30 @staticmethod |
33 def ExtractVersion(app_yaml, key='version'): | 31 def ExtractVersion(app_yaml, key='version'): |
34 '''Extracts the 'version' key from the contents of an app.yaml file. | 32 '''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'). | 33 Allow overriding the key to parse e.g. the cron file ('target'). |
36 ''' | 34 ''' |
37 # We could properly parse this using a yaml library but Python doesn't have | 35 # We could properly parse this using a yaml library but Python doesn't have |
38 # one built in so whatevs. | 36 # one built in so whatevs. |
39 key_colon = '%s:' % key | 37 key_colon = '%s:' % key |
40 versions = [line.strip()[len(key_colon):].strip() | 38 versions = [line.strip()[len(key_colon):].strip() |
(...skipping 26 matching lines...) Expand all Loading... |
67 def GenerateAppYaml(version): | 65 def GenerateAppYaml(version): |
68 '''Probably only useful for tests. | 66 '''Probably only useful for tests. |
69 ''' | 67 ''' |
70 return _APP_YAML_CONTAINER % version | 68 return _APP_YAML_CONTAINER % version |
71 | 69 |
72 def IsUpToDate(self, app_version): | 70 def IsUpToDate(self, app_version): |
73 '''Returns True if the |app_version| is up to date with respect to the one | 71 '''Returns True if the |app_version| is up to date with respect to the one |
74 checked into the host file system. | 72 checked into the host file system. |
75 ''' | 73 ''' |
76 checked_in_app_version = AppYamlHelper.ExtractVersion( | 74 checked_in_app_version = AppYamlHelper.ExtractVersion( |
77 self._file_system_at_head.ReadSingle(self._app_yaml_path)) | 75 self._host_file_system_provider.GetTrunk().ReadSingle( |
| 76 self._app_yaml_path)) |
78 if app_version == checked_in_app_version: | 77 if app_version == checked_in_app_version: |
79 return True | 78 return True |
80 if AppYamlHelper.IsGreater(app_version, checked_in_app_version): | 79 if AppYamlHelper.IsGreater(app_version, checked_in_app_version): |
81 logging.warning( | 80 logging.warning( |
82 'Server is too new! Checked in %s < currently running %s' % ( | 81 'Server is too new! Checked in %s < currently running %s' % ( |
83 checked_in_app_version, app_version)) | 82 checked_in_app_version, app_version)) |
84 return True | 83 return True |
85 return False | 84 return False |
86 | 85 |
87 def GetFirstRevisionGreaterThan(self, app_version): | 86 def GetFirstRevisionGreaterThan(self, app_version): |
(...skipping 14 matching lines...) Expand all Loading... |
102 def _GetFirstRevisionGreaterThanImpl(self, app_version): | 101 def _GetFirstRevisionGreaterThanImpl(self, app_version): |
103 def get_app_yaml_revision(file_system): | 102 def get_app_yaml_revision(file_system): |
104 return int(file_system.Stat(self._app_yaml_path).version) | 103 return int(file_system.Stat(self._app_yaml_path).version) |
105 | 104 |
106 def has_greater_app_version(file_system): | 105 def has_greater_app_version(file_system): |
107 app_version_in_file_system = AppYamlHelper.ExtractVersion( | 106 app_version_in_file_system = AppYamlHelper.ExtractVersion( |
108 file_system.ReadSingle(self._app_yaml_path)) | 107 file_system.ReadSingle(self._app_yaml_path)) |
109 return AppYamlHelper.IsGreater(app_version_in_file_system, app_version) | 108 return AppYamlHelper.IsGreater(app_version_in_file_system, app_version) |
110 | 109 |
111 found = None | 110 found = None |
112 next_file_system = self._file_system_at_head | 111 next_file_system = self._host_file_system_provider.GetTrunk() |
113 | 112 |
114 while has_greater_app_version(next_file_system): | 113 while has_greater_app_version(next_file_system): |
115 found = get_app_yaml_revision(next_file_system) | 114 found = get_app_yaml_revision(next_file_system) |
116 # Back up a revision then find when app.yaml was last updated before then. | 115 # Back up a revision then find when app.yaml was last updated before then. |
117 if found == 0: | 116 if found == 0: |
118 logging.warning('All revisions are greater than %s' % app_version) | 117 logging.warning('All revisions are greater than %s' % app_version) |
119 return 0 | 118 return 0 |
120 next_file_system = self._host_file_system_creator.Create( | 119 next_file_system = self._host_file_system_provider.GetTrunk( |
121 revision=found - 1) | 120 revision=found - 1) |
122 | 121 |
123 if found is None: | 122 if found is None: |
124 raise ValueError('All revisions are less than %s' % app_version) | 123 raise ValueError('All revisions are less than %s' % app_version) |
125 return found | 124 return found |
OLD | NEW |