From 5702f0baefc0c1321374d838f8fc27c18d79a2ae Mon Sep 17 00:00:00 2001 From: Robert Iannucci Date: Wed, 10 May 2017 23:23:46 -0700 Subject: [PATCH] [recipes] convert all recipe_modules to use new context module. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R=recipe-roller@chromium.org Bug: Change-Id: I84c19965b779f1010c00cd8c857b7533eec7413f Reviewed-on: https://chromium-review.googlesource.com/502929 Reviewed-by: Paweł Hajdan Jr. Commit-Queue: Robbie Iannucci --- recipes/recipe_modules/bot_update/__init__.py | 3 ++- recipes/recipe_modules/bot_update/api.py | 12 ++++++------ recipes/recipe_modules/gclient/__init__.py | 1 + recipes/recipe_modules/gclient/api.py | 12 ++++-------- recipes/recipe_modules/gclient/example.py | 4 ++-- recipes/recipe_modules/gerrit/__init__.py | 1 + recipes/recipe_modules/gerrit/api.py | 4 ++-- recipes/recipe_modules/git/__init__.py | 1 + recipes/recipe_modules/git/api.py | 19 ++++++++----------- recipes/recipe_modules/git/example.py | 3 ++- recipes/recipe_modules/git_cl/__init__.py | 1 + recipes/recipe_modules/git_cl/api.py | 5 ++--- recipes/recipe_modules/presubmit/__init__.py | 1 + recipes/recipe_modules/presubmit/api.py | 4 ++-- recipes/recipe_modules/tryserver/__init__.py | 3 ++- recipes/recipe_modules/tryserver/api.py | 15 +++++++-------- 16 files changed, 44 insertions(+), 45 deletions(-) diff --git a/recipes/recipe_modules/bot_update/__init__.py b/recipes/recipe_modules/bot_update/__init__.py index 86e7ea3500..cf1a672311 100644 --- a/recipes/recipe_modules/bot_update/__init__.py +++ b/recipes/recipe_modules/bot_update/__init__.py @@ -1,13 +1,14 @@ DEPS = [ 'gclient', + 'recipe_engine/context', 'recipe_engine/json', 'recipe_engine/path', 'recipe_engine/platform', 'recipe_engine/properties', 'recipe_engine/python', 'recipe_engine/raw_io', - 'rietveld', 'recipe_engine/step', + 'rietveld', 'tryserver', ] diff --git a/recipes/recipe_modules/bot_update/api.py b/recipes/recipe_modules/bot_update/api.py index f9db836b64..dc486e1dc7 100644 --- a/recipes/recipe_modules/bot_update/api.py +++ b/recipes/recipe_modules/bot_update/api.py @@ -33,7 +33,7 @@ class BotUpdateApi(recipe_api.RecipeApi): assert isinstance(cmd, (list, tuple)) bot_update_path = self.resource('bot_update.py') kwargs.setdefault('infra_step', True) - env = self.m.step.get_from_context('env', {}) + env = self.m.context.env env.setdefault('PATH', '%(PATH)s') env['PATH'] = self.m.path.pathsep.join([ env['PATH'], str(self._module.PACKAGE_REPO_ROOT)]) @@ -42,7 +42,7 @@ class BotUpdateApi(recipe_api.RecipeApi): # and die with an error "error: RPC failed; curl 28 Operation too slow" env['GIT_HTTP_LOW_SPEED_LIMIT'] = 1000 env['GIT_HTTP_LOW_SPEED_TIME'] = 300 - with self.m.step.context({'env': env}): + with self.m.context(env=env): return self.m.python(name, bot_update_path, cmd, **kwargs) @property @@ -55,7 +55,7 @@ class BotUpdateApi(recipe_api.RecipeApi): gerrit_no_rebase_patch_ref=False, **kwargs): apply_gerrit_path = self.resource('apply_gerrit.py') kwargs.setdefault('infra_step', True) - env = self.m.step.get_from_context('env', {}) + env = self.m.context.env env.setdefault('PATH', '%(PATH)s') env['PATH'] = self.m.path.pathsep.join([ env['PATH'], str(self._module.PACKAGE_REPO_ROOT)]) @@ -68,7 +68,7 @@ class BotUpdateApi(recipe_api.RecipeApi): cmd.append('--gerrit_no_reset') if gerrit_no_rebase_patch_ref: cmd.append('--gerrit_no_rebase_patch_ref') - with self.m.step.context({'env': env}): + with self.m.context(env=env): return self.m.python('apply_gerrit', apply_gerrit_path, cmd, **kwargs) def ensure_checkout(self, gclient_config=None, suffix=None, @@ -291,7 +291,7 @@ class BotUpdateApi(recipe_api.RecipeApi): try: if return_code == 3: # This is download failure, hence an infra failure. - with self.m.step.context({'infra_step': True}): + with self.m.context(infra_steps=True): self.m.python.failing_step( 'Patch failure', 'Git reported a download failure') else: @@ -308,7 +308,7 @@ class BotUpdateApi(recipe_api.RecipeApi): # first solution. if result['did_run']: co_root = result['root'] - cwd = self.m.step.get_from_context('cwd', self.m.path['start_dir']) + cwd = self.m.context.cwd or self.m.path['start_dir'] if 'checkout' not in self.m.path: self.m.path['checkout'] = cwd.join(*co_root.split(self.m.path.sep)) diff --git a/recipes/recipe_modules/gclient/__init__.py b/recipes/recipe_modules/gclient/__init__.py index b9e022d85a..ba9314152e 100644 --- a/recipes/recipe_modules/gclient/__init__.py +++ b/recipes/recipe_modules/gclient/__init__.py @@ -1,5 +1,6 @@ DEPS = [ 'infra_paths', + 'recipe_engine/context', 'recipe_engine/json', 'recipe_engine/path', 'recipe_engine/platform', diff --git a/recipes/recipe_modules/gclient/api.py b/recipes/recipe_modules/gclient/api.py index 9447305a38..a8424da711 100644 --- a/recipes/recipe_modules/gclient/api.py +++ b/recipes/recipe_modules/gclient/api.py @@ -80,12 +80,12 @@ class GclientApi(recipe_api.RecipeApi): prefix = ('[spec: %s] ' % self.spec_alias) + prefix # TODO(phajdan.jr): create a helper for adding to PATH. - env = self.m.step.get_from_context('env', {}) + env = self.m.context.env env.setdefault('PATH', '%(PATH)s') env['PATH'] = self.m.path.pathsep.join([ env['PATH'], str(self._module.PACKAGE_REPO_ROOT)]) - with self.m.step.context({'env': env}): + with self.m.context(env=env): return self.m.python(prefix + name, self.package_repo_resource('gclient.py'), cmd, @@ -268,12 +268,8 @@ class GclientApi(recipe_api.RecipeApi): def runhooks(self, args=None, name='runhooks', **kwargs): args = args or [] assert isinstance(args, (list, tuple)) - context = {} - if not self.m.step.get_from_context('cwd') and self.m.path['checkout']: - context['cwd'] = self.m.path['checkout'] - with self.m.step.context(context): - return self( - name, ['runhooks'] + list(args), infra_step=False, **kwargs) + with self.m.context(cwd=(self.m.context.cwd or self.m.path['checkout'])): + return self(name, ['runhooks'] + list(args), infra_step=False, **kwargs) @property def is_blink_mode(self): diff --git a/recipes/recipe_modules/gclient/example.py b/recipes/recipe_modules/gclient/example.py index 8f7bd3504e..b5a0d82162 100644 --- a/recipes/recipe_modules/gclient/example.py +++ b/recipes/recipe_modules/gclient/example.py @@ -4,6 +4,7 @@ DEPS = [ 'gclient', + 'recipe_engine/context', 'recipe_engine/path', 'recipe_engine/properties', 'recipe_engine/step', @@ -61,8 +62,7 @@ def RunSteps(api): bl_cfg.revisions['src/third_party/angle'] = 'refs/heads/lkgr' bl_cfg.got_revision_mapping['src/blatley'] = 'got_blatley_revision' - with api.step.context({ - 'cwd': api.path['start_dir'].join('src', 'third_party')}): + with api.context(cwd=api.path['start_dir'].join('src', 'third_party')): api.gclient.checkout( gclient_config=bl_cfg, with_branch_heads=True) diff --git a/recipes/recipe_modules/gerrit/__init__.py b/recipes/recipe_modules/gerrit/__init__.py index 864bbbfba9..7913d7caed 100644 --- a/recipes/recipe_modules/gerrit/__init__.py +++ b/recipes/recipe_modules/gerrit/__init__.py @@ -1,4 +1,5 @@ DEPS = [ + 'recipe_engine/context', 'recipe_engine/json', 'recipe_engine/path', 'recipe_engine/python', diff --git a/recipes/recipe_modules/gerrit/api.py b/recipes/recipe_modules/gerrit/api.py index 194f20ac3d..8d7b755060 100644 --- a/recipes/recipe_modules/gerrit/api.py +++ b/recipes/recipe_modules/gerrit/api.py @@ -12,12 +12,12 @@ class GerritApi(recipe_api.RecipeApi): assert isinstance(cmd, (list, tuple)) prefix = 'gerrit ' - env = self.m.step.get_from_context('env', {}) + env = self.m.context.env env.setdefault('PATH', '%(PATH)s') env['PATH'] = self.m.path.pathsep.join([ env['PATH'], str(self._module.PACKAGE_REPO_ROOT)]) - with self.m.step.context({'env': env}): + with self.m.context(env=env): return self.m.python(prefix + name, self.package_repo_resource('gerrit_client.py'), cmd, diff --git a/recipes/recipe_modules/git/__init__.py b/recipes/recipe_modules/git/__init__.py index 2dab849e99..0d7a27f53b 100644 --- a/recipes/recipe_modules/git/__init__.py +++ b/recipes/recipe_modules/git/__init__.py @@ -1,5 +1,6 @@ DEPS = [ 'infra_paths', + 'recipe_engine/context', 'recipe_engine/path', 'recipe_engine/platform', 'recipe_engine/properties', diff --git a/recipes/recipe_modules/git/api.py b/recipes/recipe_modules/git/api.py index 76656a3ca8..d6387d53c4 100644 --- a/recipes/recipe_modules/git/api.py +++ b/recipes/recipe_modules/git/api.py @@ -27,10 +27,7 @@ class GitApi(recipe_api.RecipeApi): git_cmd.extend(['-c', '%s=%s' % (k, v)]) can_fail_build = kwargs.pop('can_fail_build', True) try: - context = {} - if not self.m.step.get_from_context('cwd') and self.m.path['checkout']: - context['cwd'] = self.m.path['checkout'] - with self.m.step.context(context): + with self.m.context(cwd=(self.m.context.cwd or self.m.path['checkout'])): return self.m.step(name, git_cmd + list(args), infra_step=infra_step, **kwargs) except self.m.step.StepFailure as f: @@ -43,7 +40,7 @@ class GitApi(recipe_api.RecipeApi): """Ensures that depot_tools/git.bat actually exists.""" if not self.m.platform.is_win or self.initialized_win_git: return - with self.m.step.context({'cwd': self.package_repo_resource()}): + with self.m.context(cwd=self.package_repo_resource()): self.m.python( 'ensure git tooling on windows', self.package_repo_resource('bootstrap', 'win', 'git_bootstrap.py'), @@ -215,9 +212,9 @@ class GitApi(recipe_api.RecipeApi): path = self.m.path.pathsep.join([ str(self.package_repo_resource()), '%(PATH)s']) - with self.m.step.context({'cwd': dir_path}): + with self.m.context(cwd=dir_path): if use_git_cache: - with self.m.step.context({'env': {'PATH': path}}): + with self.m.context(env={'PATH': path}): self('retry', 'cache', 'populate', '-c', self.m.infra_paths.default_git_cache_dir, url, @@ -278,7 +275,7 @@ class GitApi(recipe_api.RecipeApi): name='count-objects before %s' % fetch_step_name, step_test_data=lambda: self.m.raw_io.test_api.stream_output( self.test_api.count_objects_output(1000))) - with self.m.step.context({'env': fetch_env}): + with self.m.context(env=fetch_env): self('retry', 'fetch', *fetch_args, name=fetch_step_name, stderr=fetch_stderr, @@ -355,7 +352,7 @@ class GitApi(recipe_api.RecipeApi): remote_name (str): the remote name to rebase from if not origin """ remote_name = remote_name or 'origin' - with self.m.step.context({'cwd': dir_path}): + with self.m.context(cwd=dir_path): try: self('rebase', '%s/master' % remote_name, name="%s rebase" % name_prefix, **kwargs) @@ -413,7 +410,7 @@ class GitApi(recipe_api.RecipeApi): upstream (str): to origin/master. kwargs: Forwarded to '__call__'. """ - env = self.m.step.get_from_context('env', {}) + env = self.m.context.env env['PATH'] = self.m.path.pathsep.join([ str(self.package_repo_resource()), '%(PATH)s']) args = ['new-branch', branch] @@ -421,5 +418,5 @@ class GitApi(recipe_api.RecipeApi): args.extend(['--upstream', upstream]) if not name: name = 'git new-branch %s' % branch - with self.m.step.context({'env': env}): + with self.m.context(env=env): return self(*args, name=name, **kwargs) diff --git a/recipes/recipe_modules/git/example.py b/recipes/recipe_modules/git/example.py index 0a5f048404..abdc6df039 100644 --- a/recipes/recipe_modules/git/example.py +++ b/recipes/recipe_modules/git/example.py @@ -4,6 +4,7 @@ DEPS = [ 'git', + 'recipe_engine/context', 'recipe_engine/path', 'recipe_engine/platform', 'recipe_engine/properties', @@ -59,7 +60,7 @@ def RunSteps(api): # If you need to run more arbitrary git commands, you can use api.git itself, # which behaves like api.step(), but automatically sets the name of the step. - with api.step.context({'cwd': api.path['checkout']}): + with api.context(cwd=api.path['checkout']): api.git('status') api.git('status', name='git status can_fail_build', diff --git a/recipes/recipe_modules/git_cl/__init__.py b/recipes/recipe_modules/git_cl/__init__.py index a4c9e4effc..b5d4da6bf8 100644 --- a/recipes/recipe_modules/git_cl/__init__.py +++ b/recipes/recipe_modules/git_cl/__init__.py @@ -1,4 +1,5 @@ DEPS = [ + 'recipe_engine/context', 'recipe_engine/raw_io', 'recipe_engine/step', ] diff --git a/recipes/recipe_modules/git_cl/api.py b/recipes/recipe_modules/git_cl/api.py index 717e2c1e87..ff6d7684e3 100644 --- a/recipes/recipe_modules/git_cl/api.py +++ b/recipes/recipe_modules/git_cl/api.py @@ -14,9 +14,8 @@ class GitClApi(recipe_api.RecipeApi): if kwargs.get('suffix'): name = name + ' (%s)' % kwargs.pop('suffix') - with self.m.step.context({ - 'cwd': self.m.step.get_from_context( - 'cwd', (self.c and self.c.repo_location) or None)}): + my_loc = self.c.repo_location if self.c else None + with self.m.context(cwd=self.m.context.cwd or my_loc): return self.m.step( name, [self.package_repo_resource('git_cl.py'), subcmd] + args, **kwargs) diff --git a/recipes/recipe_modules/presubmit/__init__.py b/recipes/recipe_modules/presubmit/__init__.py index 258f3c6847..0f3dc6ecfa 100644 --- a/recipes/recipe_modules/presubmit/__init__.py +++ b/recipes/recipe_modules/presubmit/__init__.py @@ -1,4 +1,5 @@ DEPS = [ + 'recipe_engine/context', 'recipe_engine/path', 'recipe_engine/python', 'recipe_engine/step', diff --git a/recipes/recipe_modules/presubmit/api.py b/recipes/recipe_modules/presubmit/api.py index 760ebefd84..9db783b05f 100644 --- a/recipes/recipe_modules/presubmit/api.py +++ b/recipes/recipe_modules/presubmit/api.py @@ -14,11 +14,11 @@ class PresubmitApi(recipe_api.RecipeApi): name = kwargs.pop('name', 'presubmit') - env = self.m.step.get_from_context('env', {}) + env = self.m.context.env env.setdefault('PATH', '%(PATH)s') env['PATH'] = self.m.path.pathsep.join([ env['PATH'], str(self._module.PACKAGE_REPO_ROOT)]) - with self.m.step.context({'env': env}): + with self.m.context(env=env): return self.m.python( name, self.presubmit_support_path, list(args), **kwargs) diff --git a/recipes/recipe_modules/tryserver/__init__.py b/recipes/recipe_modules/tryserver/__init__.py index 76c8fb8c8f..a5f38b9c7b 100644 --- a/recipes/recipe_modules/tryserver/__init__.py +++ b/recipes/recipe_modules/tryserver/__init__.py @@ -5,12 +5,13 @@ DEPS = [ 'git', 'git_cl', + 'recipe_engine/context', 'recipe_engine/json', 'recipe_engine/path', 'recipe_engine/platform', 'recipe_engine/properties', 'recipe_engine/python', 'recipe_engine/raw_io', - 'rietveld', 'recipe_engine/step', + 'rietveld', ] diff --git a/recipes/recipe_modules/tryserver/api.py b/recipes/recipe_modules/tryserver/api.py index 31e20258f5..21867887b9 100644 --- a/recipes/recipe_modules/tryserver/api.py +++ b/recipes/recipe_modules/tryserver/api.py @@ -84,7 +84,7 @@ class TryserverApi(recipe_api.RecipeApi): patch_path = patch_dir.join('patch.diff') self.m.python('patch git setup', git_setup_py, git_setup_args) - with self.m.step.context({'cwd': patch_dir}): + with self.m.context(cwd=patch_dir): self.m.git('fetch', 'origin', patch_ref, name='patch fetch') self.m.git('clean', '-f', '-d', '-x', name='patch clean') self.m.git('checkout', '-f', 'FETCH_HEAD', name='patch git checkout') @@ -138,9 +138,9 @@ class TryserverApi(recipe_api.RecipeApi): # removed. if patch_root is None: return self._old_get_files_affected_by_patch() - with self.m.step.context({ - 'cwd': self.m.step.get_from_context( - 'cwd', self.m.path['start_dir'].join(patch_root))}): + + cwd = self.m.context.cwd or self.m.path['start_dir'].join(patch_root) + with self.m.context(cwd=cwd): step_result = self.m.git('diff', '--cached', '--name-only', name='git diff to analyze patch', stdout=self.m.raw_io.output(), @@ -158,11 +158,10 @@ class TryserverApi(recipe_api.RecipeApi): def _old_get_files_affected_by_patch(self): - context = {} issue_root = self.m.rietveld.calculate_issue_root() - if issue_root: - context['cwd'] = self.m.path['checkout'].join(issue_root) - with self.m.step.context(context): + cwd = self.m.path['checkout'].join(issue_root) if issue_root else None + + with self.m.context(cwd=cwd): step_result = self.m.git('diff', '--cached', '--name-only', name='git diff to analyze patch', stdout=self.m.raw_io.output(),