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 slave import recipe_api |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 @recipe_api.composite_step | 178 @recipe_api.composite_step |
179 def mock_add_paths(self, path): | 179 def mock_add_paths(self, path): |
180 """For testing purposes, assert that |path| exists.""" | 180 """For testing purposes, assert that |path| exists.""" |
181 if self._test_data.enabled: | 181 if self._test_data.enabled: |
182 self._path_mod.mock_add_paths(path) | 182 self._path_mod.mock_add_paths(path) |
183 | 183 |
184 @recipe_api.composite_step | 184 @recipe_api.composite_step |
185 def assert_absolute(self, path): | 185 def assert_absolute(self, path): |
186 assert self.abspath(path) == str(path), '%s is not absolute' % path | 186 assert self.abspath(path) == str(path), '%s is not absolute' % path |
187 | 187 |
188 def listdir(self, name, path, step_test_data=None): | |
189 """Wrapper for os.listdir.""" | |
190 return self.m.python.inline('listdir %s' % name, | |
191 """ | |
192 import json, os, sys | |
193 if os.path.exists(sys.argv[1]) and os.path.isdir(sys.argv[1]): | |
194 with open(sys.argv[2], 'w') as f: | |
195 json.dump(os.listdir(sys.argv[1]), f) | |
196 """, | |
197 args=[path, self.m.json.output()], | |
198 step_test_data=(step_test_data or | |
199 self.test_api.listdir(['file 1', 'file 2'])), | |
200 ).json.output | |
201 | |
202 def makedirs(self, name, path, mode=0777): | |
203 """ | |
204 Like os.makedirs, except that if the directory exists, then there is no | |
205 error. | |
206 """ | |
207 self.assert_absolute(path) | |
208 self.m.python.inline( | |
209 'makedirs ' + name, | |
210 """ | |
211 import sys, os | |
212 path = sys.argv[1] | |
213 mode = int(sys.argv[2]) | |
214 if not os.path.isdir(path): | |
215 if os.path.exists(path): | |
216 print "%s exists but is not a dir" % path | |
217 sys.exit(1) | |
218 os.makedirs(path, mode) | |
219 """, | |
220 args=[path, str(mode)], | |
221 ) | |
222 self.mock_add_paths(path) | |
223 | |
224 def rmtree(self, name, path): | |
225 """Wrapper for chromium_utils.RemoveDirectory.""" | |
226 self.assert_absolute(path) | |
227 self.m.python.inline( | |
228 'rmtree ' + name, | |
229 """ | |
230 import os, sys | |
231 from common import chromium_utils | |
232 | |
233 if os.path.exists(sys.argv[1]): | |
234 chromium_utils.RemoveDirectory(sys.argv[1]) | |
235 """, | |
236 args=[path], | |
237 ) | |
238 | |
239 def rmcontents(self, name, path): | |
240 """ | |
241 Similar to rmtree, but removes only contents not the directory. | |
242 | |
243 This is useful e.g. when removing contents of current working directory. | |
244 Deleting current working directory makes all further getcwd calls fail | |
245 until chdir is called. chdir would be tricky in recipes, so we provide | |
246 a call that doesn't delete the directory itself. | |
247 """ | |
248 self.assert_absolute(path) | |
249 self.m.python.inline( | |
250 'rmcontents ' + name, | |
251 """ | |
252 import os, sys | |
253 from common import chromium_utils | |
254 | |
255 for p in [os.path.join(sys.argv[1], x) for x in os.listdir(sys.argv[1])]: | |
256 if os.path.isdir(p): | |
257 chromium_utils.RemoveDirectory(p) | |
258 else: | |
259 os.unlink(p) | |
260 """, | |
261 args=[path], | |
262 ) | |
263 | |
264 def rmwildcard(self, pattern, path, **kwargs): | |
265 """ | |
266 Removes all files in the subtree of path matching the glob pattern. | |
267 """ | |
268 self.assert_absolute(path) | |
269 self.m.python.inline( | |
270 'rmwildcard %s in %s' % (pattern, path), | |
271 """ | |
272 import sys | |
273 from common import chromium_utils | |
274 | |
275 chromium_utils.RemoveFilesWildcards(sys.argv[1], root=sys.argv[2]) | |
276 """, | |
277 args=[pattern,path], | |
278 **kwargs) | |
279 | |
280 @recipe_api.non_step | 188 @recipe_api.non_step |
281 def mkdtemp(self, prefix): | 189 def mkdtemp(self, prefix): |
282 """Makes a new temp directory, returns path to it.""" | 190 """Makes a new temp directory, returns path to it.""" |
283 if not self._test_data.enabled: # pragma: no cover | 191 if not self._test_data.enabled: # pragma: no cover |
284 # New path as str. | 192 # New path as str. |
285 new_path = tempfile.mkdtemp(prefix=prefix, dir=str(self['tmp_base'])) | 193 new_path = tempfile.mkdtemp(prefix=prefix, dir=str(self['tmp_base'])) |
286 # Ensure it's under self._temp_dir, convert to Path. | 194 # Ensure it's under self._temp_dir, convert to Path. |
287 new_path = _split_path(new_path) | 195 new_path = _split_path(new_path) |
288 assert new_path[:len(self._temp_dir)] == self._temp_dir | 196 assert new_path[:len(self._temp_dir)] == self._temp_dir |
289 temp_dir = self['tmp_base'].join(*new_path[len(self._temp_dir):]) | 197 temp_dir = self['tmp_base'].join(*new_path[len(self._temp_dir):]) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 return getattr(self._path_mod, name) | 233 return getattr(self._path_mod, name) |
326 if name in self.FILTER_METHODS: | 234 if name in self.FILTER_METHODS: |
327 return string_filter(getattr(self._path_mod, name)) | 235 return string_filter(getattr(self._path_mod, name)) |
328 raise AttributeError("'%s' object has no attribute '%s'" % | 236 raise AttributeError("'%s' object has no attribute '%s'" % |
329 (self._path_mod, name)) # pragma: no cover | 237 (self._path_mod, name)) # pragma: no cover |
330 | 238 |
331 @recipe_api.non_step | 239 @recipe_api.non_step |
332 def __dir__(self): # pragma: no cover | 240 def __dir__(self): # pragma: no cover |
333 # Used for helping out show_me_the_modules.py | 241 # Used for helping out show_me_the_modules.py |
334 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) | 242 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) |
OLD | NEW |