Reland "[gclient] Make getdep and setdep to provide builtin vars"

This is a reland of 5705acabe0

If a gclient config is not found, then built-in variables wont
be supported.

Original change's description:
> [gclient] Make getdep and setdep to provide builtin vars
>
> Bug: 906114
> Change-Id: I069cc21343911f7fdb3c91ecbd8fcba53fc8099f
> Reviewed-on: https://chromium-review.googlesource.com/c/1340461
> Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
> Commit-Queue: Eric Boren <borenet@chromium.org>

Bug: 906114
Change-Id: I72f30d10b5f0180fd5c616a42393f5b12055ce8e
Reviewed-on: https://chromium-review.googlesource.com/c/1341039
Reviewed-by: Eric Boren <borenet@chromium.org>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
changes/39/1341039/5
Edward Lemur 7 years ago committed by Commit Bot
parent c6ffd7af7d
commit af3328fc7a

@ -2814,7 +2814,7 @@ def CMDgetdep(parser, args):
dest='vars', metavar='VAR', default=[], dest='vars', metavar='VAR', default=[],
help='Gets the value of a given variable.') help='Gets the value of a given variable.')
parser.add_option('-r', '--revision', action='append', parser.add_option('-r', '--revision', action='append',
dest='revisions', metavar='DEP', default=[], dest='getdep_revisions', metavar='DEP', default=[],
help='Gets the revision/version for the given dependency. ' help='Gets the revision/version for the given dependency. '
'If it is a git dependency, dep must be a path. If it ' 'If it is a git dependency, dep must be a path. If it '
'is a CIPD dependency, dep must be of the form ' 'is a CIPD dependency, dep must be of the form '
@ -2831,12 +2831,21 @@ def CMDgetdep(parser, args):
'DEPS file %s does not exist.' % options.deps_file) 'DEPS file %s does not exist.' % options.deps_file)
with open(options.deps_file) as f: with open(options.deps_file) as f:
contents = f.read() contents = f.read()
local_scope = gclient_eval.Exec(contents, options.deps_file) client = GClient.LoadCurrentConfig(options)
if client is not None:
builtin_vars = client.get_builtin_vars()
else:
logging.warn(
'Couldn\'t find a valid gclient config. Will attempt to parse the DEPS '
'file without support for built-in variables.')
builtin_vars = None
local_scope = gclient_eval.Exec(contents, options.deps_file,
builtin_vars=builtin_vars)
for var in options.vars: for var in options.vars:
print(gclient_eval.GetVar(local_scope, var)) print(gclient_eval.GetVar(local_scope, var))
for name in options.revisions: for name in options.getdep_revisions:
if ':' in name: if ':' in name:
name, _, package = name.partition(':') name, _, package = name.partition(':')
if not name or not package: if not name or not package:
@ -2856,7 +2865,7 @@ def CMDsetdep(parser, args):
help='Sets a variable to the given value with the format ' help='Sets a variable to the given value with the format '
'name=value.') 'name=value.')
parser.add_option('-r', '--revision', action='append', parser.add_option('-r', '--revision', action='append',
dest='revisions', metavar='DEP@REV', default=[], dest='setdep_revisions', metavar='DEP@REV', default=[],
help='Sets the revision/version for the dependency with ' help='Sets the revision/version for the dependency with '
'the format dep@rev. If it is a git dependency, dep ' 'the format dep@rev. If it is a git dependency, dep '
'must be a path and rev must be a git hash or ' 'must be a path and rev must be a git hash or '
@ -2881,7 +2890,18 @@ def CMDsetdep(parser, args):
'DEPS file %s does not exist.' % options.deps_file) 'DEPS file %s does not exist.' % options.deps_file)
with open(options.deps_file) as f: with open(options.deps_file) as f:
contents = f.read() contents = f.read()
local_scope = gclient_eval.Exec(contents, options.deps_file)
client = GClient.LoadCurrentConfig(options)
if client is not None:
builtin_vars = client.get_builtin_vars()
else:
logging.warn(
'Couldn\'t find a valid gclient config. Will attempt to parse the DEPS '
'file without support for built-in variables.')
builtin_vars = None
local_scope = gclient_eval.Exec(contents, options.deps_file,
builtin_vars=builtin_vars)
for var in options.vars: for var in options.vars:
name, _, value = var.partition('=') name, _, value = var.partition('=')
@ -2893,7 +2913,7 @@ def CMDsetdep(parser, args):
else: else:
gclient_eval.AddVar(local_scope, name, value) gclient_eval.AddVar(local_scope, name, value)
for revision in options.revisions: for revision in options.setdep_revisions:
name, _, value = revision.partition('@') name, _, value = revision.partition('@')
if not name or not value: if not name or not value:
parser.error( parser.error(

@ -903,7 +903,52 @@ class GClientSmokeGIT(GClientSmokeBase):
with open(fake_deps) as f: with open(fake_deps) as f:
contents = f.read().splitlines() contents = f.read().splitlines()
self.assertEqual('', results[1]) self.assertEqual('', results[1], results[1])
self.assertEqual(0, results[2])
self.assertEqual([
'vars = { ',
' "foo_var": "new_val",',
' "foo_rev": "new_foo",',
'}',
'deps = {',
' "foo": {',
' "url": "url@{foo_rev}",',
' },',
' "bar": "url@new_bar",',
'}',
], contents)
def testSetDep_BuiltinVariables(self):
self.gclient(['config', self.git_base + 'repo_1', '--name', 'src'])
fake_deps = os.path.join(self.root_dir, 'DEPS.fake')
with open(fake_deps, 'w') as f:
f.write('\n'.join([
'vars = { ',
' "foo_var": "foo_val",',
' "foo_rev": "foo_rev",',
'}',
'deps = {',
' "foo": {',
' "url": "url@{foo_rev}",',
' },',
' "bar": "url@bar_rev",',
'}',
'hooks = [{',
' "name": "uses_builtin_var",',
' "pattern": ".",',
' "action": ["python", "fake.py",',
' "--with-android={checkout_android}"],',
'}]',
]))
results = self.gclient([
'setdep', '-r', 'foo@new_foo', '-r', 'bar@new_bar',
'--var', 'foo_var=new_val', '--deps-file', fake_deps])
with open(fake_deps) as f:
contents = f.read().splitlines()
self.assertEqual('', results[1], results[1])
self.assertEqual(0, results[2]) self.assertEqual(0, results[2])
self.assertEqual([ self.assertEqual([
'vars = { ', 'vars = { ',
@ -916,6 +961,12 @@ class GClientSmokeGIT(GClientSmokeBase):
' },', ' },',
' "bar": "url@new_bar",', ' "bar": "url@new_bar",',
'}', '}',
'hooks = [{',
' "name": "uses_builtin_var",',
' "pattern": ".",',
' "action": ["python", "fake.py",',
' "--with-android={checkout_android}"],',
'}]',
], contents) ], contents)
def testGetDep(self): def testGetDep(self):
@ -946,6 +997,41 @@ class GClientSmokeGIT(GClientSmokeBase):
], results[0].splitlines()) ], results[0].splitlines())
self.assertEqual(0, results[2]) self.assertEqual(0, results[2])
def testGetDep_BuiltinVariables(self):
self.gclient(['config', self.git_base + 'repo_1', '--name', 'src'])
fake_deps = os.path.join(self.root_dir, 'DEPS.fake')
with open(fake_deps, 'w') as f:
f.write('\n'.join([
'vars = { ',
' "foo_var": "foo_val",',
' "foo_rev": "foo_rev",',
'}',
'deps = {',
' "foo": {',
' "url": "url@{foo_rev}",',
' },',
' "bar": "url@bar_rev",',
'}',
'hooks = [{',
' "name": "uses_builtin_var",',
' "pattern": ".",',
' "action": ["python", "fake.py",',
' "--with-android={checkout_android}"],',
'}]',
]))
results = self.gclient([
'getdep', '-r', 'foo', '-r', 'bar','--var', 'foo_var',
'--deps-file', fake_deps])
self.assertEqual('', results[1])
self.assertEqual([
'foo_val',
'foo_rev',
'bar_rev',
], results[0].splitlines())
self.assertEqual(0, results[2])
def testFlatten(self): def testFlatten(self):
if not self.enabled: if not self.enabled:
return return

Loading…
Cancel
Save