Reland "gclient eval: Expand vars while parsing DEPS files"
This is a reland of 88f9c40e0ccefb491790888a703c857b0c3ec6d1
We no longer need the vars dict to be declared before vars can be used.
This was causing problems because gclient flatten printed vars after deps,
breaking the above assumption.
Original change's description:
> gclient eval: Expand vars while parsing DEPS files
>
> Introduce a Parse function that takes care of expanding vars while parsing
> the DEPS file.
>
> It wraps Exec and exec calls, and supports deferring the expansion until
> later, so gclient flatten gets access to the unexpanded version.
>
> Bug: 821199
> Change-Id: I943b021cc4474c9cda67b3816b841dd8ada3f5b2
> Reviewed-on: https://chromium-review.googlesource.com/973749
> Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
> Reviewed-by: Aaron Gable <agable@chromium.org>
> Reviewed-by: Dirk Pranke <dpranke@chromium.org>
Bug: 821199
Change-Id: I583df23558f91871e1a2aa2574c20d35e54635f6
Reviewed-on: https://chromium-review.googlesource.com/981086
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Michael Moss <mmoss@chromium.org>
Reviewed-by: Aaron Gable <agable@chromium.org>
diff --git a/gclient.py b/gclient.py
index d389622..b6cea2b 100755
--- a/gclient.py
+++ b/gclient.py
@@ -736,7 +736,7 @@
deps_to_add.sort(key=lambda x: x.name)
return deps_to_add
- def ParseDepsFile(self):
+ def ParseDepsFile(self, expand_vars=True):
"""Parses the DEPS file for this dependency."""
assert not self.deps_parsed
assert not self.dependencies
@@ -765,15 +765,15 @@
local_scope = {}
if deps_content:
- # Eval the content.
try:
- if self._get_option('validate_syntax', False):
- local_scope = gclient_eval.Exec(deps_content, filepath)
- else:
- global_scope = {
- 'Var': lambda var_name: '{%s}' % var_name,
- }
- exec(deps_content, global_scope, local_scope)
+ vars_override = {}
+ if self.parent:
+ vars_override = self.parent.get_vars()
+ vars_override.update(self.get_vars())
+ local_scope = gclient_eval.Parse(
+ deps_content, expand_vars,
+ self._get_option('validate_syntax', False),
+ filepath, vars_override)
except SyntaxError as e:
gclient_utils.SyntaxErrorToError(filepath, e)
@@ -988,7 +988,7 @@
file_list[i] = file_list[i][1:]
# Always parse the DEPS file.
- self.ParseDepsFile()
+ self.ParseDepsFile(expand_vars=(command != 'flatten'))
self._run_is_done(file_list or [], parsed_url)
if command in ('update', 'revert') and not options.noprehooks:
self.RunPreDepsHooks()
@@ -1873,7 +1873,7 @@
print('%s: %s' % (x, entries[x]))
logging.info(str(self))
- def ParseDepsFile(self):
+ def ParseDepsFile(self, expand_vars=None):
"""No DEPS to parse for a .gclient file."""
raise gclient_utils.Error('Internal error')
@@ -1982,7 +1982,7 @@
self._cipd_package = self._cipd_root.add_package(
self._cipd_subdir, self._package_name, self._package_version)
- def ParseDepsFile(self):
+ def ParseDepsFile(self, expand_vars=None):
"""CIPD dependencies are not currently allowed to have nested deps."""
self.add_dependencies_and_close([], [])
@@ -2939,7 +2939,9 @@
'DEPS file %s does not exist.' % options.deps_file)
with open(options.deps_file) as f:
contents = f.read()
- local_scope = gclient_eval.Exec(contents)
+ local_scope = gclient_eval.Parse(
+ contents, expand_vars=True, validate_syntax=True,
+ filename=options.deps_file)
for var in options.vars:
name, _, value = var.partition('=')