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. |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 12 | _GCLIENT_DEPS_SCHEMA = { |
| 13 | schema.Optional(basestring): schema.Or( |
| 14 | None, |
| 15 | basestring, |
| 16 | { |
| 17 | # Repo and revision to check out under the path |
| 18 | # (same as if no dict was used). |
| 19 | 'url': basestring, |
| 20 | |
| 21 | # Optional condition string. The dep will only be processed |
| 22 | # if the condition evaluates to True. |
| 23 | schema.Optional('condition'): basestring, |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame^] | 24 | |
| 25 | schema.Optional('dep_type', default='git'): basestring, |
| 26 | }, |
| 27 | # CIPD package. |
| 28 | { |
| 29 | 'packages': [ |
| 30 | { |
| 31 | 'package': basestring, |
| 32 | |
| 33 | 'version': basestring, |
| 34 | } |
| 35 | ], |
| 36 | |
| 37 | schema.Optional('condition'): basestring, |
| 38 | |
| 39 | schema.Optional('dep_type', default='cipd'): basestring, |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 40 | }, |
| 41 | ), |
| 42 | } |
| 43 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 44 | _GCLIENT_HOOKS_SCHEMA = [{ |
| 45 | # Hook action: list of command-line arguments to invoke. |
| 46 | 'action': [basestring], |
| 47 | |
| 48 | # Name of the hook. Doesn't affect operation. |
| 49 | schema.Optional('name'): basestring, |
| 50 | |
| 51 | # Hook pattern (regex). Originally intended to limit some hooks to run |
| 52 | # only when files matching the pattern have changed. In practice, with git, |
| 53 | # gclient runs all the hooks regardless of this field. |
| 54 | schema.Optional('pattern'): basestring, |
Paweł Hajdan, Jr | c936439 | 2017-06-14 17:11:56 +0200 | [diff] [blame] | 55 | |
| 56 | # Working directory where to execute the hook. |
| 57 | schema.Optional('cwd'): basestring, |
Paweł Hajdan, Jr | 032d545 | 2017-06-22 20:43:53 +0200 | [diff] [blame] | 58 | |
| 59 | # Optional condition string. The hook will only be run |
| 60 | # if the condition evaluates to True. |
| 61 | schema.Optional('condition'): basestring, |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 62 | }] |
| 63 | |
| 64 | _GCLIENT_SCHEMA = schema.Schema({ |
| 65 | # List of host names from which dependencies are allowed (whitelist). |
| 66 | # NOTE: when not present, all hosts are allowed. |
| 67 | # NOTE: scoped to current DEPS file, not recursive. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 68 | schema.Optional('allowed_hosts'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 69 | |
| 70 | # Mapping from paths to repo and revision to check out under that path. |
| 71 | # Applying this mapping to the on-disk checkout is the main purpose |
| 72 | # of gclient, and also why the config file is called DEPS. |
| 73 | # |
| 74 | # The following functions are allowed: |
| 75 | # |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 76 | # Var(): allows variable substitution (either from 'vars' dict below, |
| 77 | # or command-line override) |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 78 | schema.Optional('deps'): _GCLIENT_DEPS_SCHEMA, |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 79 | |
| 80 | # 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] | 81 | # Also see 'target_os'. |
| 82 | schema.Optional('deps_os'): { |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 83 | schema.Optional(basestring): _GCLIENT_DEPS_SCHEMA, |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 84 | }, |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 85 | |
Paweł Hajdan, Jr | 5725373 | 2017-06-06 23:49:11 +0200 | [diff] [blame] | 86 | # Path to GN args file to write selected variables. |
| 87 | schema.Optional('gclient_gn_args_file'): basestring, |
| 88 | |
| 89 | # Subset of variables to write to the GN args file (see above). |
| 90 | schema.Optional('gclient_gn_args'): [schema.Optional(basestring)], |
| 91 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 92 | # Hooks executed after gclient sync (unless suppressed), or explicitly |
| 93 | # on gclient hooks. See _GCLIENT_HOOKS_SCHEMA for details. |
| 94 | # Also see 'pre_deps_hooks'. |
| 95 | schema.Optional('hooks'): _GCLIENT_HOOKS_SCHEMA, |
| 96 | |
Scott Graham | c482674 | 2017-05-11 16:59:23 -0700 | [diff] [blame] | 97 | # Similar to 'hooks', also keyed by OS. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 98 | schema.Optional('hooks_os'): { |
| 99 | schema.Optional(basestring): _GCLIENT_HOOKS_SCHEMA |
| 100 | }, |
Scott Graham | c482674 | 2017-05-11 16:59:23 -0700 | [diff] [blame] | 101 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 102 | # Rules which #includes are allowed in the directory. |
| 103 | # Also see 'skip_child_includes' and 'specific_include_rules'. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 104 | schema.Optional('include_rules'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 105 | |
| 106 | # Hooks executed before processing DEPS. See 'hooks' for more details. |
| 107 | schema.Optional('pre_deps_hooks'): _GCLIENT_HOOKS_SCHEMA, |
| 108 | |
Paweł Hajdan, Jr | 6f79679 | 2017-06-02 08:40:06 +0200 | [diff] [blame] | 109 | # Recursion limit for nested DEPS. |
| 110 | schema.Optional('recursion'): int, |
| 111 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 112 | # Whitelists deps for which recursion should be enabled. |
| 113 | schema.Optional('recursedeps'): [ |
Paweł Hajdan, Jr | 05fec03 | 2017-05-30 23:04:23 +0200 | [diff] [blame] | 114 | schema.Optional(schema.Or( |
| 115 | basestring, |
| 116 | (basestring, basestring), |
| 117 | [basestring, basestring] |
| 118 | )), |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 119 | ], |
| 120 | |
| 121 | # Blacklists directories for checking 'include_rules'. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 122 | schema.Optional('skip_child_includes'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 123 | |
| 124 | # Mapping from paths to include rules specific for that path. |
| 125 | # See 'include_rules' for more details. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 126 | schema.Optional('specific_include_rules'): { |
| 127 | schema.Optional(basestring): [basestring] |
| 128 | }, |
| 129 | |
| 130 | # List of additional OS names to consider when selecting dependencies |
| 131 | # from deps_os. |
| 132 | schema.Optional('target_os'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 133 | |
| 134 | # For recursed-upon sub-dependencies, check out their own dependencies |
| 135 | # relative to the paren't path, rather than relative to the .gclient file. |
| 136 | schema.Optional('use_relative_paths'): bool, |
| 137 | |
| 138 | # Variables that can be referenced using Var() - see 'deps'. |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 139 | schema.Optional('vars'): { |
| 140 | schema.Optional(basestring): schema.Or(basestring, bool), |
| 141 | }, |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 142 | }) |
| 143 | |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 144 | |
| 145 | def _gclient_eval(node_or_string, global_scope, filename='<unknown>'): |
| 146 | """Safely evaluates a single expression. Returns the result.""" |
| 147 | _allowed_names = {'None': None, 'True': True, 'False': False} |
| 148 | if isinstance(node_or_string, basestring): |
| 149 | node_or_string = ast.parse(node_or_string, filename=filename, mode='eval') |
| 150 | if isinstance(node_or_string, ast.Expression): |
| 151 | node_or_string = node_or_string.body |
| 152 | def _convert(node): |
| 153 | if isinstance(node, ast.Str): |
| 154 | return node.s |
Paweł Hajdan, Jr | 6f79679 | 2017-06-02 08:40:06 +0200 | [diff] [blame] | 155 | elif isinstance(node, ast.Num): |
| 156 | return node.n |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 157 | elif isinstance(node, ast.Tuple): |
| 158 | return tuple(map(_convert, node.elts)) |
| 159 | elif isinstance(node, ast.List): |
| 160 | return list(map(_convert, node.elts)) |
| 161 | elif isinstance(node, ast.Dict): |
Paweł Hajdan, Jr | 7cf96a4 | 2017-05-26 20:28:35 +0200 | [diff] [blame] | 162 | return collections.OrderedDict( |
| 163 | (_convert(k), _convert(v)) |
| 164 | for k, v in zip(node.keys, node.values)) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 165 | elif isinstance(node, ast.Name): |
| 166 | if node.id not in _allowed_names: |
| 167 | raise ValueError( |
| 168 | 'invalid name %r (file %r, line %s)' % ( |
| 169 | node.id, filename, getattr(node, 'lineno', '<unknown>'))) |
| 170 | return _allowed_names[node.id] |
| 171 | elif isinstance(node, ast.Call): |
| 172 | if not isinstance(node.func, ast.Name): |
| 173 | raise ValueError( |
| 174 | 'invalid call: func should be a name (file %r, line %s)' % ( |
| 175 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 176 | if node.keywords or node.starargs or node.kwargs: |
| 177 | raise ValueError( |
| 178 | 'invalid call: use only regular args (file %r, line %s)' % ( |
| 179 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 180 | args = map(_convert, node.args) |
| 181 | return global_scope[node.func.id](*args) |
| 182 | elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add): |
| 183 | return _convert(node.left) + _convert(node.right) |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 184 | elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Mod): |
| 185 | return _convert(node.left) % _convert(node.right) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 186 | else: |
| 187 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 188 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
| 189 | node, ast.dump(node), filename, |
| 190 | getattr(node, 'lineno', '<unknown>'))) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 191 | return _convert(node_or_string) |
| 192 | |
| 193 | |
Paweł Hajdan, Jr | c485d5a | 2017-06-02 12:08:09 +0200 | [diff] [blame] | 194 | def Exec(content, global_scope, local_scope, filename='<unknown>'): |
| 195 | """Safely execs a set of assignments. Mutates |local_scope|.""" |
| 196 | node_or_string = ast.parse(content, filename=filename, mode='exec') |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 197 | if isinstance(node_or_string, ast.Expression): |
| 198 | node_or_string = node_or_string.body |
| 199 | |
| 200 | def _visit_in_module(node): |
| 201 | if isinstance(node, ast.Assign): |
| 202 | if len(node.targets) != 1: |
| 203 | raise ValueError( |
| 204 | 'invalid assignment: use exactly one target (file %r, line %s)' % ( |
| 205 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 206 | target = node.targets[0] |
| 207 | if not isinstance(target, ast.Name): |
| 208 | raise ValueError( |
| 209 | 'invalid assignment: target should be a name (file %r, line %s)' % ( |
| 210 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 211 | value = _gclient_eval(node.value, global_scope, filename=filename) |
| 212 | |
Paweł Hajdan, Jr | c485d5a | 2017-06-02 12:08:09 +0200 | [diff] [blame] | 213 | if target.id in local_scope: |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 214 | raise ValueError( |
| 215 | 'invalid assignment: overrides var %r (file %r, line %s)' % ( |
| 216 | target.id, filename, getattr(node, 'lineno', '<unknown>'))) |
| 217 | |
Paweł Hajdan, Jr | c485d5a | 2017-06-02 12:08:09 +0200 | [diff] [blame] | 218 | local_scope[target.id] = value |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 219 | else: |
| 220 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 221 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
| 222 | node, ast.dump(node), filename, |
| 223 | getattr(node, 'lineno', '<unknown>'))) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 224 | |
| 225 | if isinstance(node_or_string, ast.Module): |
| 226 | for stmt in node_or_string.body: |
| 227 | _visit_in_module(stmt) |
| 228 | else: |
| 229 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 230 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 231 | node_or_string, |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 232 | ast.dump(node_or_string), |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 233 | filename, |
| 234 | getattr(node_or_string, 'lineno', '<unknown>'))) |
| 235 | |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame^] | 236 | return _GCLIENT_SCHEMA.validate(local_scope) |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 237 | |
| 238 | |
| 239 | def EvaluateCondition(condition, variables, referenced_variables=None): |
| 240 | """Safely evaluates a boolean condition. Returns the result.""" |
| 241 | if not referenced_variables: |
| 242 | referenced_variables = set() |
| 243 | _allowed_names = {'None': None, 'True': True, 'False': False} |
| 244 | main_node = ast.parse(condition, mode='eval') |
| 245 | if isinstance(main_node, ast.Expression): |
| 246 | main_node = main_node.body |
| 247 | def _convert(node): |
| 248 | if isinstance(node, ast.Str): |
| 249 | return node.s |
| 250 | elif isinstance(node, ast.Name): |
| 251 | if node.id in referenced_variables: |
| 252 | raise ValueError( |
| 253 | 'invalid cyclic reference to %r (inside %r)' % ( |
| 254 | node.id, condition)) |
| 255 | elif node.id in _allowed_names: |
| 256 | return _allowed_names[node.id] |
| 257 | elif node.id in variables: |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 258 | value = variables[node.id] |
| 259 | |
| 260 | # Allow using "native" types, without wrapping everything in strings. |
| 261 | # Note that schema constraints still apply to variables. |
| 262 | if not isinstance(value, basestring): |
| 263 | return value |
| 264 | |
| 265 | # Recursively evaluate the variable reference. |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 266 | return EvaluateCondition( |
| 267 | variables[node.id], |
| 268 | variables, |
| 269 | referenced_variables.union([node.id])) |
| 270 | else: |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 271 | # Implicitly convert unrecognized names to strings. |
| 272 | # If we want to change this, we'll need to explicitly distinguish |
| 273 | # between arguments for GN to be passed verbatim, and ones to |
| 274 | # be evaluated. |
| 275 | return node.id |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 276 | elif isinstance(node, ast.BoolOp) and isinstance(node.op, ast.Or): |
| 277 | if len(node.values) != 2: |
| 278 | raise ValueError( |
| 279 | 'invalid "or": exactly 2 operands required (inside %r)' % ( |
| 280 | condition)) |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 281 | left = _convert(node.values[0]) |
| 282 | right = _convert(node.values[1]) |
| 283 | if not isinstance(left, bool): |
| 284 | raise ValueError( |
| 285 | 'invalid "or" operand %r (inside %r)' % (left, condition)) |
| 286 | if not isinstance(right, bool): |
| 287 | raise ValueError( |
| 288 | 'invalid "or" operand %r (inside %r)' % (right, condition)) |
| 289 | return left or right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 290 | elif isinstance(node, ast.BoolOp) and isinstance(node.op, ast.And): |
| 291 | if len(node.values) != 2: |
| 292 | raise ValueError( |
| 293 | 'invalid "and": exactly 2 operands required (inside %r)' % ( |
| 294 | condition)) |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 295 | left = _convert(node.values[0]) |
| 296 | right = _convert(node.values[1]) |
| 297 | if not isinstance(left, bool): |
| 298 | raise ValueError( |
| 299 | 'invalid "and" operand %r (inside %r)' % (left, condition)) |
| 300 | if not isinstance(right, bool): |
| 301 | raise ValueError( |
| 302 | 'invalid "and" operand %r (inside %r)' % (right, condition)) |
| 303 | return left and right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 304 | elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not): |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 305 | value = _convert(node.operand) |
| 306 | if not isinstance(value, bool): |
| 307 | raise ValueError( |
| 308 | 'invalid "not" operand %r (inside %r)' % (value, condition)) |
| 309 | return not value |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 310 | elif isinstance(node, ast.Compare): |
| 311 | if len(node.ops) != 1: |
| 312 | raise ValueError( |
| 313 | 'invalid compare: exactly 1 operator required (inside %r)' % ( |
| 314 | condition)) |
| 315 | if len(node.comparators) != 1: |
| 316 | raise ValueError( |
| 317 | 'invalid compare: exactly 1 comparator required (inside %r)' % ( |
| 318 | condition)) |
| 319 | |
| 320 | left = _convert(node.left) |
| 321 | right = _convert(node.comparators[0]) |
| 322 | |
| 323 | if isinstance(node.ops[0], ast.Eq): |
| 324 | return left == right |
Dirk Pranke | 77b7687 | 2017-10-05 18:29:27 -0700 | [diff] [blame] | 325 | if isinstance(node.ops[0], ast.NotEq): |
| 326 | return left != right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 327 | |
| 328 | raise ValueError( |
| 329 | 'unexpected operator: %s %s (inside %r)' % ( |
| 330 | node.ops[0], ast.dump(node), condition)) |
| 331 | else: |
| 332 | raise ValueError( |
| 333 | 'unexpected AST node: %s %s (inside %r)' % ( |
| 334 | node, ast.dump(node), condition)) |
| 335 | return _convert(main_node) |