OLD | NEW |
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 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 """This module holds utilities which make writing recipes easier.""" | 5 """This module holds utilities which make writing recipes easier.""" |
6 | 6 |
| 7 import contextlib as _contextlib |
7 import os as _os | 8 import os as _os |
8 | 9 |
9 # e.g. /b/build/slave/<slave-name>/build | 10 # e.g. /b/build/slave/<slave-name>/build |
10 SLAVE_BUILD_ROOT = _os.path.abspath(_os.getcwd()) | 11 SLAVE_BUILD_ROOT = _os.path.abspath(_os.getcwd()) |
11 # e.g. /b | 12 # e.g. /b |
12 ROOT = _os.path.abspath(_os.path.join(SLAVE_BUILD_ROOT, _os.pardir, _os.pardir, | 13 ROOT = _os.path.abspath(_os.path.join(SLAVE_BUILD_ROOT, _os.pardir, _os.pardir, |
13 _os.pardir, _os.pardir)) | 14 _os.pardir, _os.pardir)) |
14 # e.g. /b/build_internal | 15 # e.g. /b/build_internal |
15 BUILD_INTERNAL_ROOT = _os.path.join(ROOT, 'build_internal') | 16 BUILD_INTERNAL_ROOT = _os.path.join(ROOT, 'build_internal') |
16 # e.g. /b/build | 17 # e.g. /b/build |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 The actual checkout root is filled in by annotated_run after the recipe | 51 The actual checkout root is filled in by annotated_run after the recipe |
51 completes, and is dependent on the implementation of 'root()' in | 52 completes, and is dependent on the implementation of 'root()' in |
52 annotated_checkout for the checkout type that you've selected. | 53 annotated_checkout for the checkout type that you've selected. |
53 | 54 |
54 NOTE: In order for this function to work, your recipe MUST use the 'checkout' | 55 NOTE: In order for this function to work, your recipe MUST use the 'checkout' |
55 functionality provided by annotated_run. | 56 functionality provided by annotated_run. |
56 """ # pylint: disable=W0105 | 57 """ # pylint: disable=W0105 |
57 checkout_path = _path_method('checkout_path', "%(CheckoutRootPlaceholder)s") | 58 checkout_path = _path_method('checkout_path', "%(CheckoutRootPlaceholder)s") |
58 | 59 |
59 | 60 |
| 61 @_contextlib.contextmanager |
| 62 def mock_paths(): |
| 63 path_base_names = ['depot_tools', 'build_internal', 'build', 'slave_build', |
| 64 'root'] |
| 65 g = globals() |
| 66 tokens = {} |
| 67 path_funcs = {} |
| 68 try: |
| 69 for name in path_base_names: |
| 70 token_name = (name+"_root").upper() |
| 71 token_val = '[%s]' % token_name |
| 72 path_func_name = (name+"_path") |
| 73 |
| 74 if token_name in g: |
| 75 tokens[token_name] = g[token_name] |
| 76 g[token_name] = token_val |
| 77 |
| 78 if path_func_name in g: |
| 79 path_funcs[path_func_name] = g[path_func_name] |
| 80 g[path_func_name] = _path_method(path_func_name, token_val) |
| 81 yield |
| 82 finally: |
| 83 g.update(tokens) |
| 84 g.update(path_funcs) |
| 85 |
| 86 |
60 def deep_set(obj, key_vals): | 87 def deep_set(obj, key_vals): |
61 """Take an object (a dict or list), and a list of key/value pairs to set, | 88 """Take an object (a dict or list), and a list of key/value pairs to set, |
62 and transform it by replacing items in obj at the key locations with the | 89 and transform it by replacing items in obj at the key locations with the |
63 respective values. | 90 respective values. |
64 | 91 |
65 keys are strings in the form of: (str|int)[.(str|int)]* | 92 keys are strings in the form of: (str|int)[.(str|int)]* |
66 | 93 |
67 Example: | 94 Example: |
68 obj = {'some': {'deep': {'list': [1, 2, 3, 4, 5, 6]}}} | 95 obj = {'some': {'deep': {'list': [1, 2, 3, 4, 5, 6]}}} |
69 key_vals = [("some.deep.list.3", 'foobar')] | 96 key_vals = [("some.deep.list.3", 'foobar')] |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 '-e', 'commit-bot@chromium.org']) | 283 '-e', 'commit-bot@chromium.org']) |
257 | 284 |
258 def git_step(self, *args): | 285 def git_step(self, *args): |
259 name = 'git '+args[0] | 286 name = 'git '+args[0] |
260 # Distinguish 'git config' commands by the variable they are setting. | 287 # Distinguish 'git config' commands by the variable they are setting. |
261 if args[0] == 'config' and not args[1].startswith('-'): | 288 if args[0] == 'config' and not args[1].startswith('-'): |
262 name += " "+args[1] | 289 name += " "+args[1] |
263 return self.step(name, [ | 290 return self.step(name, [ |
264 'git', '--work-tree', checkout_path(), | 291 'git', '--work-tree', checkout_path(), |
265 '--git-dir', checkout_path('.git')]+list(args)) | 292 '--git-dir', checkout_path('.git')]+list(args)) |
OLD | NEW |