diff --git a/gclient.py b/gclient.py index 03177c3f0..e5b1c4638 100755 --- a/gclient.py +++ b/gclient.py @@ -2814,7 +2814,7 @@ def CMDgetdep(parser, args): dest='vars', metavar='VAR', default=[], help='Gets the value of a given variable.') 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. ' 'If it is a git dependency, dep must be a path. If it ' '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) with open(options.deps_file) as f: 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: print(gclient_eval.GetVar(local_scope, var)) - for name in options.revisions: + for name in options.getdep_revisions: if ':' in name: name, _, package = name.partition(':') 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 ' 'name=value.') 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 ' 'the format dep@rev. If it is a git dependency, dep ' '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) with open(options.deps_file) as f: 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: name, _, value = var.partition('=') @@ -2893,7 +2913,7 @@ def CMDsetdep(parser, args): else: gclient_eval.AddVar(local_scope, name, value) - for revision in options.revisions: + for revision in options.setdep_revisions: name, _, value = revision.partition('@') if not name or not value: parser.error( diff --git a/tests/gclient_smoketest.py b/tests/gclient_smoketest.py index ecd31987b..94dce2ed5 100755 --- a/tests/gclient_smoketest.py +++ b/tests/gclient_smoketest.py @@ -903,7 +903,52 @@ class GClientSmokeGIT(GClientSmokeBase): with open(fake_deps) as f: 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([ 'vars = { ', @@ -916,6 +961,12 @@ class GClientSmokeGIT(GClientSmokeBase): ' },', ' "bar": "url@new_bar",', '}', + 'hooks = [{', + ' "name": "uses_builtin_var",', + ' "pattern": ".",', + ' "action": ["python", "fake.py",', + ' "--with-android={checkout_android}"],', + '}]', ], contents) def testGetDep(self): @@ -946,6 +997,41 @@ class GClientSmokeGIT(GClientSmokeBase): ], results[0].splitlines()) 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): if not self.enabled: return