Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(315)

Side by Side Diff: recipe_modules/path/api.py

Issue 2918463002: [recipe_modules/file] add real file module to engine. (Closed)
Patch Set: rebase Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2013 The LUCI Authors. All rights reserved. 1 # Copyright 2013 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0 2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file. 3 # that can be found in the LICENSE file.
4 4
5 import functools 5 import functools
6 import itertools 6 import itertools
7 import os 7 import os
8 import re 8 import re
9 import tempfile 9 import tempfile
10 10
(...skipping 17 matching lines...) Expand all
28 def string_filter(func): 28 def string_filter(func):
29 @functools.wraps(func) 29 @functools.wraps(func)
30 def inner(*args, **kwargs): 30 def inner(*args, **kwargs):
31 return func(*map(str, args), **kwargs) 31 return func(*map(str, args), **kwargs)
32 return inner 32 return inner
33 33
34 34
35 class path_set(object): 35 class path_set(object):
36 """ implements a set which contains all the parents folders of added folders. 36 """ implements a set which contains all the parents folders of added folders.
37 """ 37 """
38 # TODO(iannucci): Expand this to be a full fakey filesystem, including file
39 # contents and file types. Coordinate with the `file` module.
38 def __init__(self, path_mod, initial_paths): 40 def __init__(self, path_mod, initial_paths):
39 self._path_mod = path_mod 41 self._path_mod = path_mod
40 self._initial_paths = set(initial_paths) 42 self._initial_paths = set(initial_paths)
41 self._paths = set() 43 self._paths = set()
42 44
43 def _initialize(self): 45 def _initialize(self):
44 self._initialize = lambda: None 46 self._initialize = lambda: None
45 for path in self._initial_paths: 47 for path in self._initial_paths:
46 self.add(path) 48 self.add(path)
47 self._initial_paths = None 49 self._initial_paths = None
48 self.contains = lambda path: path in self._paths 50 self.contains = lambda path: path in self._paths
49 51
50 def add(self, path): 52 def add(self, path):
51 path = str(path) 53 path = str(path)
52 self._initialize() 54 self._initialize()
53 while path: 55 while path:
54 self._paths.add(path) 56 self._paths.add(path)
55 path = self._path_mod.dirname(path) 57 path = self._path_mod.dirname(path)
56 58
59 def copy(self, source, dest):
60 source, dest = str(source), str(dest)
61 self._initialize()
62 to_add = set()
63 for p in self._paths:
64 if p.startswith(source):
65 to_add.add(p.replace(source, dest))
66 self._paths |= to_add
67
68 def remove(self, path, filt):
69 path = str(path)
70 self._initialize()
71 kill_set = set(p for p in self._paths if p.startswith(path) and filt(p))
72 self._paths -= kill_set
73
57 def contains(self, path): 74 def contains(self, path):
58 self._initialize() 75 self._initialize()
59 return self.contains(path) 76 return self.contains(path)
60 77
61 78
62 class fake_path(object): 79 class fake_path(object):
63 """Standin for os.path when we're in test mode. 80 """Standin for os.path when we're in test mode.
64 81
65 This class simulates the os.path interface exposed by PathApi, respecting the 82 This class simulates the os.path interface exposed by PathApi, respecting the
66 current platform according to the `platform` module. This allows us to 83 current platform according to the `platform` module. This allows us to
(...skipping 17 matching lines...) Expand all
84 def __getattr__(self, name): 101 def __getattr__(self, name):
85 self._init_pth() 102 self._init_pth()
86 return getattr(self._pth, name) 103 return getattr(self._pth, name)
87 104
88 def mock_add_paths(self, path): 105 def mock_add_paths(self, path):
89 """ 106 """
90 Adds a path and all of its parents to the set of existing paths. 107 Adds a path and all of its parents to the set of existing paths.
91 """ 108 """
92 self._mock_path_exists.add(path) 109 self._mock_path_exists.add(path)
93 110
111 def mock_copy_paths(self, source, dest):
112 """
113 Duplicates a path and all of its children to another path.
114 """
115 self._mock_path_exists.copy(source, dest)
116
117 def mock_remove_paths(self, path, filt):
118 """
119 Removes a path and all of its children from the set of existing paths.
120 """
121 self._mock_path_exists.remove(path, filt)
122
94 def exists(self, path): # pylint: disable=E0202 123 def exists(self, path): # pylint: disable=E0202
95 """Return True if path refers to an existing path.""" 124 """Return True if path refers to an existing path."""
96 return self._mock_path_exists.contains(path) 125 return self._mock_path_exists.contains(path)
97 126
98 # This matches: 127 # This matches:
99 # [START_DIR] 128 # [START_DIR]
100 # RECIPE[some_pkg::some_module:recipe_name] 129 # RECIPE[some_pkg::some_module:recipe_name]
101 # 130 #
102 # and friends at the beginning of a string. 131 # and friends at the beginning of a string.
103 ROOT_MATCHER = re.compile('^[A-Z_]*\[[^]]*\]') 132 ROOT_MATCHER = re.compile('^[A-Z_]*\[[^]]*\]')
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 self._temp_dir = ['/'] 262 self._temp_dir = ['/']
234 self._cache_dir = ['/', 'b', 'c'] 263 self._cache_dir = ['/', 'b', 'c']
235 264
236 self.set_config('BASE') 265 self.set_config('BASE')
237 266
238 def mock_add_paths(self, path): 267 def mock_add_paths(self, path):
239 """For testing purposes, assert that |path| exists.""" 268 """For testing purposes, assert that |path| exists."""
240 if self._test_data.enabled: 269 if self._test_data.enabled:
241 self._path_mod.mock_add_paths(path) 270 self._path_mod.mock_add_paths(path)
242 271
272 def mock_copy_paths(self, source, dest):
273 """For testing purposes, copy |source| to |dest|."""
274 if self._test_data.enabled:
275 self._path_mod.mock_copy_paths(source, dest)
276
277 def mock_remove_paths(self, path, filt=lambda p: True):
278 """For testing purposes, assert that |path| doesn't exist.
279
280 Args:
281 path (str|Path) - The path to remove.
282 filt (func[str] bool) - Called for every candidate path. Return
283 True to remove this path.
284 """
285 if self._test_data.enabled:
286 self._path_mod.mock_remove_paths(path, filt)
287
243 def assert_absolute(self, path): 288 def assert_absolute(self, path):
244 assert self.abspath(path) == str(path), '%s is not absolute' % path 289 assert self.abspath(path) == str(path), '%s is not absolute' % path
245 290
246 def mkdtemp(self, prefix): 291 def mkdtemp(self, prefix):
247 """Makes a new temp directory, returns path to it.""" 292 """Makes a new temp directory, returns path to it."""
248 if not self._test_data.enabled: # pragma: no cover 293 if not self._test_data.enabled: # pragma: no cover
249 # New path as str. 294 # New path as str.
250 new_path = tempfile.mkdtemp(prefix=prefix, dir=str(self['tmp_base'])) 295 new_path = tempfile.mkdtemp(prefix=prefix, dir=str(self['tmp_base']))
251 # Ensure it's under self._temp_dir, convert to Path. 296 # Ensure it's under self._temp_dir, convert to Path.
252 new_path = _split_path(new_path) 297 new_path = _split_path(new_path)
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 if name in self.OK_ATTRS: 377 if name in self.OK_ATTRS:
333 return getattr(self._path_mod, name) 378 return getattr(self._path_mod, name)
334 if name in self.FILTER_METHODS: 379 if name in self.FILTER_METHODS:
335 return string_filter(getattr(self._path_mod, name)) 380 return string_filter(getattr(self._path_mod, name))
336 raise AttributeError("'%s' object has no attribute '%s'" % 381 raise AttributeError("'%s' object has no attribute '%s'" %
337 (self._path_mod, name)) # pragma: no cover 382 (self._path_mod, name)) # pragma: no cover
338 383
339 def __dir__(self): # pragma: no cover 384 def __dir__(self): # pragma: no cover
340 # Used for helping out show_me_the_modules.py 385 # Used for helping out show_me_the_modules.py
341 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS) 386 return self.__dict__.keys() + list(self.OK_ATTRS + self.FILTER_METHODS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698