| 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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 def _split_path(path): # pragma: no cover | 118 def _split_path(path): # pragma: no cover |
| 119 """Relative or absolute path -> tuple of components.""" | 119 """Relative or absolute path -> tuple of components.""" |
| 120 abs_path = os.path.abspath(path).split(os.path.sep) | 120 abs_path = os.path.abspath(path).split(os.path.sep) |
| 121 # Guarantee that the first element is an absolute drive or the posix root. | 121 # Guarantee that the first element is an absolute drive or the posix root. |
| 122 if abs_path[0].endswith(':'): | 122 if abs_path[0].endswith(':'): |
| 123 abs_path[0] += '\\' | 123 abs_path[0] += '\\' |
| 124 elif abs_path[0] == '': | 124 elif abs_path[0] == '': |
| 125 abs_path[0] = '/' | 125 abs_path[0] = '/' |
| 126 else: | 126 else: |
| 127 assert False, 'Got unexpected path format: %r' % abs_path | 127 assert False, 'Got unexpected path format: %r' % abs_path |
| 128 return abs_path | 128 return tuple(abs_path) |
| 129 | 129 |
| 130 | 130 |
| 131 class PathApi(recipe_api.RecipeApi): | 131 class PathApi(recipe_api.RecipeApi): |
| 132 """ | 132 """ |
| 133 PathApi provides common os.path functions as well as convenience functions | 133 PathApi provides common os.path functions as well as convenience functions |
| 134 for generating absolute paths to things in a testable way. | 134 for generating absolute paths to things in a testable way. |
| 135 | 135 |
| 136 Mocks: | 136 Mocks: |
| 137 exists (list): Paths which should exist in the test case. Thes must be paths | 137 exists (list): Paths which should exist in the test case. Thes must be paths |
| 138 using the [*_ROOT] placeholders. ex. '[BUILD_ROOT]/scripts'. | 138 using the [*_ROOT] placeholders. ex. '[BUILD_ROOT]/scripts'. |
| 139 """ | 139 """ |
| 140 | 140 |
| 141 OK_ATTRS = ('pardir', 'sep', 'pathsep') | 141 OK_ATTRS = ('pardir', 'sep', 'pathsep') |
| 142 | 142 |
| 143 # Because the native 'path' type in python is a str, we filter the *args | 143 # Because the native 'path' type in python is a str, we filter the *args |
| 144 # of these methods to stringify them first (otherwise they would be getting | 144 # of these methods to stringify them first (otherwise they would be getting |
| 145 # recipe_util_types.Path instances). | 145 # recipe_util_types.Path instances). |
| 146 FILTER_METHODS = ('abspath', 'basename', 'exists', 'join', 'split', | 146 FILTER_METHODS = ('abspath', 'basename', 'exists', 'join', 'split', |
| 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 'BASE_PATH_OVERRIDES': { |
| 154 k: _split_path(v) |
| 155 for k, v in |
| 156 self._engine.properties.get('base_path_overrides', {}).iteritems() |
| 157 }, |
| 153 } | 158 } |
| 154 | 159 |
| 155 def __init__(self, **kwargs): | 160 def __init__(self, **kwargs): |
| 156 super(PathApi, self).__init__(**kwargs) | 161 super(PathApi, self).__init__(**kwargs) |
| 157 config_types.Path.set_tostring_fn( | 162 config_types.Path.set_tostring_fn( |
| 158 PathToString(self, self._test_data)) | 163 PathToString(self, self._test_data)) |
| 159 | 164 |
| 160 # Used in mkdtemp when generating and checking expectations. | 165 # Used in mkdtemp when generating and checking expectations. |
| 161 self._test_counter = 0 | 166 self._test_counter = 0 |
| 162 | 167 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 if name in self.OK_ATTRS: | 235 if name in self.OK_ATTRS: |
| 231 return getattr(self._path_mod, name) | 236 return getattr(self._path_mod, name) |
| 232 if name in self.FILTER_METHODS: | 237 if name in self.FILTER_METHODS: |
| 233 return string_filter(getattr(self._path_mod, name)) | 238 return string_filter(getattr(self._path_mod, name)) |
| 234 raise AttributeError("'%s' object has no attribute '%s'" % | 239 raise AttributeError("'%s' object has no attribute '%s'" % |
| 235 (self._path_mod, name)) # pragma: no cover | 240 (self._path_mod, name)) # pragma: no cover |
| 236 | 241 |
| 237 def __dir__(self): # pragma: no cover | 242 def __dir__(self): # pragma: no cover |
| 238 # Used for helping out show_me_the_modules.py | 243 # Used for helping out show_me_the_modules.py |
| 239 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) | 244 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) |
| OLD | NEW |