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