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 |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 6 | import cStringIO |
Paweł Hajdan, Jr | 7cf96a4 | 2017-05-26 20:28:35 +0200 | [diff] [blame] | 7 | import collections |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 8 | import tokenize |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 9 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 10 | from third_party import schema |
| 11 | |
| 12 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 13 | class _NodeDict(collections.MutableMapping): |
| 14 | """Dict-like type that also stores information on AST nodes and tokens.""" |
| 15 | def __init__(self, data, tokens=None): |
| 16 | self.data = collections.OrderedDict(data) |
| 17 | self.tokens = tokens |
| 18 | |
| 19 | def __str__(self): |
| 20 | return str({k: v[0] for k, v in self.data.iteritems()}) |
| 21 | |
| 22 | def __getitem__(self, key): |
| 23 | return self.data[key][0] |
| 24 | |
| 25 | def __setitem__(self, key, value): |
| 26 | self.data[key] = (value, None) |
| 27 | |
| 28 | def __delitem__(self, key): |
| 29 | del self.data[key] |
| 30 | |
| 31 | def __iter__(self): |
| 32 | return iter(self.data) |
| 33 | |
| 34 | def __len__(self): |
| 35 | return len(self.data) |
| 36 | |
Edward Lesmes | 3d99381 | 2018-04-02 12:52:49 -0400 | [diff] [blame] | 37 | def MoveTokens(self, origin, delta): |
| 38 | if self.tokens: |
| 39 | new_tokens = {} |
| 40 | for pos, token in self.tokens.iteritems(): |
| 41 | if pos[0] >= origin: |
| 42 | pos = (pos[0] + delta, pos[1]) |
| 43 | token = token[:2] + (pos,) + token[3:] |
| 44 | new_tokens[pos] = token |
| 45 | |
| 46 | for value, node in self.data.values(): |
| 47 | if node.lineno >= origin: |
| 48 | node.lineno += delta |
| 49 | if isinstance(value, _NodeDict): |
| 50 | value.MoveTokens(origin, delta) |
| 51 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 52 | def GetNode(self, key): |
| 53 | return self.data[key][1] |
| 54 | |
Edward Lesmes | 6c24d37 | 2018-03-28 12:52:29 -0400 | [diff] [blame] | 55 | def SetNode(self, key, value, node): |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 56 | self.data[key] = (value, node) |
| 57 | |
| 58 | |
| 59 | def _NodeDictSchema(dict_schema): |
| 60 | """Validate dict_schema after converting _NodeDict to a regular dict.""" |
| 61 | def validate(d): |
| 62 | schema.Schema(dict_schema).validate(dict(d)) |
| 63 | return True |
| 64 | return validate |
| 65 | |
| 66 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 67 | # See https://github.com/keleshev/schema for docs how to configure schema. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 68 | _GCLIENT_DEPS_SCHEMA = _NodeDictSchema({ |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 69 | schema.Optional(basestring): schema.Or( |
| 70 | None, |
| 71 | basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 72 | _NodeDictSchema({ |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 73 | # Repo and revision to check out under the path |
| 74 | # (same as if no dict was used). |
Michael Moss | 012013e | 2018-03-30 17:03:19 -0700 | [diff] [blame] | 75 | 'url': schema.Or(None, basestring), |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 76 | |
| 77 | # Optional condition string. The dep will only be processed |
| 78 | # if the condition evaluates to True. |
| 79 | schema.Optional('condition'): basestring, |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 80 | |
| 81 | schema.Optional('dep_type', default='git'): basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 82 | }), |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 83 | # CIPD package. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 84 | _NodeDictSchema({ |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 85 | 'packages': [ |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 86 | _NodeDictSchema({ |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 87 | 'package': basestring, |
| 88 | |
| 89 | 'version': basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 90 | }) |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 91 | ], |
| 92 | |
| 93 | schema.Optional('condition'): basestring, |
| 94 | |
| 95 | schema.Optional('dep_type', default='cipd'): basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 96 | }), |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 97 | ), |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 98 | }) |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 99 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 100 | _GCLIENT_HOOKS_SCHEMA = [_NodeDictSchema({ |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 101 | # Hook action: list of command-line arguments to invoke. |
| 102 | 'action': [basestring], |
| 103 | |
| 104 | # Name of the hook. Doesn't affect operation. |
| 105 | schema.Optional('name'): basestring, |
| 106 | |
| 107 | # Hook pattern (regex). Originally intended to limit some hooks to run |
| 108 | # only when files matching the pattern have changed. In practice, with git, |
| 109 | # gclient runs all the hooks regardless of this field. |
| 110 | schema.Optional('pattern'): basestring, |
Paweł Hajdan, Jr | c936439 | 2017-06-14 17:11:56 +0200 | [diff] [blame] | 111 | |
| 112 | # Working directory where to execute the hook. |
| 113 | schema.Optional('cwd'): basestring, |
Paweł Hajdan, Jr | 032d545 | 2017-06-22 20:43:53 +0200 | [diff] [blame] | 114 | |
| 115 | # Optional condition string. The hook will only be run |
| 116 | # if the condition evaluates to True. |
| 117 | schema.Optional('condition'): basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 118 | })] |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 119 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 120 | _GCLIENT_SCHEMA = schema.Schema(_NodeDictSchema({ |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 121 | # List of host names from which dependencies are allowed (whitelist). |
| 122 | # NOTE: when not present, all hosts are allowed. |
| 123 | # NOTE: scoped to current DEPS file, not recursive. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 124 | schema.Optional('allowed_hosts'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 125 | |
| 126 | # Mapping from paths to repo and revision to check out under that path. |
| 127 | # Applying this mapping to the on-disk checkout is the main purpose |
| 128 | # of gclient, and also why the config file is called DEPS. |
| 129 | # |
| 130 | # The following functions are allowed: |
| 131 | # |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 132 | # Var(): allows variable substitution (either from 'vars' dict below, |
| 133 | # or command-line override) |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 134 | schema.Optional('deps'): _GCLIENT_DEPS_SCHEMA, |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 135 | |
| 136 | # 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] | 137 | # Also see 'target_os'. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 138 | schema.Optional('deps_os'): _NodeDictSchema({ |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 139 | schema.Optional(basestring): _GCLIENT_DEPS_SCHEMA, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 140 | }), |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 141 | |
Paweł Hajdan, Jr | 5725373 | 2017-06-06 23:49:11 +0200 | [diff] [blame] | 142 | # Path to GN args file to write selected variables. |
| 143 | schema.Optional('gclient_gn_args_file'): basestring, |
| 144 | |
| 145 | # Subset of variables to write to the GN args file (see above). |
| 146 | schema.Optional('gclient_gn_args'): [schema.Optional(basestring)], |
| 147 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 148 | # Hooks executed after gclient sync (unless suppressed), or explicitly |
| 149 | # on gclient hooks. See _GCLIENT_HOOKS_SCHEMA for details. |
| 150 | # Also see 'pre_deps_hooks'. |
| 151 | schema.Optional('hooks'): _GCLIENT_HOOKS_SCHEMA, |
| 152 | |
Scott Graham | c482674 | 2017-05-11 16:59:23 -0700 | [diff] [blame] | 153 | # Similar to 'hooks', also keyed by OS. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 154 | schema.Optional('hooks_os'): _NodeDictSchema({ |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 155 | schema.Optional(basestring): _GCLIENT_HOOKS_SCHEMA |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 156 | }), |
Scott Graham | c482674 | 2017-05-11 16:59:23 -0700 | [diff] [blame] | 157 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 158 | # Rules which #includes are allowed in the directory. |
| 159 | # Also see 'skip_child_includes' and 'specific_include_rules'. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 160 | schema.Optional('include_rules'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 161 | |
| 162 | # Hooks executed before processing DEPS. See 'hooks' for more details. |
| 163 | schema.Optional('pre_deps_hooks'): _GCLIENT_HOOKS_SCHEMA, |
| 164 | |
Paweł Hajdan, Jr | 6f79679 | 2017-06-02 08:40:06 +0200 | [diff] [blame] | 165 | # Recursion limit for nested DEPS. |
| 166 | schema.Optional('recursion'): int, |
| 167 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 168 | # Whitelists deps for which recursion should be enabled. |
| 169 | schema.Optional('recursedeps'): [ |
Paweł Hajdan, Jr | 05fec03 | 2017-05-30 23:04:23 +0200 | [diff] [blame] | 170 | schema.Optional(schema.Or( |
| 171 | basestring, |
| 172 | (basestring, basestring), |
| 173 | [basestring, basestring] |
| 174 | )), |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 175 | ], |
| 176 | |
| 177 | # Blacklists directories for checking 'include_rules'. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 178 | schema.Optional('skip_child_includes'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 179 | |
| 180 | # Mapping from paths to include rules specific for that path. |
| 181 | # See 'include_rules' for more details. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 182 | schema.Optional('specific_include_rules'): _NodeDictSchema({ |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 183 | schema.Optional(basestring): [basestring] |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 184 | }), |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 185 | |
| 186 | # List of additional OS names to consider when selecting dependencies |
| 187 | # from deps_os. |
| 188 | schema.Optional('target_os'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 189 | |
| 190 | # For recursed-upon sub-dependencies, check out their own dependencies |
| 191 | # relative to the paren't path, rather than relative to the .gclient file. |
| 192 | schema.Optional('use_relative_paths'): bool, |
| 193 | |
| 194 | # Variables that can be referenced using Var() - see 'deps'. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 195 | schema.Optional('vars'): _NodeDictSchema({ |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 196 | schema.Optional(basestring): schema.Or(basestring, bool), |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 197 | }), |
| 198 | })) |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 199 | |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 200 | |
Edward Lesmes | 6c24d37 | 2018-03-28 12:52:29 -0400 | [diff] [blame] | 201 | def _gclient_eval(node_or_string, vars_dict=None, expand_vars=False, |
| 202 | filename='<unknown>'): |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 203 | """Safely evaluates a single expression. Returns the result.""" |
| 204 | _allowed_names = {'None': None, 'True': True, 'False': False} |
| 205 | if isinstance(node_or_string, basestring): |
| 206 | node_or_string = ast.parse(node_or_string, filename=filename, mode='eval') |
| 207 | if isinstance(node_or_string, ast.Expression): |
| 208 | node_or_string = node_or_string.body |
| 209 | def _convert(node): |
| 210 | if isinstance(node, ast.Str): |
Edward Lesmes | 6c24d37 | 2018-03-28 12:52:29 -0400 | [diff] [blame] | 211 | if not expand_vars: |
| 212 | return node.s |
| 213 | try: |
| 214 | return node.s.format(**vars_dict) |
| 215 | except KeyError as e: |
| 216 | raise ValueError( |
| 217 | '%s was used as a variable, but was not declared in the vars dict ' |
| 218 | '(file %r, line %s)' % ( |
| 219 | e.message, filename, getattr(node, 'lineno', '<unknown>'))) |
Paweł Hajdan, Jr | 6f79679 | 2017-06-02 08:40:06 +0200 | [diff] [blame] | 220 | elif isinstance(node, ast.Num): |
| 221 | return node.n |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 222 | elif isinstance(node, ast.Tuple): |
| 223 | return tuple(map(_convert, node.elts)) |
| 224 | elif isinstance(node, ast.List): |
| 225 | return list(map(_convert, node.elts)) |
| 226 | elif isinstance(node, ast.Dict): |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 227 | return _NodeDict((_convert(k), (_convert(v), v)) |
Paweł Hajdan, Jr | 7cf96a4 | 2017-05-26 20:28:35 +0200 | [diff] [blame] | 228 | for k, v in zip(node.keys, node.values)) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 229 | elif isinstance(node, ast.Name): |
| 230 | if node.id not in _allowed_names: |
| 231 | raise ValueError( |
| 232 | 'invalid name %r (file %r, line %s)' % ( |
| 233 | node.id, filename, getattr(node, 'lineno', '<unknown>'))) |
| 234 | return _allowed_names[node.id] |
| 235 | elif isinstance(node, ast.Call): |
Edward Lesmes | 9f53129 | 2018-03-20 21:27:15 -0400 | [diff] [blame] | 236 | if not isinstance(node.func, ast.Name) or node.func.id != 'Var': |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 237 | raise ValueError( |
Edward Lesmes | 9f53129 | 2018-03-20 21:27:15 -0400 | [diff] [blame] | 238 | 'Var is the only allowed function (file %r, line %s)' % ( |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 239 | filename, getattr(node, 'lineno', '<unknown>'))) |
Edward Lesmes | 9f53129 | 2018-03-20 21:27:15 -0400 | [diff] [blame] | 240 | if node.keywords or node.starargs or node.kwargs or len(node.args) != 1: |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 241 | raise ValueError( |
Edward Lesmes | 9f53129 | 2018-03-20 21:27:15 -0400 | [diff] [blame] | 242 | 'Var takes exactly one argument (file %r, line %s)' % ( |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 243 | filename, getattr(node, 'lineno', '<unknown>'))) |
Edward Lesmes | 9f53129 | 2018-03-20 21:27:15 -0400 | [diff] [blame] | 244 | arg = _convert(node.args[0]) |
| 245 | if not isinstance(arg, basestring): |
| 246 | raise ValueError( |
| 247 | 'Var\'s argument must be a variable name (file %r, line %s)' % ( |
| 248 | filename, getattr(node, 'lineno', '<unknown>'))) |
Edward Lesmes | 6c24d37 | 2018-03-28 12:52:29 -0400 | [diff] [blame] | 249 | if not expand_vars: |
| 250 | return '{%s}' % arg |
| 251 | if vars_dict is None: |
| 252 | raise ValueError( |
| 253 | 'vars must be declared before Var can be used (file %r, line %s)' |
| 254 | % (filename, getattr(node, 'lineno', '<unknown>'))) |
| 255 | if arg not in vars_dict: |
| 256 | raise ValueError( |
| 257 | '%s was used as a variable, but was not declared in the vars dict ' |
| 258 | '(file %r, line %s)' % ( |
| 259 | arg, filename, getattr(node, 'lineno', '<unknown>'))) |
| 260 | return vars_dict[arg] |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 261 | elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add): |
| 262 | return _convert(node.left) + _convert(node.right) |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 263 | elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Mod): |
| 264 | return _convert(node.left) % _convert(node.right) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 265 | else: |
| 266 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 267 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
| 268 | node, ast.dump(node), filename, |
| 269 | getattr(node, 'lineno', '<unknown>'))) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 270 | return _convert(node_or_string) |
| 271 | |
| 272 | |
Edward Lesmes | 6c24d37 | 2018-03-28 12:52:29 -0400 | [diff] [blame] | 273 | def Exec(content, expand_vars=True, filename='<unknown>', vars_override=None): |
| 274 | """Safely execs a set of assignments.""" |
| 275 | def _validate_statement(node, local_scope): |
| 276 | if not isinstance(node, ast.Assign): |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 277 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 278 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
| 279 | node, ast.dump(node), filename, |
| 280 | getattr(node, 'lineno', '<unknown>'))) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 281 | |
Edward Lesmes | 6c24d37 | 2018-03-28 12:52:29 -0400 | [diff] [blame] | 282 | if len(node.targets) != 1: |
| 283 | raise ValueError( |
| 284 | 'invalid assignment: use exactly one target (file %r, line %s)' % ( |
| 285 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 286 | |
| 287 | target = node.targets[0] |
| 288 | if not isinstance(target, ast.Name): |
| 289 | raise ValueError( |
| 290 | 'invalid assignment: target should be a name (file %r, line %s)' % ( |
| 291 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 292 | if target.id in local_scope: |
| 293 | raise ValueError( |
| 294 | 'invalid assignment: overrides var %r (file %r, line %s)' % ( |
| 295 | target.id, filename, getattr(node, 'lineno', '<unknown>'))) |
| 296 | |
| 297 | node_or_string = ast.parse(content, filename=filename, mode='exec') |
| 298 | if isinstance(node_or_string, ast.Expression): |
| 299 | node_or_string = node_or_string.body |
| 300 | |
| 301 | if not isinstance(node_or_string, ast.Module): |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 302 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 303 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 304 | node_or_string, |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 305 | ast.dump(node_or_string), |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 306 | filename, |
| 307 | getattr(node_or_string, 'lineno', '<unknown>'))) |
| 308 | |
Edward Lesmes | 6c24d37 | 2018-03-28 12:52:29 -0400 | [diff] [blame] | 309 | statements = {} |
| 310 | for statement in node_or_string.body: |
| 311 | _validate_statement(statement, statements) |
| 312 | statements[statement.targets[0].id] = statement.value |
| 313 | |
| 314 | tokens = { |
| 315 | token[2]: list(token) |
| 316 | for token in tokenize.generate_tokens( |
| 317 | cStringIO.StringIO(content).readline) |
| 318 | } |
| 319 | local_scope = _NodeDict({}, tokens) |
| 320 | |
| 321 | # Process vars first, so we can expand variables in the rest of the DEPS file. |
| 322 | vars_dict = {} |
| 323 | if 'vars' in statements: |
| 324 | vars_statement = statements['vars'] |
| 325 | value = _gclient_eval(vars_statement, None, False, filename) |
| 326 | local_scope.SetNode('vars', value, vars_statement) |
| 327 | # Update the parsed vars with the overrides, but only if they are already |
| 328 | # present (overrides do not introduce new variables). |
| 329 | vars_dict.update(value) |
| 330 | if vars_override: |
| 331 | vars_dict.update({ |
| 332 | k: v |
| 333 | for k, v in vars_override.iteritems() |
| 334 | if k in vars_dict}) |
| 335 | |
| 336 | for name, node in statements.iteritems(): |
| 337 | value = _gclient_eval(node, vars_dict, expand_vars, filename) |
| 338 | local_scope.SetNode(name, value, node) |
| 339 | |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 340 | return _GCLIENT_SCHEMA.validate(local_scope) |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 341 | |
| 342 | |
Edward Lesmes | 6c24d37 | 2018-03-28 12:52:29 -0400 | [diff] [blame] | 343 | def Parse(content, expand_vars, validate_syntax, filename, vars_override=None): |
| 344 | """Parses DEPS strings. |
| 345 | |
| 346 | Executes the Python-like string stored in content, resulting in a Python |
| 347 | dictionary specifyied by the schema above. Supports syntax validation and |
| 348 | variable expansion. |
| 349 | |
| 350 | Args: |
| 351 | content: str. DEPS file stored as a string. |
| 352 | expand_vars: bool. Whether variables should be expanded to their values. |
| 353 | validate_syntax: bool. Whether syntax should be validated using the schema |
| 354 | defined above. |
| 355 | filename: str. The name of the DEPS file, or a string describing the source |
| 356 | of the content, e.g. '<string>', '<unknown>'. |
| 357 | vars_override: dict, optional. A dictionary with overrides for the variables |
| 358 | defined by the DEPS file. |
| 359 | |
| 360 | Returns: |
| 361 | A Python dict with the parsed contents of the DEPS file, as specified by the |
| 362 | schema above. |
| 363 | """ |
| 364 | # TODO(ehmaldonado): Make validate_syntax = True the only case |
| 365 | if validate_syntax: |
| 366 | return Exec(content, expand_vars, filename, vars_override) |
| 367 | |
| 368 | local_scope = {} |
| 369 | global_scope = {'Var': lambda var_name: '{%s}' % var_name} |
| 370 | |
| 371 | # If we use 'exec' directly, it complains that 'Parse' contains a nested |
| 372 | # function with free variables. |
| 373 | # This is because on versions of Python < 2.7.9, "exec(a, b, c)" not the same |
| 374 | # as "exec a in b, c" (See https://bugs.python.org/issue21591). |
| 375 | eval(compile(content, filename, 'exec'), global_scope, local_scope) |
| 376 | |
| 377 | if 'vars' not in local_scope or not expand_vars: |
| 378 | return local_scope |
| 379 | |
| 380 | vars_dict = {} |
| 381 | vars_dict.update(local_scope['vars']) |
| 382 | if vars_override: |
| 383 | vars_dict.update({ |
| 384 | k: v |
| 385 | for k, v in vars_override.iteritems() |
| 386 | if k in vars_dict |
| 387 | }) |
| 388 | |
| 389 | def _DeepFormat(node): |
| 390 | if isinstance(node, basestring): |
| 391 | return node.format(**vars_dict) |
| 392 | elif isinstance(node, dict): |
| 393 | return { |
| 394 | k.format(**vars_dict): _DeepFormat(v) |
| 395 | for k, v in node.iteritems() |
| 396 | } |
| 397 | elif isinstance(node, list): |
| 398 | return [_DeepFormat(elem) for elem in node] |
| 399 | elif isinstance(node, tuple): |
| 400 | return tuple(_DeepFormat(elem) for elem in node) |
| 401 | else: |
| 402 | return node |
| 403 | |
| 404 | return _DeepFormat(local_scope) |
| 405 | |
| 406 | |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 407 | def EvaluateCondition(condition, variables, referenced_variables=None): |
| 408 | """Safely evaluates a boolean condition. Returns the result.""" |
| 409 | if not referenced_variables: |
| 410 | referenced_variables = set() |
| 411 | _allowed_names = {'None': None, 'True': True, 'False': False} |
| 412 | main_node = ast.parse(condition, mode='eval') |
| 413 | if isinstance(main_node, ast.Expression): |
| 414 | main_node = main_node.body |
| 415 | def _convert(node): |
| 416 | if isinstance(node, ast.Str): |
| 417 | return node.s |
| 418 | elif isinstance(node, ast.Name): |
| 419 | if node.id in referenced_variables: |
| 420 | raise ValueError( |
| 421 | 'invalid cyclic reference to %r (inside %r)' % ( |
| 422 | node.id, condition)) |
| 423 | elif node.id in _allowed_names: |
| 424 | return _allowed_names[node.id] |
| 425 | elif node.id in variables: |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 426 | value = variables[node.id] |
| 427 | |
| 428 | # Allow using "native" types, without wrapping everything in strings. |
| 429 | # Note that schema constraints still apply to variables. |
| 430 | if not isinstance(value, basestring): |
| 431 | return value |
| 432 | |
| 433 | # Recursively evaluate the variable reference. |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 434 | return EvaluateCondition( |
| 435 | variables[node.id], |
| 436 | variables, |
| 437 | referenced_variables.union([node.id])) |
| 438 | else: |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 439 | # Implicitly convert unrecognized names to strings. |
| 440 | # If we want to change this, we'll need to explicitly distinguish |
| 441 | # between arguments for GN to be passed verbatim, and ones to |
| 442 | # be evaluated. |
| 443 | return node.id |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 444 | elif isinstance(node, ast.BoolOp) and isinstance(node.op, ast.Or): |
| 445 | if len(node.values) != 2: |
| 446 | raise ValueError( |
| 447 | 'invalid "or": exactly 2 operands required (inside %r)' % ( |
| 448 | condition)) |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 449 | left = _convert(node.values[0]) |
| 450 | right = _convert(node.values[1]) |
| 451 | if not isinstance(left, bool): |
| 452 | raise ValueError( |
| 453 | 'invalid "or" operand %r (inside %r)' % (left, condition)) |
| 454 | if not isinstance(right, bool): |
| 455 | raise ValueError( |
| 456 | 'invalid "or" operand %r (inside %r)' % (right, condition)) |
| 457 | return left or right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 458 | elif isinstance(node, ast.BoolOp) and isinstance(node.op, ast.And): |
| 459 | if len(node.values) != 2: |
| 460 | raise ValueError( |
| 461 | 'invalid "and": exactly 2 operands required (inside %r)' % ( |
| 462 | condition)) |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 463 | left = _convert(node.values[0]) |
| 464 | right = _convert(node.values[1]) |
| 465 | if not isinstance(left, bool): |
| 466 | raise ValueError( |
| 467 | 'invalid "and" operand %r (inside %r)' % (left, condition)) |
| 468 | if not isinstance(right, bool): |
| 469 | raise ValueError( |
| 470 | 'invalid "and" operand %r (inside %r)' % (right, condition)) |
| 471 | return left and right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 472 | elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not): |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 473 | value = _convert(node.operand) |
| 474 | if not isinstance(value, bool): |
| 475 | raise ValueError( |
| 476 | 'invalid "not" operand %r (inside %r)' % (value, condition)) |
| 477 | return not value |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 478 | elif isinstance(node, ast.Compare): |
| 479 | if len(node.ops) != 1: |
| 480 | raise ValueError( |
| 481 | 'invalid compare: exactly 1 operator required (inside %r)' % ( |
| 482 | condition)) |
| 483 | if len(node.comparators) != 1: |
| 484 | raise ValueError( |
| 485 | 'invalid compare: exactly 1 comparator required (inside %r)' % ( |
| 486 | condition)) |
| 487 | |
| 488 | left = _convert(node.left) |
| 489 | right = _convert(node.comparators[0]) |
| 490 | |
| 491 | if isinstance(node.ops[0], ast.Eq): |
| 492 | return left == right |
Dirk Pranke | 77b7687 | 2017-10-05 18:29:27 -0700 | [diff] [blame] | 493 | if isinstance(node.ops[0], ast.NotEq): |
| 494 | return left != right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 495 | |
| 496 | raise ValueError( |
| 497 | 'unexpected operator: %s %s (inside %r)' % ( |
| 498 | node.ops[0], ast.dump(node), condition)) |
| 499 | else: |
| 500 | raise ValueError( |
| 501 | 'unexpected AST node: %s %s (inside %r)' % ( |
| 502 | node, ast.dump(node), condition)) |
| 503 | return _convert(main_node) |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 504 | |
| 505 | |
| 506 | def RenderDEPSFile(gclient_dict): |
| 507 | contents = sorted(gclient_dict.tokens.values(), key=lambda token: token[2]) |
| 508 | return tokenize.untokenize(contents) |
| 509 | |
| 510 | |
| 511 | def _UpdateAstString(tokens, node, value): |
| 512 | position = node.lineno, node.col_offset |
Edward Lesmes | 62af4e4 | 2018-03-30 18:15:44 -0400 | [diff] [blame] | 513 | quote_char = tokens[position][1][0] |
| 514 | tokens[position][1] = quote_char + value + quote_char |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 515 | node.s = value |
| 516 | |
| 517 | |
Edward Lesmes | 3d99381 | 2018-04-02 12:52:49 -0400 | [diff] [blame] | 518 | def _ShiftLinesInTokens(tokens, delta, start): |
| 519 | new_tokens = {} |
| 520 | for token in tokens.values(): |
| 521 | if token[2][0] >= start: |
| 522 | token[2] = token[2][0] + delta, token[2][1] |
| 523 | token[3] = token[3][0] + delta, token[3][1] |
| 524 | new_tokens[token[2]] = token |
| 525 | return new_tokens |
| 526 | |
| 527 | |
| 528 | def AddVar(gclient_dict, var_name, value): |
| 529 | if not isinstance(gclient_dict, _NodeDict) or gclient_dict.tokens is None: |
| 530 | raise ValueError( |
| 531 | "Can't use SetVar for the given gclient dict. It contains no " |
| 532 | "formatting information.") |
| 533 | |
| 534 | if 'vars' not in gclient_dict: |
| 535 | raise KeyError("vars dict is not defined.") |
| 536 | |
| 537 | if var_name in gclient_dict['vars']: |
| 538 | raise ValueError( |
| 539 | "%s has already been declared in the vars dict. Consider using SetVar " |
| 540 | "instead." % var_name) |
| 541 | |
| 542 | if not gclient_dict['vars']: |
| 543 | raise ValueError('vars dict is empty. This is not yet supported.') |
| 544 | |
| 545 | # We will attempt to add the var right before the first var. |
| 546 | node = gclient_dict.GetNode('vars').keys[0] |
| 547 | if node is None: |
| 548 | raise ValueError( |
| 549 | "The vars dict has no formatting information." % var_name) |
| 550 | line = node.lineno |
| 551 | col = node.col_offset |
| 552 | |
| 553 | # We use a minimal Python dictionary, so that ast can parse it. |
| 554 | var_content = '{\n%s"%s": "%s",\n}' % (' ' * col, var_name, value) |
| 555 | var_ast = ast.parse(var_content).body[0].value |
| 556 | |
| 557 | # Set the ast nodes for the key and value. |
| 558 | vars_node = gclient_dict.GetNode('vars') |
| 559 | |
| 560 | var_name_node = var_ast.keys[0] |
| 561 | var_name_node.lineno += line - 2 |
| 562 | vars_node.keys.insert(0, var_name_node) |
| 563 | |
| 564 | value_node = var_ast.values[0] |
| 565 | value_node.lineno += line - 2 |
| 566 | vars_node.values.insert(0, value_node) |
| 567 | |
| 568 | # Update the tokens. |
| 569 | var_tokens = list(tokenize.generate_tokens( |
| 570 | cStringIO.StringIO(var_content).readline)) |
| 571 | var_tokens = { |
| 572 | token[2]: list(token) |
| 573 | # Ignore the tokens corresponding to braces and new lines. |
| 574 | for token in var_tokens[2:-2] |
| 575 | } |
| 576 | |
| 577 | gclient_dict.tokens = _ShiftLinesInTokens(gclient_dict.tokens, 1, line) |
| 578 | gclient_dict.tokens.update(_ShiftLinesInTokens(var_tokens, line - 2, 0)) |
| 579 | |
| 580 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 581 | def SetVar(gclient_dict, var_name, value): |
| 582 | if not isinstance(gclient_dict, _NodeDict) or gclient_dict.tokens is None: |
| 583 | raise ValueError( |
| 584 | "Can't use SetVar for the given gclient dict. It contains no " |
| 585 | "formatting information.") |
| 586 | tokens = gclient_dict.tokens |
| 587 | |
Edward Lesmes | 3d99381 | 2018-04-02 12:52:49 -0400 | [diff] [blame] | 588 | if 'vars' not in gclient_dict: |
| 589 | raise KeyError("vars dict is not defined.") |
| 590 | |
| 591 | if var_name not in gclient_dict['vars']: |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 592 | raise ValueError( |
Edward Lesmes | 3d99381 | 2018-04-02 12:52:49 -0400 | [diff] [blame] | 593 | "%s has not been declared in the vars dict. Consider using AddVar " |
| 594 | "instead." % var_name) |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 595 | |
| 596 | node = gclient_dict['vars'].GetNode(var_name) |
| 597 | if node is None: |
| 598 | raise ValueError( |
| 599 | "The vars entry for %s has no formatting information." % var_name) |
| 600 | |
| 601 | _UpdateAstString(tokens, node, value) |
Edward Lesmes | 6c24d37 | 2018-03-28 12:52:29 -0400 | [diff] [blame] | 602 | gclient_dict['vars'].SetNode(var_name, value, node) |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 603 | |
| 604 | |
| 605 | def SetCIPD(gclient_dict, dep_name, package_name, new_version): |
| 606 | if not isinstance(gclient_dict, _NodeDict) or gclient_dict.tokens is None: |
| 607 | raise ValueError( |
| 608 | "Can't use SetCIPD for the given gclient dict. It contains no " |
| 609 | "formatting information.") |
| 610 | tokens = gclient_dict.tokens |
| 611 | |
| 612 | if 'deps' not in gclient_dict or dep_name not in gclient_dict['deps']: |
Edward Lesmes | 3d99381 | 2018-04-02 12:52:49 -0400 | [diff] [blame] | 613 | raise KeyError( |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 614 | "Could not find any dependency called %s." % dep_name) |
| 615 | |
| 616 | # Find the package with the given name |
| 617 | packages = [ |
| 618 | package |
| 619 | for package in gclient_dict['deps'][dep_name]['packages'] |
| 620 | if package['package'] == package_name |
| 621 | ] |
| 622 | if len(packages) != 1: |
| 623 | raise ValueError( |
| 624 | "There must be exactly one package with the given name (%s), " |
| 625 | "%s were found." % (package_name, len(packages))) |
| 626 | |
| 627 | # TODO(ehmaldonado): Support Var in package's version. |
| 628 | node = packages[0].GetNode('version') |
| 629 | if node is None: |
| 630 | raise ValueError( |
| 631 | "The deps entry for %s:%s has no formatting information." % |
| 632 | (dep_name, package_name)) |
| 633 | |
| 634 | new_version = 'version:' + new_version |
| 635 | _UpdateAstString(tokens, node, new_version) |
Edward Lesmes | 6c24d37 | 2018-03-28 12:52:29 -0400 | [diff] [blame] | 636 | packages[0].SetNode('version', new_version, node) |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 637 | |
| 638 | |
Edward Lesmes | 9f53129 | 2018-03-20 21:27:15 -0400 | [diff] [blame] | 639 | def SetRevision(gclient_dict, dep_name, new_revision): |
Edward Lesmes | 62af4e4 | 2018-03-30 18:15:44 -0400 | [diff] [blame] | 640 | def _GetVarName(node): |
| 641 | if isinstance(node, ast.Call): |
| 642 | return node.args[0].s |
| 643 | elif node.s.endswith('}'): |
| 644 | last_brace = node.s.rfind('{') |
| 645 | return node.s[last_brace+1:-1] |
| 646 | return None |
| 647 | |
| 648 | def _UpdateRevision(dep_dict, dep_key, new_revision): |
| 649 | dep_node = dep_dict.GetNode(dep_key) |
| 650 | if dep_node is None: |
| 651 | raise ValueError( |
| 652 | "The deps entry for %s has no formatting information." % dep_name) |
| 653 | |
| 654 | node = dep_node |
| 655 | if isinstance(node, ast.BinOp): |
| 656 | node = node.right |
| 657 | |
| 658 | if not isinstance(node, ast.Call) and not isinstance(node, ast.Str): |
| 659 | raise ValueError( |
| 660 | "Unsupported dependency revision format. Please file a bug.") |
| 661 | |
| 662 | var_name = _GetVarName(node) |
| 663 | if var_name is not None: |
| 664 | SetVar(gclient_dict, var_name, new_revision) |
| 665 | else: |
| 666 | if '@' in node.s: |
| 667 | new_revision = node.s.split('@')[0] + '@' + new_revision |
| 668 | _UpdateAstString(tokens, node, new_revision) |
| 669 | dep_dict.SetNode(dep_key, new_revision, node) |
| 670 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 671 | if not isinstance(gclient_dict, _NodeDict) or gclient_dict.tokens is None: |
| 672 | raise ValueError( |
| 673 | "Can't use SetRevision for the given gclient dict. It contains no " |
| 674 | "formatting information.") |
| 675 | tokens = gclient_dict.tokens |
| 676 | |
| 677 | if 'deps' not in gclient_dict or dep_name not in gclient_dict['deps']: |
Edward Lesmes | 3d99381 | 2018-04-02 12:52:49 -0400 | [diff] [blame] | 678 | raise KeyError( |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 679 | "Could not find any dependency called %s." % dep_name) |
| 680 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 681 | if isinstance(gclient_dict['deps'][dep_name], _NodeDict): |
Edward Lesmes | 62af4e4 | 2018-03-30 18:15:44 -0400 | [diff] [blame] | 682 | _UpdateRevision(gclient_dict['deps'][dep_name], 'url', new_revision) |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame] | 683 | else: |
Edward Lesmes | 62af4e4 | 2018-03-30 18:15:44 -0400 | [diff] [blame] | 684 | _UpdateRevision(gclient_dict['deps'], dep_name, new_revision) |