Add validate command to gclient
Inspired by https://chromium-review.googlesource.com/c/411515/ . Bug: 570091 Change-Id: I7bf9653178c06125ae8be1dee021acfc187b2bdc Reviewed-on: https://chromium-review.googlesource.com/497848 Commit-Queue: Paweł Hajdan Jr. <phajdan.jr@chromium.org> Reviewed-by: Dirk Pranke <dpranke@chromium.org>changes/48/497848/6
parent
69a239e477
commit
e2f9feecaf
@ -0,0 +1,142 @@
|
|||||||
|
# Copyright 2017 The Chromium Authors. All rights reserved.
|
||||||
|
# Use of this source code is governed by a BSD-style license that can be
|
||||||
|
# found in the LICENSE file.
|
||||||
|
|
||||||
|
import ast
|
||||||
|
|
||||||
|
|
||||||
|
def _gclient_eval(node_or_string, global_scope, filename='<unknown>'):
|
||||||
|
"""Safely evaluates a single expression. Returns the result."""
|
||||||
|
_allowed_names = {'None': None, 'True': True, 'False': False}
|
||||||
|
if isinstance(node_or_string, basestring):
|
||||||
|
node_or_string = ast.parse(node_or_string, filename=filename, mode='eval')
|
||||||
|
if isinstance(node_or_string, ast.Expression):
|
||||||
|
node_or_string = node_or_string.body
|
||||||
|
def _convert(node):
|
||||||
|
if isinstance(node, ast.Str):
|
||||||
|
return node.s
|
||||||
|
elif isinstance(node, ast.Tuple):
|
||||||
|
return tuple(map(_convert, node.elts))
|
||||||
|
elif isinstance(node, ast.List):
|
||||||
|
return list(map(_convert, node.elts))
|
||||||
|
elif isinstance(node, ast.Dict):
|
||||||
|
return dict((_convert(k), _convert(v))
|
||||||
|
for k, v in zip(node.keys, node.values))
|
||||||
|
elif isinstance(node, ast.Name):
|
||||||
|
if node.id not in _allowed_names:
|
||||||
|
raise ValueError(
|
||||||
|
'invalid name %r (file %r, line %s)' % (
|
||||||
|
node.id, filename, getattr(node, 'lineno', '<unknown>')))
|
||||||
|
return _allowed_names[node.id]
|
||||||
|
elif isinstance(node, ast.Call):
|
||||||
|
if not isinstance(node.func, ast.Name):
|
||||||
|
raise ValueError(
|
||||||
|
'invalid call: func should be a name (file %r, line %s)' % (
|
||||||
|
filename, getattr(node, 'lineno', '<unknown>')))
|
||||||
|
if node.keywords or node.starargs or node.kwargs:
|
||||||
|
raise ValueError(
|
||||||
|
'invalid call: use only regular args (file %r, line %s)' % (
|
||||||
|
filename, getattr(node, 'lineno', '<unknown>')))
|
||||||
|
args = map(_convert, node.args)
|
||||||
|
return global_scope[node.func.id](*args)
|
||||||
|
elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add):
|
||||||
|
return _convert(node.left) + _convert(node.right)
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
'unexpected AST node: %s (file %r, line %s)' % (
|
||||||
|
node, filename, getattr(node, 'lineno', '<unknown>')))
|
||||||
|
return _convert(node_or_string)
|
||||||
|
|
||||||
|
|
||||||
|
def _gclient_exec(node_or_string, global_scope, filename='<unknown>'):
|
||||||
|
"""Safely execs a set of assignments. Returns resulting scope."""
|
||||||
|
result_scope = {}
|
||||||
|
|
||||||
|
if isinstance(node_or_string, basestring):
|
||||||
|
node_or_string = ast.parse(node_or_string, filename=filename, mode='exec')
|
||||||
|
if isinstance(node_or_string, ast.Expression):
|
||||||
|
node_or_string = node_or_string.body
|
||||||
|
|
||||||
|
def _visit_in_module(node):
|
||||||
|
if isinstance(node, ast.Assign):
|
||||||
|
if len(node.targets) != 1:
|
||||||
|
raise ValueError(
|
||||||
|
'invalid assignment: use exactly one target (file %r, line %s)' % (
|
||||||
|
filename, getattr(node, 'lineno', '<unknown>')))
|
||||||
|
target = node.targets[0]
|
||||||
|
if not isinstance(target, ast.Name):
|
||||||
|
raise ValueError(
|
||||||
|
'invalid assignment: target should be a name (file %r, line %s)' % (
|
||||||
|
filename, getattr(node, 'lineno', '<unknown>')))
|
||||||
|
value = _gclient_eval(node.value, global_scope, filename=filename)
|
||||||
|
|
||||||
|
if target.id in result_scope:
|
||||||
|
raise ValueError(
|
||||||
|
'invalid assignment: overrides var %r (file %r, line %s)' % (
|
||||||
|
target.id, filename, getattr(node, 'lineno', '<unknown>')))
|
||||||
|
|
||||||
|
result_scope[target.id] = value
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
'unexpected AST node: %s (file %r, line %s)' % (
|
||||||
|
node, filename, getattr(node, 'lineno', '<unknown>')))
|
||||||
|
|
||||||
|
if isinstance(node_or_string, ast.Module):
|
||||||
|
for stmt in node_or_string.body:
|
||||||
|
_visit_in_module(stmt)
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
'unexpected AST node: %s (file %r, line %s)' % (
|
||||||
|
node_or_string,
|
||||||
|
filename,
|
||||||
|
getattr(node_or_string, 'lineno', '<unknown>')))
|
||||||
|
|
||||||
|
return result_scope
|
||||||
|
|
||||||
|
|
||||||
|
class CheckFailure(Exception):
|
||||||
|
"""Contains details of a check failure."""
|
||||||
|
def __init__(self, msg, path, exp, act):
|
||||||
|
super(CheckFailure, self).__init__(msg)
|
||||||
|
self.path = path
|
||||||
|
self.exp = exp
|
||||||
|
self.act = act
|
||||||
|
|
||||||
|
|
||||||
|
def Check(content, path, global_scope, expected_scope):
|
||||||
|
"""Cross-checks the old and new gclient eval logic.
|
||||||
|
|
||||||
|
Safely execs |content| (backed by file |path|) using |global_scope|,
|
||||||
|
and compares with |expected_scope|.
|
||||||
|
|
||||||
|
Throws CheckFailure if any difference between |expected_scope| and scope
|
||||||
|
returned by new gclient eval code is detected.
|
||||||
|
"""
|
||||||
|
def fail(prefix, exp, act):
|
||||||
|
raise CheckFailure(
|
||||||
|
'gclient check for %s: %s exp %s, got %s' % (
|
||||||
|
path, prefix, repr(exp), repr(act)), prefix, exp, act)
|
||||||
|
|
||||||
|
def compare(expected, actual, var_path, actual_scope):
|
||||||
|
if isinstance(expected, dict):
|
||||||
|
exp = set(expected.keys())
|
||||||
|
act = set(actual.keys())
|
||||||
|
if exp != act:
|
||||||
|
fail(var_path, exp, act)
|
||||||
|
for k in expected:
|
||||||
|
compare(expected[k], actual[k], var_path + '["%s"]' % k, actual_scope)
|
||||||
|
return
|
||||||
|
elif isinstance(expected, list):
|
||||||
|
exp = len(expected)
|
||||||
|
act = len(actual)
|
||||||
|
if exp != act:
|
||||||
|
fail('len(%s)' % var_path, expected_scope, actual_scope)
|
||||||
|
for i in range(exp):
|
||||||
|
compare(expected[i], actual[i], var_path + '[%d]' % i, actual_scope)
|
||||||
|
else:
|
||||||
|
if expected != actual:
|
||||||
|
fail(var_path, expected_scope, actual_scope)
|
||||||
|
|
||||||
|
result_scope = _gclient_exec(content, global_scope, filename=path)
|
||||||
|
|
||||||
|
compare(expected_scope, result_scope, '', result_scope)
|
@ -0,0 +1,116 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# Copyright 2017 The Chromium Authors. All rights reserved.
|
||||||
|
# Use of this source code is governed by a BSD-style license that can be
|
||||||
|
# found in the LICENSE file.
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
|
import gclient_eval
|
||||||
|
|
||||||
|
|
||||||
|
class GClientEvalTest(unittest.TestCase):
|
||||||
|
def test_str(self):
|
||||||
|
self.assertEqual('foo', gclient_eval._gclient_eval('"foo"', {}))
|
||||||
|
|
||||||
|
def test_tuple(self):
|
||||||
|
self.assertEqual(('a', 'b'), gclient_eval._gclient_eval('("a", "b")', {}))
|
||||||
|
|
||||||
|
def test_list(self):
|
||||||
|
self.assertEqual(['a', 'b'], gclient_eval._gclient_eval('["a", "b"]', {}))
|
||||||
|
|
||||||
|
def test_dict(self):
|
||||||
|
self.assertEqual({'a': 'b'}, gclient_eval._gclient_eval('{"a": "b"}', {}))
|
||||||
|
|
||||||
|
def test_name_safe(self):
|
||||||
|
self.assertEqual(True, gclient_eval._gclient_eval('True', {}))
|
||||||
|
|
||||||
|
def test_name_unsafe(self):
|
||||||
|
with self.assertRaises(ValueError) as cm:
|
||||||
|
gclient_eval._gclient_eval('UnsafeName', {'UnsafeName': 'foo'})
|
||||||
|
self.assertIn('invalid name \'UnsafeName\'', str(cm.exception))
|
||||||
|
|
||||||
|
def test_call(self):
|
||||||
|
self.assertEqual(
|
||||||
|
'bar',
|
||||||
|
gclient_eval._gclient_eval('Foo("bar")', {'Foo': lambda x: x}))
|
||||||
|
|
||||||
|
def test_plus(self):
|
||||||
|
self.assertEqual('foo', gclient_eval._gclient_eval('"f" + "o" + "o"', {}))
|
||||||
|
|
||||||
|
def test_not_expression(self):
|
||||||
|
with self.assertRaises(SyntaxError) as cm:
|
||||||
|
gclient_eval._gclient_eval('def foo():\n pass', {})
|
||||||
|
self.assertIn('invalid syntax', str(cm.exception))
|
||||||
|
|
||||||
|
def test_not_whitelisted(self):
|
||||||
|
with self.assertRaises(ValueError) as cm:
|
||||||
|
gclient_eval._gclient_eval('[x for x in [1, 2, 3]]', {})
|
||||||
|
self.assertIn(
|
||||||
|
'unexpected AST node: <_ast.ListComp object', str(cm.exception))
|
||||||
|
|
||||||
|
|
||||||
|
class GClientExecTest(unittest.TestCase):
|
||||||
|
def test_basic(self):
|
||||||
|
self.assertEqual(
|
||||||
|
{'a': '1', 'b': '2', 'c': '3'},
|
||||||
|
gclient_eval._gclient_exec('a = "1"\nb = "2"\nc = "3"', {}))
|
||||||
|
|
||||||
|
def test_multiple_assignment(self):
|
||||||
|
with self.assertRaises(ValueError) as cm:
|
||||||
|
gclient_eval._gclient_exec('a, b, c = "a", "b", "c"', {})
|
||||||
|
self.assertIn(
|
||||||
|
'invalid assignment: target should be a name', str(cm.exception))
|
||||||
|
|
||||||
|
def test_override(self):
|
||||||
|
with self.assertRaises(ValueError) as cm:
|
||||||
|
gclient_eval._gclient_exec('a = "a"\na = "x"', {})
|
||||||
|
self.assertIn(
|
||||||
|
'invalid assignment: overrides var \'a\'', str(cm.exception))
|
||||||
|
|
||||||
|
|
||||||
|
class CheckTest(unittest.TestCase):
|
||||||
|
TEST_CODE="""
|
||||||
|
list_var = ["a", "b", "c"]
|
||||||
|
|
||||||
|
dict_var = {"a": "1", "b": "2", "c": "3"}
|
||||||
|
|
||||||
|
nested_var = {
|
||||||
|
"list": ["a", "b", "c"],
|
||||||
|
"dict": {"a": "1", "b": "2", "c": "3"}
|
||||||
|
}"""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.expected = {}
|
||||||
|
exec(self.TEST_CODE, {}, self.expected)
|
||||||
|
|
||||||
|
def test_pass(self):
|
||||||
|
gclient_eval.Check(self.TEST_CODE, '<string>', {}, self.expected)
|
||||||
|
|
||||||
|
def test_fail_list(self):
|
||||||
|
self.expected['list_var'][0] = 'x'
|
||||||
|
with self.assertRaises(gclient_eval.CheckFailure):
|
||||||
|
gclient_eval.Check(self.TEST_CODE, '<string>', {}, self.expected)
|
||||||
|
|
||||||
|
def test_fail_dict(self):
|
||||||
|
self.expected['dict_var']['a'] = 'x'
|
||||||
|
with self.assertRaises(gclient_eval.CheckFailure):
|
||||||
|
gclient_eval.Check(self.TEST_CODE, '<string>', {}, self.expected)
|
||||||
|
|
||||||
|
def test_fail_nested(self):
|
||||||
|
self.expected['nested_var']['dict']['c'] = 'x'
|
||||||
|
with self.assertRaises(gclient_eval.CheckFailure):
|
||||||
|
gclient_eval.Check(self.TEST_CODE, '<string>', {}, self.expected)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
level = logging.DEBUG if '-v' in sys.argv else logging.FATAL
|
||||||
|
logging.basicConfig(
|
||||||
|
level=level,
|
||||||
|
format='%(asctime).19s %(levelname)s %(filename)s:'
|
||||||
|
'%(lineno)s %(message)s')
|
||||||
|
unittest.main()
|
Loading…
Reference in New Issue