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

Unified Diff: recipe_modules/step/api.py

Issue 2668113002: step: expose get_context() to get the context object (Closed)
Patch Set: comments Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: recipe_modules/step/api.py
diff --git a/recipe_modules/step/api.py b/recipe_modules/step/api.py
index 8530f6ffdafcd8d71d8595732653e2a08a79fa7c..13075f93513b862f9f1275f26455ba159d2cb6ab 100644
--- a/recipe_modules/step/api.py
+++ b/recipe_modules/step/api.py
@@ -74,9 +74,33 @@ class StepApi(recipe_api.RecipeApiPlain):
@property
def context(self):
- """ See recipe_api.py for docs. """
+ """Returns a context manager which can set values applying to all steps
+ within the block.
+
+ Example usage:
+ with api.step.context({'cwd': api.path['checkout']}):
+ api.step(...)
+
+ Valid keys:
+ cwd (Path object from api.path): working directory
+ env ({name -> value}): environment variables
+ infra_step (bool): whether the step failure should be marked as infra
+ failure
+ name (str): step name prefix
+
+ See recipe_api.py for more info.
+ """
return recipe_api.context
+ def get_from_context(self, key, default=None):
+ """Returns |key|'s value from context if present, otherwise |default|."""
+ return recipe_api._STEP_CONTEXT.get(key, default)
+
+ def combine_with_context(self, key, value):
+ """Combines |value| with the value for |key| in current context, if any.
+ Returns the combined value."""
+ return recipe_api._STEP_CONTEXT.get_with_context(key, value)
+
@contextlib.contextmanager
def nest(self, name):
"""Nest is the high-level interface to annotated hierarchical steps.
@@ -171,9 +195,6 @@ class StepApi(recipe_api.RecipeApiPlain):
kwargs['timeout'] = timeout
kwargs['ok_ret'] = ok_ret
- # Obtain information from composite step parent.
- compositor = recipe_api._STEP_CONTEXT
-
# Calculate our full step name. If a step already has that name, add an
# index to the end of it.
#
@@ -181,7 +202,7 @@ class StepApi(recipe_api.RecipeApiPlain):
# by the user. If this happens, we'll continue appending indexes until we
# have a unique step name.
while True:
- full_name = compositor.get_with_context('name', name)
+ full_name = self.combine_with_context('name', name)
if full_name not in self._seen_steps:
break
@@ -191,11 +212,11 @@ class StepApi(recipe_api.RecipeApiPlain):
self._seen_steps.add(full_name)
if 'cwd' not in kwargs:
- kwargs['cwd'] = compositor.get('cwd')
- kwargs['env'] = compositor.get_with_context('env', kwargs.get('env', {}))
- kwargs['infra_step'] = compositor.get_with_context(
+ kwargs['cwd'] = self.get_from_context('cwd')
+ kwargs['env'] = self.combine_with_context('env', kwargs.get('env', {}))
+ kwargs['infra_step'] = self.combine_with_context(
'infra_step', bool(infra_step))
- kwargs['step_nest_level'] = compositor.get_with_context('nest_level', 0)
+ kwargs['step_nest_level'] = self.combine_with_context('nest_level', 0)
kwargs['name'] = full_name
kwargs['base_name'] = name
« 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