| 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 slave import recipe_api | 10 from recipe_engine import recipe_api |
| 11 from slave import recipe_config_types | 11 from recipe_engine import config_types |
| 12 | 12 |
| 13 | 13 |
| 14 def PathToString(api, test): | 14 def PathToString(api, test): |
| 15 def PathToString_inner(path): | 15 def PathToString_inner(path): |
| 16 assert isinstance(path, recipe_config_types.Path) | 16 assert isinstance(path, config_types.Path) |
| 17 base_path = None | 17 base_path = None |
| 18 suffix = path.platform_ext.get(api.m.platform.name, '') | 18 suffix = path.platform_ext.get(api.m.platform.name, '') |
| 19 if isinstance(path.base, recipe_config_types.NamedBasePath): | 19 if isinstance(path.base, config_types.NamedBasePath): |
| 20 name = path.base.name | 20 name = path.base.name |
| 21 if name in api.c.dynamic_paths: | 21 if name in api.c.dynamic_paths: |
| 22 base_path = api.c.dynamic_paths[name] | 22 base_path = api.c.dynamic_paths[name] |
| 23 elif name in api.c.base_paths: | 23 elif name in api.c.base_paths: |
| 24 if test.enabled: | 24 if test.enabled: |
| 25 base_path = repr(path.base) | 25 base_path = repr(path.base) |
| 26 else: # pragma: no cover | 26 else: # pragma: no cover |
| 27 base_path = api.join(*api.c.base_paths[name]) | 27 base_path = api.join(*api.c.base_paths[name]) |
| 28 elif isinstance(path.base, recipe_config_types.ModuleBasePath): | 28 elif isinstance(path.base, config_types.ModuleBasePath): |
| 29 if test.enabled: | 29 if test.enabled: |
| 30 base_path = repr(path.base) | 30 base_path = repr(path.base) |
| 31 else: # pragma: no cover | 31 else: # pragma: no cover |
| 32 base_path = os.path.dirname(path.base.module.__file__) | 32 base_path = os.path.dirname(path.base.module.__file__) |
| 33 else: # pragma: no cover | 33 else: # pragma: no cover |
| 34 raise NotImplementedError('PathToString not implemented for %s' % | 34 raise NotImplementedError('PathToString not implemented for %s' % |
| 35 path.base.__class__.__name__) | 35 path.base.__class__.__name__) |
| 36 assert base_path, 'Could not get base %r for path' % path.base | 36 assert base_path, 'Could not get base %r for path' % path.base |
| 37 return api.join(base_path, *path.pieces) + suffix | 37 return api.join(base_path, *path.pieces) + suffix |
| 38 return PathToString_inner | 38 return PathToString_inner |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 'splitext') | 147 'splitext') |
| 148 | 148 |
| 149 def get_config_defaults(self): | 149 def get_config_defaults(self): |
| 150 return { | 150 return { |
| 151 'CURRENT_WORKING_DIR': self._startup_cwd, | 151 'CURRENT_WORKING_DIR': self._startup_cwd, |
| 152 'TEMP_DIR': self._temp_dir, | 152 'TEMP_DIR': self._temp_dir, |
| 153 } | 153 } |
| 154 | 154 |
| 155 def __init__(self, **kwargs): | 155 def __init__(self, **kwargs): |
| 156 super(PathApi, self).__init__(**kwargs) | 156 super(PathApi, self).__init__(**kwargs) |
| 157 recipe_config_types.Path.set_tostring_fn( | 157 config_types.Path.set_tostring_fn( |
| 158 PathToString(self, self._test_data)) | 158 PathToString(self, self._test_data)) |
| 159 | 159 |
| 160 # Used in mkdtemp when generating and checking expectations. | 160 # Used in mkdtemp when generating and checking expectations. |
| 161 self._test_counter = 0 | 161 self._test_counter = 0 |
| 162 | 162 |
| 163 if not self._test_data.enabled: # pragma: no cover | 163 if not self._test_data.enabled: # pragma: no cover |
| 164 self._path_mod = os.path | 164 self._path_mod = os.path |
| 165 # Capture the cwd on process start to avoid shenanigans. | 165 # Capture the cwd on process start to avoid shenanigans. |
| 166 self._startup_cwd = _split_path(os.getcwd()) | 166 self._startup_cwd = _split_path(os.getcwd()) |
| 167 # Use default system wide temp dir as a root temp dir. | 167 # Use default system wide temp dir as a root temp dir. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 self._test_counter += 1 | 199 self._test_counter += 1 |
| 200 temp_dir = self['tmp_base'].join( | 200 temp_dir = self['tmp_base'].join( |
| 201 '%s_tmp_%d' % (prefix, self._test_counter)) | 201 '%s_tmp_%d' % (prefix, self._test_counter)) |
| 202 self.mock_add_paths(temp_dir) | 202 self.mock_add_paths(temp_dir) |
| 203 return temp_dir | 203 return temp_dir |
| 204 | 204 |
| 205 def __contains__(self, pathname): | 205 def __contains__(self, pathname): |
| 206 return bool(self.c.dynamic_paths.get(pathname)) | 206 return bool(self.c.dynamic_paths.get(pathname)) |
| 207 | 207 |
| 208 def __setitem__(self, pathname, path): | 208 def __setitem__(self, pathname, path): |
| 209 assert isinstance(path, recipe_config_types.Path), ( | 209 assert isinstance(path, config_types.Path), ( |
| 210 'Setting dynamic path to something other than a Path: %r' % path) | 210 'Setting dynamic path to something other than a Path: %r' % path) |
| 211 assert pathname in self.c.dynamic_paths, ( | 211 assert pathname in self.c.dynamic_paths, ( |
| 212 'Must declare dynamic path (%r) in config before setting it.' % path) | 212 'Must declare dynamic path (%r) in config before setting it.' % path) |
| 213 assert isinstance(path.base, recipe_config_types.BasePath), ( | 213 assert isinstance(path.base, config_types.BasePath), ( |
| 214 'Dynamic path values must be based on a base_path' % path.base) | 214 'Dynamic path values must be based on a base_path' % path.base) |
| 215 self.c.dynamic_paths[pathname] = path | 215 self.c.dynamic_paths[pathname] = path |
| 216 | 216 |
| 217 def __getitem__(self, name): | 217 def __getitem__(self, name): |
| 218 if name in self.c.dynamic_paths: | 218 if name in self.c.dynamic_paths: |
| 219 r = self.c.dynamic_paths[name] | 219 r = self.c.dynamic_paths[name] |
| 220 assert r is not None, ('Tried to get dynamic path %s but it has not been ' | 220 assert r is not None, ('Tried to get dynamic path %s but it has not been ' |
| 221 'set yet.' % name) | 221 'set yet.' % name) |
| 222 return r | 222 return r |
| 223 if name in self.c.base_paths: | 223 if name in self.c.base_paths: |
| 224 return recipe_config_types.Path(recipe_config_types.NamedBasePath(name)) | 224 return config_types.Path(config_types.NamedBasePath(name)) |
| 225 | 225 |
| 226 def __getattr__(self, name): | 226 def __getattr__(self, name): |
| 227 # retrieve os.path attributes | 227 # retrieve os.path attributes |
| 228 if name in self.OK_ATTRS: | 228 if name in self.OK_ATTRS: |
| 229 return getattr(self._path_mod, name) | 229 return getattr(self._path_mod, name) |
| 230 if name in self.FILTER_METHODS: | 230 if name in self.FILTER_METHODS: |
| 231 return string_filter(getattr(self._path_mod, name)) | 231 return string_filter(getattr(self._path_mod, name)) |
| 232 raise AttributeError("'%s' object has no attribute '%s'" % | 232 raise AttributeError("'%s' object has no attribute '%s'" % |
| 233 (self._path_mod, name)) # pragma: no cover | 233 (self._path_mod, name)) # pragma: no cover |
| 234 | 234 |
| 235 def __dir__(self): # pragma: no cover | 235 def __dir__(self): # pragma: no cover |
| 236 # Used for helping out show_me_the_modules.py | 236 # Used for helping out show_me_the_modules.py |
| 237 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) | 237 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) |
| OLD | NEW |