| 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 tempfile | 8 import tempfile |
| 8 | 9 |
| 9 from slave import recipe_api | 10 from slave import recipe_api |
| 10 from slave import recipe_config_types | 11 from slave import recipe_config_types |
| 11 | 12 |
| 12 | 13 |
| 13 def PathTostring(api, test): | 14 def PathToString(api, test): |
| 14 def PathTostring_inner(path): | 15 def PathToString_inner(path): |
| 15 assert isinstance(path, recipe_config_types.Path) | 16 assert isinstance(path, recipe_config_types.Path) |
| 16 base_path = None | 17 base_path = None |
| 17 suffix = path.platform_ext.get(api.m.platform.name, '') | 18 suffix = path.platform_ext.get(api.m.platform.name, '') |
| 18 if path.base in api.c.dynamic_paths: | 19 if isinstance(path.base, recipe_config_types.NamedBasePath): |
| 19 base_path = api.c.dynamic_paths[path.base] | 20 name = path.base.name |
| 20 elif path.base in api.c.base_paths: | 21 if name in api.c.dynamic_paths: |
| 22 base_path = api.c.dynamic_paths[name] |
| 23 elif name in api.c.base_paths: |
| 24 if test.enabled: |
| 25 base_path = repr(path.base) |
| 26 else: # pragma: no cover |
| 27 base_path = api.join(*api.c.base_paths[name]) |
| 28 elif isinstance(path.base, recipe_config_types.ModuleBasePath): |
| 21 if test.enabled: | 29 if test.enabled: |
| 22 base_path = '[%s]' % path.base.upper() | 30 base_path = repr(path.base) |
| 23 else: # pragma: no cover | 31 else: # pragma: no cover |
| 24 base_path = api.join(*api.c.base_paths[path.base]) | 32 base_path = os.path.dirname(path.base.module.__file__) |
| 33 else: # pragma: no cover |
| 34 raise NotImplementedError('PathToString not implemented for %s' % |
| 35 path.base.__class__.__name__) |
| 25 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 |
| 26 return api.join(base_path, *path.pieces) + suffix | 37 return api.join(base_path, *path.pieces) + suffix |
| 27 return PathTostring_inner | 38 return PathToString_inner |
| 28 | 39 |
| 29 | 40 |
| 30 def string_filter(func): | 41 def string_filter(func): |
| 31 @functools.wraps(func) | 42 @functools.wraps(func) |
| 32 def inner(*args, **kwargs): | 43 def inner(*args, **kwargs): |
| 33 return func(*map(str, args), **kwargs) | 44 return func(*map(str, args), **kwargs) |
| 34 return inner | 45 return inner |
| 35 | 46 |
| 36 | 47 |
| 37 class path_set(object): | 48 class path_set(object): |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 148 |
| 138 def get_config_defaults(self): | 149 def get_config_defaults(self): |
| 139 return { | 150 return { |
| 140 'CURRENT_WORKING_DIR': self._startup_cwd, | 151 'CURRENT_WORKING_DIR': self._startup_cwd, |
| 141 'TEMP_DIR': self._temp_dir, | 152 'TEMP_DIR': self._temp_dir, |
| 142 } | 153 } |
| 143 | 154 |
| 144 def __init__(self, **kwargs): | 155 def __init__(self, **kwargs): |
| 145 super(PathApi, self).__init__(**kwargs) | 156 super(PathApi, self).__init__(**kwargs) |
| 146 recipe_config_types.Path.set_tostring_fn( | 157 recipe_config_types.Path.set_tostring_fn( |
| 147 PathTostring(self, self._test_data)) | 158 PathToString(self, self._test_data)) |
| 148 | 159 |
| 149 # Used in mkdtemp when generating and checking expectations. | 160 # Used in mkdtemp when generating and checking expectations. |
| 150 self._test_counter = 0 | 161 self._test_counter = 0 |
| 151 | 162 |
| 152 if not self._test_data.enabled: # pragma: no cover | 163 if not self._test_data.enabled: # pragma: no cover |
| 153 self._path_mod = os.path | 164 self._path_mod = os.path |
| 154 # Capture the cwd on process start to avoid shenanigans. | 165 # Capture the cwd on process start to avoid shenanigans. |
| 155 self._startup_cwd = _split_path(os.getcwd()) | 166 self._startup_cwd = _split_path(os.getcwd()) |
| 156 # Use default system wide temp dir as a root temp dir. | 167 # Use default system wide temp dir as a root temp dir. |
| 157 self._temp_dir = _split_path(tempfile.gettempdir()) | 168 self._temp_dir = _split_path(tempfile.gettempdir()) |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 @recipe_api.non_step | 297 @recipe_api.non_step |
| 287 def __contains__(self, pathname): | 298 def __contains__(self, pathname): |
| 288 return bool(self.c.dynamic_paths.get(pathname)) | 299 return bool(self.c.dynamic_paths.get(pathname)) |
| 289 | 300 |
| 290 @recipe_api.non_step | 301 @recipe_api.non_step |
| 291 def __setitem__(self, pathname, path): | 302 def __setitem__(self, pathname, path): |
| 292 assert isinstance(path, recipe_config_types.Path), ( | 303 assert isinstance(path, recipe_config_types.Path), ( |
| 293 'Setting dynamic path to something other than a Path: %r' % path) | 304 'Setting dynamic path to something other than a Path: %r' % path) |
| 294 assert pathname in self.c.dynamic_paths, ( | 305 assert pathname in self.c.dynamic_paths, ( |
| 295 'Must declare dynamic path (%r) in config before setting it.' % path) | 306 'Must declare dynamic path (%r) in config before setting it.' % path) |
| 296 assert path.base in self.c.base_paths, ( | 307 assert isinstance(path.base, recipe_config_types.BasePath), ( |
| 297 'Dynamic path values must be based on a base_path.') | 308 'Dynamic path values must be based on a base_path' % path.base) |
| 298 self.c.dynamic_paths[pathname] = path | 309 self.c.dynamic_paths[pathname] = path |
| 299 | 310 |
| 300 @recipe_api.non_step | 311 @recipe_api.non_step |
| 301 def __getitem__(self, name): | 312 def __getitem__(self, name): |
| 302 if name in self.c.dynamic_paths: | 313 if name in self.c.dynamic_paths: |
| 303 r = self.c.dynamic_paths[name] | 314 r = self.c.dynamic_paths[name] |
| 304 assert r is not None, ('Tried to get dynamic path %s but it has not been ' | 315 assert r is not None, ('Tried to get dynamic path %s but it has not been ' |
| 305 'set yet.' % name) | 316 'set yet.' % name) |
| 306 return r | 317 return r |
| 307 if name in self.c.base_paths: | 318 if name in self.c.base_paths: |
| 308 return recipe_config_types.Path(name, _bypass=True) | 319 return recipe_config_types.Path(recipe_config_types.NamedBasePath(name)) |
| 309 | 320 |
| 310 @recipe_api.non_step | 321 @recipe_api.non_step |
| 311 def __getattr__(self, name): | 322 def __getattr__(self, name): |
| 312 # retrieve os.path attributes | 323 # retrieve os.path attributes |
| 313 if name in self.OK_ATTRS: | 324 if name in self.OK_ATTRS: |
| 314 return getattr(self._path_mod, name) | 325 return getattr(self._path_mod, name) |
| 315 if name in self.FILTER_METHODS: | 326 if name in self.FILTER_METHODS: |
| 316 return string_filter(getattr(self._path_mod, name)) | 327 return string_filter(getattr(self._path_mod, name)) |
| 317 raise AttributeError("'%s' object has no attribute '%s'" % | 328 raise AttributeError("'%s' object has no attribute '%s'" % |
| 318 (self._path_mod, name)) # pragma: no cover | 329 (self._path_mod, name)) # pragma: no cover |
| 319 | 330 |
| 320 @recipe_api.non_step | 331 @recipe_api.non_step |
| 321 def __dir__(self): # pragma: no cover | 332 def __dir__(self): # pragma: no cover |
| 322 # Used for helping out show_me_the_modules.py | 333 # Used for helping out show_me_the_modules.py |
| 323 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) | 334 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) |
| OLD | NEW |