Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 contextlib | 5 import contextlib |
| 6 import datetime | 6 import datetime |
| 7 | 7 |
| 8 from recipe_engine import recipe_api | 8 from recipe_engine import recipe_api |
| 9 | 9 |
| 10 | 10 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 raise | 67 raise |
| 68 finally: | 68 finally: |
| 69 if result.json.output: | 69 if result.json.output: |
| 70 new_step_text = result.json.output['step_text'] | 70 new_step_text = result.json.output['step_text'] |
| 71 api.step.active_result.presentation.step_text = new_step_text | 71 api.step.active_result.presentation.step_text = new_step_text |
| 72 """ | 72 """ |
| 73 return self.step_client.previous_step_result() | 73 return self.step_client.previous_step_result() |
| 74 | 74 |
| 75 @property | 75 @property |
| 76 def context(self): | 76 def context(self): |
| 77 """ See recipe_api.py for docs. """ | 77 """Returns a context manager which can set values applying to all steps |
| 78 within the block. | |
| 79 | |
| 80 Example usage: | |
| 81 with api.step.context({'cwd': api.path['checkout']}): | |
| 82 api.step(...) | |
| 83 """ | |
| 78 return recipe_api.context | 84 return recipe_api.context |
| 79 | 85 |
| 86 def get_from_context(self, key, default=None): | |
| 87 """Returns |key| from context if present, otherwise |default|.""" | |
|
iannucci
2017/02/01 17:47:00
Returns |key|'s value from context...
Paweł Hajdan Jr.
2017/02/01 17:50:25
Done.
| |
| 88 return recipe_api._STEP_CONTEXT.get(key, default) | |
| 89 | |
| 90 def combine_with_context(self, key, value): | |
| 91 """Combines |value| with the value for |key| in current context, if any.""" | |
|
iannucci
2017/02/01 17:47:00
... and returns the combined value.
It would be r
Paweł Hajdan Jr.
2017/02/01 17:50:25
Done.
| |
| 92 return recipe_api._STEP_CONTEXT.get_with_context(key, value) | |
| 93 | |
| 80 @contextlib.contextmanager | 94 @contextlib.contextmanager |
| 81 def nest(self, name): | 95 def nest(self, name): |
| 82 """Nest is the high-level interface to annotated hierarchical steps. | 96 """Nest is the high-level interface to annotated hierarchical steps. |
| 83 | 97 |
| 84 Calling | 98 Calling |
| 85 | 99 |
| 86 with api.step.nest(<name>): | 100 with api.step.nest(<name>): |
| 87 ... | 101 ... |
| 88 | 102 |
| 89 will generate a dummy step and implicitly create a new context (as | 103 will generate a dummy step and implicitly create a new context (as |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 ok_ret = set(range(-256, 256)) | 178 ok_ret = set(range(-256, 256)) |
| 165 | 179 |
| 166 if cmd is not None: | 180 if cmd is not None: |
| 167 command = list(wrapper) | 181 command = list(wrapper) |
| 168 command += cmd | 182 command += cmd |
| 169 kwargs['cmd'] = command | 183 kwargs['cmd'] = command |
| 170 | 184 |
| 171 kwargs['timeout'] = timeout | 185 kwargs['timeout'] = timeout |
| 172 kwargs['ok_ret'] = ok_ret | 186 kwargs['ok_ret'] = ok_ret |
| 173 | 187 |
| 174 # Obtain information from composite step parent. | |
| 175 compositor = recipe_api._STEP_CONTEXT | |
| 176 | |
| 177 # Calculate our full step name. If a step already has that name, add an | 188 # Calculate our full step name. If a step already has that name, add an |
| 178 # index to the end of it. | 189 # index to the end of it. |
| 179 # | 190 # |
| 180 # Note that another step could exist with that index already added to it | 191 # Note that another step could exist with that index already added to it |
| 181 # by the user. If this happens, we'll continue appending indexes until we | 192 # by the user. If this happens, we'll continue appending indexes until we |
| 182 # have a unique step name. | 193 # have a unique step name. |
| 183 while True: | 194 while True: |
| 184 full_name = compositor.get_with_context('name', name) | 195 full_name = self.combine_with_context('name', name) |
|
iannucci
2017/02/01 17:47:00
ew, I forgot about name. this is used to implement
Paweł Hajdan Jr.
2017/02/01 17:50:25
Yes, see api.step.nest .
| |
| 185 if full_name not in self._seen_steps: | 196 if full_name not in self._seen_steps: |
| 186 break | 197 break |
| 187 | 198 |
| 188 step_count = self._step_names.setdefault(full_name, 1) + 1 | 199 step_count = self._step_names.setdefault(full_name, 1) + 1 |
| 189 self._step_names[full_name] = step_count | 200 self._step_names[full_name] = step_count |
| 190 name = "%s (%d)" % (name, step_count) | 201 name = "%s (%d)" % (name, step_count) |
| 191 self._seen_steps.add(full_name) | 202 self._seen_steps.add(full_name) |
| 192 | 203 |
| 193 if 'cwd' not in kwargs: | 204 if 'cwd' not in kwargs: |
| 194 kwargs['cwd'] = compositor.get('cwd') | 205 kwargs['cwd'] = self.get_from_context('cwd') |
| 195 kwargs['env'] = compositor.get_with_context('env', kwargs.get('env', {})) | 206 kwargs['env'] = self.combine_with_context('env', kwargs.get('env', {})) |
| 196 kwargs['infra_step'] = compositor.get_with_context( | 207 kwargs['infra_step'] = self.combine_with_context( |
| 197 'infra_step', bool(infra_step)) | 208 'infra_step', bool(infra_step)) |
| 198 kwargs['step_nest_level'] = compositor.get_with_context('nest_level', 0) | 209 kwargs['step_nest_level'] = self.combine_with_context('nest_level', 0) |
| 199 kwargs['name'] = full_name | 210 kwargs['name'] = full_name |
| 200 kwargs['base_name'] = name | 211 kwargs['base_name'] = name |
| 201 | 212 |
| 202 schema = self.make_config() | 213 schema = self.make_config() |
| 203 schema.set_val(kwargs) | 214 schema.set_val(kwargs) |
| 204 return self.run_from_dict(schema.as_jsonish()) | 215 return self.run_from_dict(schema.as_jsonish()) |
| 205 | 216 |
| 206 # TODO(martiniss) delete, and make generator_script use **kwargs on step() | 217 # TODO(martiniss) delete, and make generator_script use **kwargs on step() |
| 207 @recipe_api.composite_step | 218 @recipe_api.composite_step |
| 208 def run_from_dict(self, dct): | 219 def run_from_dict(self, dct): |
| 209 return self.step_client.run_step(dct) | 220 return self.step_client.run_step(dct) |
| OLD | NEW |