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

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

Issue 2668113002: step: expose get_context() to get the context object (Closed)
Patch Set: comments Created 3 years, 10 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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
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
84 Valid keys:
85 cwd (Path object from api.path): working directory
86 env ({name -> value}): environment variables
87 infra_step (bool): whether the step failure should be marked as infra
88 failure
89 name (str): step name prefix
90
91 See recipe_api.py for more info.
92 """
78 return recipe_api.context 93 return recipe_api.context
79 94
95 def get_from_context(self, key, default=None):
96 """Returns |key|'s value from context if present, otherwise |default|."""
97 return recipe_api._STEP_CONTEXT.get(key, default)
98
99 def combine_with_context(self, key, value):
100 """Combines |value| with the value for |key| in current context, if any.
101 Returns the combined value."""
102 return recipe_api._STEP_CONTEXT.get_with_context(key, value)
103
80 @contextlib.contextmanager 104 @contextlib.contextmanager
81 def nest(self, name): 105 def nest(self, name):
82 """Nest is the high-level interface to annotated hierarchical steps. 106 """Nest is the high-level interface to annotated hierarchical steps.
83 107
84 Calling 108 Calling
85 109
86 with api.step.nest(<name>): 110 with api.step.nest(<name>):
87 ... 111 ...
88 112
89 will generate a dummy step and implicitly create a new context (as 113 will generate a dummy step and implicitly create a new context (as
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 ok_ret = set(range(-256, 256)) 188 ok_ret = set(range(-256, 256))
165 189
166 if cmd is not None: 190 if cmd is not None:
167 command = list(wrapper) 191 command = list(wrapper)
168 command += cmd 192 command += cmd
169 kwargs['cmd'] = command 193 kwargs['cmd'] = command
170 194
171 kwargs['timeout'] = timeout 195 kwargs['timeout'] = timeout
172 kwargs['ok_ret'] = ok_ret 196 kwargs['ok_ret'] = ok_ret
173 197
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 198 # Calculate our full step name. If a step already has that name, add an
178 # index to the end of it. 199 # index to the end of it.
179 # 200 #
180 # Note that another step could exist with that index already added to it 201 # 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 202 # by the user. If this happens, we'll continue appending indexes until we
182 # have a unique step name. 203 # have a unique step name.
183 while True: 204 while True:
184 full_name = compositor.get_with_context('name', name) 205 full_name = self.combine_with_context('name', name)
185 if full_name not in self._seen_steps: 206 if full_name not in self._seen_steps:
186 break 207 break
187 208
188 step_count = self._step_names.setdefault(full_name, 1) + 1 209 step_count = self._step_names.setdefault(full_name, 1) + 1
189 self._step_names[full_name] = step_count 210 self._step_names[full_name] = step_count
190 name = "%s (%d)" % (name, step_count) 211 name = "%s (%d)" % (name, step_count)
191 self._seen_steps.add(full_name) 212 self._seen_steps.add(full_name)
192 213
193 if 'cwd' not in kwargs: 214 if 'cwd' not in kwargs:
194 kwargs['cwd'] = compositor.get('cwd') 215 kwargs['cwd'] = self.get_from_context('cwd')
195 kwargs['env'] = compositor.get_with_context('env', kwargs.get('env', {})) 216 kwargs['env'] = self.combine_with_context('env', kwargs.get('env', {}))
196 kwargs['infra_step'] = compositor.get_with_context( 217 kwargs['infra_step'] = self.combine_with_context(
197 'infra_step', bool(infra_step)) 218 'infra_step', bool(infra_step))
198 kwargs['step_nest_level'] = compositor.get_with_context('nest_level', 0) 219 kwargs['step_nest_level'] = self.combine_with_context('nest_level', 0)
199 kwargs['name'] = full_name 220 kwargs['name'] = full_name
200 kwargs['base_name'] = name 221 kwargs['base_name'] = name
201 222
202 schema = self.make_config() 223 schema = self.make_config()
203 schema.set_val(kwargs) 224 schema.set_val(kwargs)
204 return self.run_from_dict(schema.as_jsonish()) 225 return self.run_from_dict(schema.as_jsonish())
205 226
206 # TODO(martiniss) delete, and make generator_script use **kwargs on step() 227 # TODO(martiniss) delete, and make generator_script use **kwargs on step()
207 @recipe_api.composite_step 228 @recipe_api.composite_step
208 def run_from_dict(self, dct): 229 def run_from_dict(self, dct):
209 return self.step_client.run_step(dct) 230 return self.step_client.run_step(dct)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698