| 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, |
| 152 'CURRENT_WORKING_DIR': self._startup_cwd, | 153 'CURRENT_WORKING_DIR': self._startup_cwd, |
| 153 'TEMP_DIR': self._temp_dir, | 154 'TEMP_DIR': self._temp_dir, |
| 154 } | 155 } |
| 155 | 156 |
| 156 def __init__(self, **kwargs): | 157 def __init__(self, **kwargs): |
| 157 super(PathApi, self).__init__(**kwargs) | 158 super(PathApi, self).__init__(**kwargs) |
| 158 config_types.Path.set_tostring_fn( | 159 config_types.Path.set_tostring_fn( |
| 159 PathToString(self, self._test_data)) | 160 PathToString(self, self._test_data)) |
| 160 | 161 |
| 161 # Used in mkdtemp when generating and checking expectations. | 162 # Used in mkdtemp when generating and checking expectations. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 172 self._startup_cwd = ['/', 'FakeTestingCWD'] | 173 self._startup_cwd = ['/', 'FakeTestingCWD'] |
| 173 # Appended to placeholder '[TMP]' to get fake path in test. | 174 # Appended to placeholder '[TMP]' to get fake path in test. |
| 174 self._temp_dir = ['/'] | 175 self._temp_dir = ['/'] |
| 175 | 176 |
| 176 self._config_set = False | 177 self._config_set = False |
| 177 | 178 |
| 178 def _lazy_set_config(self): | 179 def _lazy_set_config(self): |
| 179 if self._config_set: | 180 if self._config_set: |
| 180 return | 181 return |
| 181 self._config_set = True | 182 self._config_set = True |
| 182 self.set_config('BASE') | 183 |
| 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') |
| 183 | 189 |
| 184 def mock_add_paths(self, path): | 190 def mock_add_paths(self, path): |
| 185 """For testing purposes, assert that |path| exists.""" | 191 """For testing purposes, assert that |path| exists.""" |
| 186 if self._test_data.enabled: | 192 if self._test_data.enabled: |
| 187 self._path_mod.mock_add_paths(path) | 193 self._path_mod.mock_add_paths(path) |
| 188 | 194 |
| 189 def assert_absolute(self, path): | 195 def assert_absolute(self, path): |
| 190 assert self.abspath(path) == str(path), '%s is not absolute' % path | 196 assert self.abspath(path) == str(path), '%s is not absolute' % path |
| 191 | 197 |
| 192 def mkdtemp(self, prefix): | 198 def mkdtemp(self, prefix): |
| 193 """Makes a new temp directory, returns path to it.""" | 199 """Makes a new temp directory, returns path to it.""" |
| 194 self._lazy_set_config() | 200 self._lazy_set_config() |
| 195 if not self._test_data.enabled: # pragma: no cover | 201 if not self._test_data.enabled: # pragma: no cover |
| 196 # New path as str. | 202 # New path as str. |
| 197 new_path = tempfile.mkdtemp(prefix=prefix, dir=str(self['tmp'])) | 203 new_path = tempfile.mkdtemp(prefix=prefix, dir=str(self['tmp_base'])) |
| 198 # Ensure it's under self._temp_dir, convert to Path. | 204 # Ensure it's under self._temp_dir, convert to Path. |
| 199 new_path = _split_path(new_path) | 205 new_path = _split_path(new_path) |
| 200 assert new_path[:len(self._temp_dir)] == self._temp_dir | 206 assert new_path[:len(self._temp_dir)] == self._temp_dir |
| 201 temp_dir = self['tmp'].join(*new_path[len(self._temp_dir):]) | 207 temp_dir = self['tmp_base'].join(*new_path[len(self._temp_dir):]) |
| 202 else: | 208 else: |
| 203 self._test_counter += 1 | 209 self._test_counter += 1 |
| 204 assert isinstance(prefix, basestring) | 210 assert isinstance(prefix, basestring) |
| 205 temp_dir = self['tmp'].join( | 211 temp_dir = self['tmp_base'].join( |
| 206 '%s_tmp_%d' % (prefix, self._test_counter)) | 212 '%s_tmp_%d' % (prefix, self._test_counter)) |
| 207 self.mock_add_paths(temp_dir) | 213 self.mock_add_paths(temp_dir) |
| 208 return temp_dir | 214 return temp_dir |
| 209 | 215 |
| 210 def __contains__(self, pathname): | 216 def __contains__(self, pathname): |
| 211 self._lazy_set_config() | 217 self._lazy_set_config() |
| 212 return bool(self.c.dynamic_paths.get(pathname)) | 218 return bool(self.c.dynamic_paths.get(pathname)) |
| 213 | 219 |
| 214 def __setitem__(self, pathname, path): | 220 def __setitem__(self, pathname, path): |
| 215 self._lazy_set_config() | 221 self._lazy_set_config() |
| (...skipping 21 matching lines...) Expand all Loading... |
| 237 if name in self.OK_ATTRS: | 243 if name in self.OK_ATTRS: |
| 238 return getattr(self._path_mod, name) | 244 return getattr(self._path_mod, name) |
| 239 if name in self.FILTER_METHODS: | 245 if name in self.FILTER_METHODS: |
| 240 return string_filter(getattr(self._path_mod, name)) | 246 return string_filter(getattr(self._path_mod, name)) |
| 241 raise AttributeError("'%s' object has no attribute '%s'" % | 247 raise AttributeError("'%s' object has no attribute '%s'" % |
| 242 (self._path_mod, name)) # pragma: no cover | 248 (self._path_mod, name)) # pragma: no cover |
| 243 | 249 |
| 244 def __dir__(self): # pragma: no cover | 250 def __dir__(self): # pragma: no cover |
| 245 # Used for helping out show_me_the_modules.py | 251 # Used for helping out show_me_the_modules.py |
| 246 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) | 252 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) |
| OLD | NEW |