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 functools | 5 import functools |
6 import os | 6 import os |
7 import sys | 7 import sys |
8 import tempfile | 8 import tempfile |
9 | 9 |
10 from recipe_engine import recipe_api | 10 from recipe_engine import recipe_api |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 OK_ATTRS = ('pardir', 'sep', 'pathsep') | 142 OK_ATTRS = ('pardir', 'sep', 'pathsep') |
143 | 143 |
144 # Because the native 'path' type in python is a str, we filter the *args | 144 # Because the native 'path' type in python is a str, we filter the *args |
145 # of these methods to stringify them first (otherwise they would be getting | 145 # of these methods to stringify them first (otherwise they would be getting |
146 # recipe_util_types.Path instances). | 146 # recipe_util_types.Path instances). |
147 FILTER_METHODS = ('abspath', 'basename', 'dirname', 'exists', 'join', 'split', | 147 FILTER_METHODS = ('abspath', 'basename', 'dirname', 'exists', 'join', 'split', |
148 'splitext') | 148 'splitext') |
149 | 149 |
150 def get_config_defaults(self): | 150 def get_config_defaults(self): |
151 return { | 151 return { |
152 'PLATFORM': self.m.platform.name, | |
153 'CURRENT_WORKING_DIR': self._startup_cwd, | 152 'CURRENT_WORKING_DIR': self._startup_cwd, |
154 'TEMP_DIR': self._temp_dir, | 153 'TEMP_DIR': self._temp_dir, |
155 } | 154 } |
156 | 155 |
157 def __init__(self, **kwargs): | 156 def __init__(self, **kwargs): |
158 super(PathApi, self).__init__(**kwargs) | 157 super(PathApi, self).__init__(**kwargs) |
159 config_types.Path.set_tostring_fn( | 158 config_types.Path.set_tostring_fn( |
160 PathToString(self, self._test_data)) | 159 PathToString(self, self._test_data)) |
161 | 160 |
162 # Used in mkdtemp when generating and checking expectations. | 161 # Used in mkdtemp when generating and checking expectations. |
(...skipping 10 matching lines...) Expand all Loading... |
173 self._startup_cwd = ['/', 'FakeTestingCWD'] | 172 self._startup_cwd = ['/', 'FakeTestingCWD'] |
174 # Appended to placeholder '[TMP]' to get fake path in test. | 173 # Appended to placeholder '[TMP]' to get fake path in test. |
175 self._temp_dir = ['/'] | 174 self._temp_dir = ['/'] |
176 | 175 |
177 self._config_set = False | 176 self._config_set = False |
178 | 177 |
179 def _lazy_set_config(self): | 178 def _lazy_set_config(self): |
180 if self._config_set: | 179 if self._config_set: |
181 return | 180 return |
182 self._config_set = True | 181 self._config_set = True |
183 | 182 self.set_config('BASE') |
184 path_config = self.m.properties.get('path_config') | |
185 if path_config in ('kitchen', 'swarming'): | |
186 self.set_config(path_config) | |
187 else: | |
188 self.set_config('buildbot') | |
189 | 183 |
190 def mock_add_paths(self, path): | 184 def mock_add_paths(self, path): |
191 """For testing purposes, assert that |path| exists.""" | 185 """For testing purposes, assert that |path| exists.""" |
192 if self._test_data.enabled: | 186 if self._test_data.enabled: |
193 self._path_mod.mock_add_paths(path) | 187 self._path_mod.mock_add_paths(path) |
194 | 188 |
195 def assert_absolute(self, path): | 189 def assert_absolute(self, path): |
196 assert self.abspath(path) == str(path), '%s is not absolute' % path | 190 assert self.abspath(path) == str(path), '%s is not absolute' % path |
197 | 191 |
198 def mkdtemp(self, prefix): | 192 def mkdtemp(self, prefix): |
199 """Makes a new temp directory, returns path to it.""" | 193 """Makes a new temp directory, returns path to it.""" |
200 self._lazy_set_config() | 194 self._lazy_set_config() |
201 if not self._test_data.enabled: # pragma: no cover | 195 if not self._test_data.enabled: # pragma: no cover |
202 # New path as str. | 196 # New path as str. |
203 new_path = tempfile.mkdtemp(prefix=prefix, dir=str(self['tmp_base'])) | 197 new_path = tempfile.mkdtemp(prefix=prefix, dir=str(self['tmp'])) |
204 # Ensure it's under self._temp_dir, convert to Path. | 198 # Ensure it's under self._temp_dir, convert to Path. |
205 new_path = _split_path(new_path) | 199 new_path = _split_path(new_path) |
206 assert new_path[:len(self._temp_dir)] == self._temp_dir | 200 assert new_path[:len(self._temp_dir)] == self._temp_dir |
207 temp_dir = self['tmp_base'].join(*new_path[len(self._temp_dir):]) | 201 temp_dir = self['tmp'].join(*new_path[len(self._temp_dir):]) |
208 else: | 202 else: |
209 self._test_counter += 1 | 203 self._test_counter += 1 |
210 assert isinstance(prefix, basestring) | 204 assert isinstance(prefix, basestring) |
211 temp_dir = self['tmp_base'].join( | 205 temp_dir = self['tmp'].join( |
212 '%s_tmp_%d' % (prefix, self._test_counter)) | 206 '%s_tmp_%d' % (prefix, self._test_counter)) |
213 self.mock_add_paths(temp_dir) | 207 self.mock_add_paths(temp_dir) |
214 return temp_dir | 208 return temp_dir |
215 | 209 |
216 def __contains__(self, pathname): | 210 def __contains__(self, pathname): |
217 self._lazy_set_config() | 211 self._lazy_set_config() |
218 return bool(self.c.dynamic_paths.get(pathname)) | 212 return bool(self.c.dynamic_paths.get(pathname)) |
219 | 213 |
220 def __setitem__(self, pathname, path): | 214 def __setitem__(self, pathname, path): |
221 self._lazy_set_config() | 215 self._lazy_set_config() |
(...skipping 21 matching lines...) Expand all Loading... |
243 if name in self.OK_ATTRS: | 237 if name in self.OK_ATTRS: |
244 return getattr(self._path_mod, name) | 238 return getattr(self._path_mod, name) |
245 if name in self.FILTER_METHODS: | 239 if name in self.FILTER_METHODS: |
246 return string_filter(getattr(self._path_mod, name)) | 240 return string_filter(getattr(self._path_mod, name)) |
247 raise AttributeError("'%s' object has no attribute '%s'" % | 241 raise AttributeError("'%s' object has no attribute '%s'" % |
248 (self._path_mod, name)) # pragma: no cover | 242 (self._path_mod, name)) # pragma: no cover |
249 | 243 |
250 def __dir__(self): # pragma: no cover | 244 def __dir__(self): # pragma: no cover |
251 # Used for helping out show_me_the_modules.py | 245 # Used for helping out show_me_the_modules.py |
252 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) | 246 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) |
OLD | NEW |