Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 1 | # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import ast |
Paweł Hajdan, Jr | 7cf96a4 | 2017-05-26 18:28:35 | [diff] [blame] | 6 | import collections |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 7 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 8 | from third_party import schema |
| 9 | |
| 10 | |
| 11 | # See https://github.com/keleshev/schema for docs how to configure schema. |
| 12 | _GCLIENT_HOOKS_SCHEMA = [{ |
| 13 | # Hook action: list of command-line arguments to invoke. |
| 14 | 'action': [basestring], |
| 15 | |
| 16 | # Name of the hook. Doesn't affect operation. |
| 17 | schema.Optional('name'): basestring, |
| 18 | |
| 19 | # Hook pattern (regex). Originally intended to limit some hooks to run |
| 20 | # only when files matching the pattern have changed. In practice, with git, |
| 21 | # gclient runs all the hooks regardless of this field. |
| 22 | schema.Optional('pattern'): basestring, |
| 23 | }] |
| 24 | |
| 25 | _GCLIENT_SCHEMA = schema.Schema({ |
| 26 | # List of host names from which dependencies are allowed (whitelist). |
| 27 | # NOTE: when not present, all hosts are allowed. |
| 28 | # NOTE: scoped to current DEPS file, not recursive. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 29 | schema.Optional('allowed_hosts'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 30 | |
| 31 | # Mapping from paths to repo and revision to check out under that path. |
| 32 | # Applying this mapping to the on-disk checkout is the main purpose |
| 33 | # of gclient, and also why the config file is called DEPS. |
| 34 | # |
| 35 | # The following functions are allowed: |
| 36 | # |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 37 | # Var(): allows variable substitution (either from 'vars' dict below, |
| 38 | # or command-line override) |
Paweł Hajdan, Jr | c7ba033 | 2017-05-29 14:38:45 | [diff] [blame] | 39 | schema.Optional('deps'): { |
| 40 | schema.Optional(basestring): schema.Or( |
| 41 | basestring, |
| 42 | { |
| 43 | 'url': basestring, |
| 44 | }, |
| 45 | ), |
| 46 | }, |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 47 | |
| 48 | # Similar to 'deps' (see above) - also keyed by OS (e.g. 'linux'). |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 49 | # Also see 'target_os'. |
| 50 | schema.Optional('deps_os'): { |
| 51 | schema.Optional(basestring): { |
| 52 | schema.Optional(basestring): schema.Or(basestring, None) |
| 53 | } |
| 54 | }, |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 55 | |
| 56 | # Hooks executed after gclient sync (unless suppressed), or explicitly |
| 57 | # on gclient hooks. See _GCLIENT_HOOKS_SCHEMA for details. |
| 58 | # Also see 'pre_deps_hooks'. |
| 59 | schema.Optional('hooks'): _GCLIENT_HOOKS_SCHEMA, |
| 60 | |
Scott Graham | c482674 | 2017-05-11 23:59:23 | [diff] [blame] | 61 | # Similar to 'hooks', also keyed by OS. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 62 | schema.Optional('hooks_os'): { |
| 63 | schema.Optional(basestring): _GCLIENT_HOOKS_SCHEMA |
| 64 | }, |
Scott Graham | c482674 | 2017-05-11 23:59:23 | [diff] [blame] | 65 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 66 | # Rules which #includes are allowed in the directory. |
| 67 | # Also see 'skip_child_includes' and 'specific_include_rules'. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 68 | schema.Optional('include_rules'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 69 | |
| 70 | # Hooks executed before processing DEPS. See 'hooks' for more details. |
| 71 | schema.Optional('pre_deps_hooks'): _GCLIENT_HOOKS_SCHEMA, |
| 72 | |
Paweł Hajdan, Jr | 6f79679 | 2017-06-02 06:40:06 | [diff] [blame] | 73 | # Recursion limit for nested DEPS. |
| 74 | schema.Optional('recursion'): int, |
| 75 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 76 | # Whitelists deps for which recursion should be enabled. |
| 77 | schema.Optional('recursedeps'): [ |
Paweł Hajdan, Jr | 05fec03 | 2017-05-30 21:04:23 | [diff] [blame] | 78 | schema.Optional(schema.Or( |
| 79 | basestring, |
| 80 | (basestring, basestring), |
| 81 | [basestring, basestring] |
| 82 | )), |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 83 | ], |
| 84 | |
| 85 | # Blacklists directories for checking 'include_rules'. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 86 | schema.Optional('skip_child_includes'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 87 | |
| 88 | # Mapping from paths to include rules specific for that path. |
| 89 | # See 'include_rules' for more details. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 90 | schema.Optional('specific_include_rules'): { |
| 91 | schema.Optional(basestring): [basestring] |
| 92 | }, |
| 93 | |
| 94 | # List of additional OS names to consider when selecting dependencies |
| 95 | # from deps_os. |
| 96 | schema.Optional('target_os'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 97 | |
| 98 | # For recursed-upon sub-dependencies, check out their own dependencies |
| 99 | # relative to the paren't path, rather than relative to the .gclient file. |
| 100 | schema.Optional('use_relative_paths'): bool, |
| 101 | |
| 102 | # Variables that can be referenced using Var() - see 'deps'. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 103 | schema.Optional('vars'): {schema.Optional(basestring): basestring}, |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 104 | }) |
| 105 | |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 106 | |
| 107 | def _gclient_eval(node_or_string, global_scope, filename='<unknown>'): |
| 108 | """Safely evaluates a single expression. Returns the result.""" |
| 109 | _allowed_names = {'None': None, 'True': True, 'False': False} |
| 110 | if isinstance(node_or_string, basestring): |
| 111 | node_or_string = ast.parse(node_or_string, filename=filename, mode='eval') |
| 112 | if isinstance(node_or_string, ast.Expression): |
| 113 | node_or_string = node_or_string.body |
| 114 | def _convert(node): |
| 115 | if isinstance(node, ast.Str): |
| 116 | return node.s |
Paweł Hajdan, Jr | 6f79679 | 2017-06-02 06:40:06 | [diff] [blame] | 117 | elif isinstance(node, ast.Num): |
| 118 | return node.n |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 119 | elif isinstance(node, ast.Tuple): |
| 120 | return tuple(map(_convert, node.elts)) |
| 121 | elif isinstance(node, ast.List): |
| 122 | return list(map(_convert, node.elts)) |
| 123 | elif isinstance(node, ast.Dict): |
Paweł Hajdan, Jr | 7cf96a4 | 2017-05-26 18:28:35 | [diff] [blame] | 124 | return collections.OrderedDict( |
| 125 | (_convert(k), _convert(v)) |
| 126 | for k, v in zip(node.keys, node.values)) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 127 | elif isinstance(node, ast.Name): |
| 128 | if node.id not in _allowed_names: |
| 129 | raise ValueError( |
| 130 | 'invalid name %r (file %r, line %s)' % ( |
| 131 | node.id, filename, getattr(node, 'lineno', '<unknown>'))) |
| 132 | return _allowed_names[node.id] |
| 133 | elif isinstance(node, ast.Call): |
| 134 | if not isinstance(node.func, ast.Name): |
| 135 | raise ValueError( |
| 136 | 'invalid call: func should be a name (file %r, line %s)' % ( |
| 137 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 138 | if node.keywords or node.starargs or node.kwargs: |
| 139 | raise ValueError( |
| 140 | 'invalid call: use only regular args (file %r, line %s)' % ( |
| 141 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 142 | args = map(_convert, node.args) |
| 143 | return global_scope[node.func.id](*args) |
| 144 | elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add): |
| 145 | return _convert(node.left) + _convert(node.right) |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 146 | elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Mod): |
| 147 | return _convert(node.left) % _convert(node.right) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 148 | else: |
| 149 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 18:14:44 | [diff] [blame] | 150 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
| 151 | node, ast.dump(node), filename, |
| 152 | getattr(node, 'lineno', '<unknown>'))) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 153 | return _convert(node_or_string) |
| 154 | |
| 155 | |
Paweł Hajdan, Jr | c485d5a | 2017-06-02 10:08:09 | [diff] [blame] | 156 | def Exec(content, global_scope, local_scope, filename='<unknown>'): |
| 157 | """Safely execs a set of assignments. Mutates |local_scope|.""" |
| 158 | node_or_string = ast.parse(content, filename=filename, mode='exec') |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 159 | if isinstance(node_or_string, ast.Expression): |
| 160 | node_or_string = node_or_string.body |
| 161 | |
| 162 | def _visit_in_module(node): |
| 163 | if isinstance(node, ast.Assign): |
| 164 | if len(node.targets) != 1: |
| 165 | raise ValueError( |
| 166 | 'invalid assignment: use exactly one target (file %r, line %s)' % ( |
| 167 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 168 | target = node.targets[0] |
| 169 | if not isinstance(target, ast.Name): |
| 170 | raise ValueError( |
| 171 | 'invalid assignment: target should be a name (file %r, line %s)' % ( |
| 172 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 173 | value = _gclient_eval(node.value, global_scope, filename=filename) |
| 174 | |
Paweł Hajdan, Jr | c485d5a | 2017-06-02 10:08:09 | [diff] [blame] | 175 | if target.id in local_scope: |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 176 | raise ValueError( |
| 177 | 'invalid assignment: overrides var %r (file %r, line %s)' % ( |
| 178 | target.id, filename, getattr(node, 'lineno', '<unknown>'))) |
| 179 | |
Paweł Hajdan, Jr | c485d5a | 2017-06-02 10:08:09 | [diff] [blame] | 180 | local_scope[target.id] = value |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 181 | else: |
| 182 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 18:14:44 | [diff] [blame] | 183 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
| 184 | node, ast.dump(node), filename, |
| 185 | getattr(node, 'lineno', '<unknown>'))) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 186 | |
| 187 | if isinstance(node_or_string, ast.Module): |
| 188 | for stmt in node_or_string.body: |
| 189 | _visit_in_module(stmt) |
| 190 | else: |
| 191 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 18:14:44 | [diff] [blame] | 192 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 193 | node_or_string, |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 18:14:44 | [diff] [blame] | 194 | ast.dump(node_or_string), |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 195 | filename, |
| 196 | getattr(node_or_string, 'lineno', '<unknown>'))) |
| 197 | |
Paweł Hajdan, Jr | c485d5a | 2017-06-02 10:08:09 | [diff] [blame] | 198 | _GCLIENT_SCHEMA.validate(local_scope) |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 19:46:57 | [diff] [blame^] | 199 | |
| 200 | |
| 201 | def EvaluateCondition(condition, variables, referenced_variables=None): |
| 202 | """Safely evaluates a boolean condition. Returns the result.""" |
| 203 | if not referenced_variables: |
| 204 | referenced_variables = set() |
| 205 | _allowed_names = {'None': None, 'True': True, 'False': False} |
| 206 | main_node = ast.parse(condition, mode='eval') |
| 207 | if isinstance(main_node, ast.Expression): |
| 208 | main_node = main_node.body |
| 209 | def _convert(node): |
| 210 | if isinstance(node, ast.Str): |
| 211 | return node.s |
| 212 | elif isinstance(node, ast.Name): |
| 213 | if node.id in referenced_variables: |
| 214 | raise ValueError( |
| 215 | 'invalid cyclic reference to %r (inside %r)' % ( |
| 216 | node.id, condition)) |
| 217 | elif node.id in _allowed_names: |
| 218 | return _allowed_names[node.id] |
| 219 | elif node.id in variables: |
| 220 | return EvaluateCondition( |
| 221 | variables[node.id], |
| 222 | variables, |
| 223 | referenced_variables.union([node.id])) |
| 224 | else: |
| 225 | raise ValueError( |
| 226 | 'invalid name %r (inside %r)' % (node.id, condition)) |
| 227 | elif isinstance(node, ast.BoolOp) and isinstance(node.op, ast.Or): |
| 228 | if len(node.values) != 2: |
| 229 | raise ValueError( |
| 230 | 'invalid "or": exactly 2 operands required (inside %r)' % ( |
| 231 | condition)) |
| 232 | return _convert(node.values[0]) or _convert(node.values[1]) |
| 233 | elif isinstance(node, ast.BoolOp) and isinstance(node.op, ast.And): |
| 234 | if len(node.values) != 2: |
| 235 | raise ValueError( |
| 236 | 'invalid "and": exactly 2 operands required (inside %r)' % ( |
| 237 | condition)) |
| 238 | return _convert(node.values[0]) and _convert(node.values[1]) |
| 239 | elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not): |
| 240 | return not _convert(node.operand) |
| 241 | elif isinstance(node, ast.Compare): |
| 242 | if len(node.ops) != 1: |
| 243 | raise ValueError( |
| 244 | 'invalid compare: exactly 1 operator required (inside %r)' % ( |
| 245 | condition)) |
| 246 | if len(node.comparators) != 1: |
| 247 | raise ValueError( |
| 248 | 'invalid compare: exactly 1 comparator required (inside %r)' % ( |
| 249 | condition)) |
| 250 | |
| 251 | left = _convert(node.left) |
| 252 | right = _convert(node.comparators[0]) |
| 253 | |
| 254 | if isinstance(node.ops[0], ast.Eq): |
| 255 | return left == right |
| 256 | |
| 257 | raise ValueError( |
| 258 | 'unexpected operator: %s %s (inside %r)' % ( |
| 259 | node.ops[0], ast.dump(node), condition)) |
| 260 | else: |
| 261 | raise ValueError( |
| 262 | 'unexpected AST node: %s %s (inside %r)' % ( |
| 263 | node, ast.dump(node), condition)) |
| 264 | return _convert(main_node) |