From c93643954df2265d7d3febd9d6509238fffb45b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Hajdan=2C=20Jr?= Date: Wed, 14 Jun 2017 17:11:56 +0200 Subject: [PATCH] gclient: add cwd support for hooks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes it possible to run hooks properly in flattened DEPS. Bug: 570091 Change-Id: If8175a57ebe8f607bd4ac83d4a26dcc4cc18165c Reviewed-on: https://chromium-review.googlesource.com/535476 Reviewed-by: Andrii Shyshkalov Commit-Queue: Paweł Hajdan Jr. --- gclient.py | 19 ++++++++++++------- gclient_eval.py | 3 +++ tests/gclient_smoketest.py | 2 ++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/gclient.py b/gclient.py index 97fa624f6..4badcfbc0 100755 --- a/gclient.py +++ b/gclient.py @@ -191,22 +191,24 @@ def ToGNString(value, allow_dicts = True): class Hook(object): """Descriptor of command ran before/after sync or on demand.""" - def __init__(self, action, pattern=None, name=None): + def __init__(self, action, pattern=None, name=None, cwd=None): """Constructor. Arguments: action (list of basestring): argv of the command to run pattern (basestring regex): noop with git; deprecated name (basestring): optional name; no effect on operation + cwd (basestring): working directory to use """ self._action = gclient_utils.freeze(action) self._pattern = pattern self._name = name + self._cwd = cwd @staticmethod def from_dict(d): """Creates a Hook instance from a dict like in the DEPS file.""" - return Hook(d['action'], d.get('pattern'), d.get('name')) + return Hook(d['action'], d.get('pattern'), d.get('name'), d.get('cwd')) @property def action(self): @@ -235,10 +237,14 @@ class Hook(object): # Python script. Run it by starting a new copy of the same # interpreter. cmd[0] = sys.executable + + cwd = root + if self._cwd: + cwd = os.path.join(cwd, self._cwd) try: start_time = time.time() gclient_utils.CheckCallAndFilterAndHeader( - cmd, cwd=root, always=True) + cmd, cwd=cwd, always=True) except (gclient_utils.Error, subprocess2.CalledProcessError) as e: # Use a discrete exit status code of 2 to indicate that a hook action # failed. Users of this script may wish to treat hook action failures @@ -1833,8 +1839,7 @@ def _FlattenDep(dep, deps, deps_os, hooks, pre_deps_hooks, unpinned_deps): # TODO(phajdan.jr): also handle hooks_os. hooks.extend([(dep, hook) for hook in dep.deps_hooks]) - pre_deps_hooks.extend( - [(dep, {'action': hook}) for hook in dep.pre_deps_hooks]) + pre_deps_hooks.extend([(dep, hook) for hook in dep.pre_deps_hooks]) def _FlattenRecurse(dep, deps, deps_os, hooks, pre_deps_hooks, unpinned_deps): @@ -1929,9 +1934,9 @@ def _HooksToLines(name, hooks): s.append(' "name": "%s",' % hook.name) if hook.pattern is not None: s.append(' "pattern": "%s",' % hook.pattern) - # TODO(phajdan.jr): actions may contain paths that need to be adjusted, - # i.e. they may be relative to the dependency path, not solution root. s.extend( + # Hooks run in the parent directory of their dep. + [' "cwd": "%s"' % os.path.normpath(os.path.dirname(dep.name))] + [' "action": ['] + [' "%s",' % arg for arg in hook.action] + [' ]', ' },', ''] diff --git a/gclient_eval.py b/gclient_eval.py index 182ff9a28..2f27634ae 100644 --- a/gclient_eval.py +++ b/gclient_eval.py @@ -20,6 +20,9 @@ _GCLIENT_HOOKS_SCHEMA = [{ # only when files matching the pattern have changed. In practice, with git, # gclient runs all the hooks regardless of this field. schema.Optional('pattern'): basestring, + + # Working directory where to execute the hook. + schema.Optional('cwd'): basestring, }] _GCLIENT_SCHEMA = schema.Schema({ diff --git a/tests/gclient_smoketest.py b/tests/gclient_smoketest.py index 2b8f238a9..324a24a21 100755 --- a/tests/gclient_smoketest.py +++ b/tests/gclient_smoketest.py @@ -638,6 +638,7 @@ class GClientSmokeGIT(GClientSmokeBase): ' # src', ' {', ' "pattern": ".",', + ' "cwd": "."', ' "action": [', ' "python",', ' "-c",', @@ -648,6 +649,7 @@ class GClientSmokeGIT(GClientSmokeBase): ' # src', ' {', ' "pattern": "nonexistent",', + ' "cwd": "."', ' "action": [', ' "python",', ' "-c",',