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 | |
| 37 | def GetNode(self, key): |
| 38 | return self.data[key][1] |
| 39 | |
| 40 | def _SetNode(self, key, value, node): |
| 41 | self.data[key] = (value, node) |
| 42 | |
| 43 | |
| 44 | def _NodeDictSchema(dict_schema): |
| 45 | """Validate dict_schema after converting _NodeDict to a regular dict.""" |
| 46 | def validate(d): |
| 47 | schema.Schema(dict_schema).validate(dict(d)) |
| 48 | return True |
| 49 | return validate |
| 50 | |
| 51 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 52 | # See https://github.com/keleshev/schema for docs how to configure schema. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 53 | _GCLIENT_DEPS_SCHEMA = _NodeDictSchema({ |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 54 | schema.Optional(basestring): schema.Or( |
| 55 | None, |
| 56 | basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 57 | _NodeDictSchema({ |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 58 | # Repo and revision to check out under the path |
| 59 | # (same as if no dict was used). |
| 60 | 'url': basestring, |
| 61 | |
| 62 | # Optional condition string. The dep will only be processed |
| 63 | # if the condition evaluates to True. |
| 64 | schema.Optional('condition'): basestring, |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 65 | |
| 66 | schema.Optional('dep_type', default='git'): basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 67 | }), |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 68 | # CIPD package. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 69 | _NodeDictSchema({ |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 70 | 'packages': [ |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 71 | _NodeDictSchema({ |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 72 | 'package': basestring, |
| 73 | |
| 74 | 'version': basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 75 | }) |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 76 | ], |
| 77 | |
| 78 | schema.Optional('condition'): basestring, |
| 79 | |
| 80 | schema.Optional('dep_type', default='cipd'): basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 81 | }), |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 82 | ), |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 83 | }) |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 84 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 85 | _GCLIENT_HOOKS_SCHEMA = [_NodeDictSchema({ |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 86 | # Hook action: list of command-line arguments to invoke. |
| 87 | 'action': [basestring], |
| 88 | |
| 89 | # Name of the hook. Doesn't affect operation. |
| 90 | schema.Optional('name'): basestring, |
| 91 | |
| 92 | # Hook pattern (regex). Originally intended to limit some hooks to run |
| 93 | # only when files matching the pattern have changed. In practice, with git, |
| 94 | # gclient runs all the hooks regardless of this field. |
| 95 | schema.Optional('pattern'): basestring, |
Paweł Hajdan, Jr | c936439 | 2017-06-14 17:11:56 +0200 | [diff] [blame] | 96 | |
| 97 | # Working directory where to execute the hook. |
| 98 | schema.Optional('cwd'): basestring, |
Paweł Hajdan, Jr | 032d545 | 2017-06-22 20:43:53 +0200 | [diff] [blame] | 99 | |
| 100 | # Optional condition string. The hook will only be run |
| 101 | # if the condition evaluates to True. |
| 102 | schema.Optional('condition'): basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 103 | })] |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 104 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 105 | _GCLIENT_SCHEMA = schema.Schema(_NodeDictSchema({ |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 106 | # List of host names from which dependencies are allowed (whitelist). |
| 107 | # NOTE: when not present, all hosts are allowed. |
| 108 | # NOTE: scoped to current DEPS file, not recursive. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 109 | schema.Optional('allowed_hosts'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 110 | |
| 111 | # Mapping from paths to repo and revision to check out under that path. |
| 112 | # Applying this mapping to the on-disk checkout is the main purpose |
| 113 | # of gclient, and also why the config file is called DEPS. |
| 114 | # |
| 115 | # The following functions are allowed: |
| 116 | # |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 117 | # Var(): allows variable substitution (either from 'vars' dict below, |
| 118 | # or command-line override) |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 119 | schema.Optional('deps'): _GCLIENT_DEPS_SCHEMA, |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 120 | |
| 121 | # 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] | 122 | # Also see 'target_os'. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 123 | schema.Optional('deps_os'): _NodeDictSchema({ |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 18:51:58 +0200 | [diff] [blame] | 124 | schema.Optional(basestring): _GCLIENT_DEPS_SCHEMA, |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 125 | }), |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 126 | |
Paweł Hajdan, Jr | 5725373 | 2017-06-06 23:49:11 +0200 | [diff] [blame] | 127 | # Path to GN args file to write selected variables. |
| 128 | schema.Optional('gclient_gn_args_file'): basestring, |
| 129 | |
| 130 | # Subset of variables to write to the GN args file (see above). |
| 131 | schema.Optional('gclient_gn_args'): [schema.Optional(basestring)], |
| 132 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 133 | # Hooks executed after gclient sync (unless suppressed), or explicitly |
| 134 | # on gclient hooks. See _GCLIENT_HOOKS_SCHEMA for details. |
| 135 | # Also see 'pre_deps_hooks'. |
| 136 | schema.Optional('hooks'): _GCLIENT_HOOKS_SCHEMA, |
| 137 | |
Scott Graham | c482674 | 2017-05-11 16:59:23 -0700 | [diff] [blame] | 138 | # Similar to 'hooks', also keyed by OS. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 139 | schema.Optional('hooks_os'): _NodeDictSchema({ |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 140 | schema.Optional(basestring): _GCLIENT_HOOKS_SCHEMA |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 141 | }), |
Scott Graham | c482674 | 2017-05-11 16:59:23 -0700 | [diff] [blame] | 142 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 143 | # Rules which #includes are allowed in the directory. |
| 144 | # Also see 'skip_child_includes' and 'specific_include_rules'. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 145 | schema.Optional('include_rules'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 146 | |
| 147 | # Hooks executed before processing DEPS. See 'hooks' for more details. |
| 148 | schema.Optional('pre_deps_hooks'): _GCLIENT_HOOKS_SCHEMA, |
| 149 | |
Paweł Hajdan, Jr | 6f79679 | 2017-06-02 08:40:06 +0200 | [diff] [blame] | 150 | # Recursion limit for nested DEPS. |
| 151 | schema.Optional('recursion'): int, |
| 152 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 153 | # Whitelists deps for which recursion should be enabled. |
| 154 | schema.Optional('recursedeps'): [ |
Paweł Hajdan, Jr | 05fec03 | 2017-05-30 23:04:23 +0200 | [diff] [blame] | 155 | schema.Optional(schema.Or( |
| 156 | basestring, |
| 157 | (basestring, basestring), |
| 158 | [basestring, basestring] |
| 159 | )), |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 160 | ], |
| 161 | |
| 162 | # Blacklists directories for checking 'include_rules'. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 163 | schema.Optional('skip_child_includes'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 164 | |
| 165 | # Mapping from paths to include rules specific for that path. |
| 166 | # See 'include_rules' for more details. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 167 | schema.Optional('specific_include_rules'): _NodeDictSchema({ |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 168 | schema.Optional(basestring): [basestring] |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 169 | }), |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 170 | |
| 171 | # List of additional OS names to consider when selecting dependencies |
| 172 | # from deps_os. |
| 173 | schema.Optional('target_os'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 174 | |
| 175 | # For recursed-upon sub-dependencies, check out their own dependencies |
| 176 | # relative to the paren't path, rather than relative to the .gclient file. |
| 177 | schema.Optional('use_relative_paths'): bool, |
| 178 | |
| 179 | # Variables that can be referenced using Var() - see 'deps'. |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 180 | schema.Optional('vars'): _NodeDictSchema({ |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 181 | schema.Optional(basestring): schema.Or(basestring, bool), |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 182 | }), |
| 183 | })) |
Paweł Hajdan, Jr | beec006 | 2017-05-10 21:51:05 +0200 | [diff] [blame] | 184 | |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 185 | |
| 186 | def _gclient_eval(node_or_string, global_scope, filename='<unknown>'): |
| 187 | """Safely evaluates a single expression. Returns the result.""" |
| 188 | _allowed_names = {'None': None, 'True': True, 'False': False} |
| 189 | if isinstance(node_or_string, basestring): |
| 190 | node_or_string = ast.parse(node_or_string, filename=filename, mode='eval') |
| 191 | if isinstance(node_or_string, ast.Expression): |
| 192 | node_or_string = node_or_string.body |
| 193 | def _convert(node): |
| 194 | if isinstance(node, ast.Str): |
| 195 | return node.s |
Paweł Hajdan, Jr | 6f79679 | 2017-06-02 08:40:06 +0200 | [diff] [blame] | 196 | elif isinstance(node, ast.Num): |
| 197 | return node.n |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 198 | elif isinstance(node, ast.Tuple): |
| 199 | return tuple(map(_convert, node.elts)) |
| 200 | elif isinstance(node, ast.List): |
| 201 | return list(map(_convert, node.elts)) |
| 202 | elif isinstance(node, ast.Dict): |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 203 | return _NodeDict((_convert(k), (_convert(v), v)) |
Paweł Hajdan, Jr | 7cf96a4 | 2017-05-26 20:28:35 +0200 | [diff] [blame] | 204 | for k, v in zip(node.keys, node.values)) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 205 | elif isinstance(node, ast.Name): |
| 206 | if node.id not in _allowed_names: |
| 207 | raise ValueError( |
| 208 | 'invalid name %r (file %r, line %s)' % ( |
| 209 | node.id, filename, getattr(node, 'lineno', '<unknown>'))) |
| 210 | return _allowed_names[node.id] |
| 211 | elif isinstance(node, ast.Call): |
| 212 | if not isinstance(node.func, ast.Name): |
| 213 | raise ValueError( |
| 214 | 'invalid call: func should be a name (file %r, line %s)' % ( |
| 215 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 216 | if node.keywords or node.starargs or node.kwargs: |
| 217 | raise ValueError( |
| 218 | 'invalid call: use only regular args (file %r, line %s)' % ( |
| 219 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 220 | args = map(_convert, node.args) |
| 221 | return global_scope[node.func.id](*args) |
| 222 | elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add): |
| 223 | return _convert(node.left) + _convert(node.right) |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 16:57:37 +0200 | [diff] [blame] | 224 | elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Mod): |
| 225 | return _convert(node.left) % _convert(node.right) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 226 | else: |
| 227 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 228 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
| 229 | node, ast.dump(node), filename, |
| 230 | getattr(node, 'lineno', '<unknown>'))) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 231 | return _convert(node_or_string) |
| 232 | |
| 233 | |
Paweł Hajdan, Jr | c485d5a | 2017-06-02 12:08:09 +0200 | [diff] [blame] | 234 | def Exec(content, global_scope, local_scope, filename='<unknown>'): |
| 235 | """Safely execs a set of assignments. Mutates |local_scope|.""" |
| 236 | node_or_string = ast.parse(content, filename=filename, mode='exec') |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 237 | if isinstance(node_or_string, ast.Expression): |
| 238 | node_or_string = node_or_string.body |
| 239 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 240 | defined_variables = set() |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 241 | def _visit_in_module(node): |
| 242 | if isinstance(node, ast.Assign): |
| 243 | if len(node.targets) != 1: |
| 244 | raise ValueError( |
| 245 | 'invalid assignment: use exactly one target (file %r, line %s)' % ( |
| 246 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 247 | target = node.targets[0] |
| 248 | if not isinstance(target, ast.Name): |
| 249 | raise ValueError( |
| 250 | 'invalid assignment: target should be a name (file %r, line %s)' % ( |
| 251 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 252 | value = _gclient_eval(node.value, global_scope, filename=filename) |
| 253 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 254 | if target.id in defined_variables: |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 255 | raise ValueError( |
| 256 | 'invalid assignment: overrides var %r (file %r, line %s)' % ( |
| 257 | target.id, filename, getattr(node, 'lineno', '<unknown>'))) |
| 258 | |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 259 | defined_variables.add(target.id) |
| 260 | return target.id, (value, node.value) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 261 | else: |
| 262 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 263 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
| 264 | node, ast.dump(node), filename, |
| 265 | getattr(node, 'lineno', '<unknown>'))) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 266 | |
| 267 | if isinstance(node_or_string, ast.Module): |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 268 | data = [] |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 269 | for stmt in node_or_string.body: |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 270 | data.append(_visit_in_module(stmt)) |
| 271 | tokens = { |
| 272 | token[2]: list(token) |
| 273 | for token in tokenize.generate_tokens( |
| 274 | cStringIO.StringIO(content).readline) |
| 275 | } |
| 276 | local_scope = _NodeDict(data, tokens) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 277 | else: |
| 278 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 279 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 280 | node_or_string, |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 20:14:44 +0200 | [diff] [blame] | 281 | ast.dump(node_or_string), |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 10:04:02 +0200 | [diff] [blame] | 282 | filename, |
| 283 | getattr(node_or_string, 'lineno', '<unknown>'))) |
| 284 | |
John Budorick | 0f7b200 | 2018-01-19 15:46:17 -0800 | [diff] [blame] | 285 | return _GCLIENT_SCHEMA.validate(local_scope) |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 286 | |
| 287 | |
| 288 | def EvaluateCondition(condition, variables, referenced_variables=None): |
| 289 | """Safely evaluates a boolean condition. Returns the result.""" |
| 290 | if not referenced_variables: |
| 291 | referenced_variables = set() |
| 292 | _allowed_names = {'None': None, 'True': True, 'False': False} |
| 293 | main_node = ast.parse(condition, mode='eval') |
| 294 | if isinstance(main_node, ast.Expression): |
| 295 | main_node = main_node.body |
| 296 | def _convert(node): |
| 297 | if isinstance(node, ast.Str): |
| 298 | return node.s |
| 299 | elif isinstance(node, ast.Name): |
| 300 | if node.id in referenced_variables: |
| 301 | raise ValueError( |
| 302 | 'invalid cyclic reference to %r (inside %r)' % ( |
| 303 | node.id, condition)) |
| 304 | elif node.id in _allowed_names: |
| 305 | return _allowed_names[node.id] |
| 306 | elif node.id in variables: |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 307 | value = variables[node.id] |
| 308 | |
| 309 | # Allow using "native" types, without wrapping everything in strings. |
| 310 | # Note that schema constraints still apply to variables. |
| 311 | if not isinstance(value, basestring): |
| 312 | return value |
| 313 | |
| 314 | # Recursively evaluate the variable reference. |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 315 | return EvaluateCondition( |
| 316 | variables[node.id], |
| 317 | variables, |
| 318 | referenced_variables.union([node.id])) |
| 319 | else: |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 320 | # Implicitly convert unrecognized names to strings. |
| 321 | # If we want to change this, we'll need to explicitly distinguish |
| 322 | # between arguments for GN to be passed verbatim, and ones to |
| 323 | # be evaluated. |
| 324 | return node.id |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 325 | elif isinstance(node, ast.BoolOp) and isinstance(node.op, ast.Or): |
| 326 | if len(node.values) != 2: |
| 327 | raise ValueError( |
| 328 | 'invalid "or": exactly 2 operands required (inside %r)' % ( |
| 329 | condition)) |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 330 | left = _convert(node.values[0]) |
| 331 | right = _convert(node.values[1]) |
| 332 | if not isinstance(left, bool): |
| 333 | raise ValueError( |
| 334 | 'invalid "or" operand %r (inside %r)' % (left, condition)) |
| 335 | if not isinstance(right, bool): |
| 336 | raise ValueError( |
| 337 | 'invalid "or" operand %r (inside %r)' % (right, condition)) |
| 338 | return left or right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 339 | elif isinstance(node, ast.BoolOp) and isinstance(node.op, ast.And): |
| 340 | if len(node.values) != 2: |
| 341 | raise ValueError( |
| 342 | 'invalid "and": exactly 2 operands required (inside %r)' % ( |
| 343 | condition)) |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 344 | left = _convert(node.values[0]) |
| 345 | right = _convert(node.values[1]) |
| 346 | if not isinstance(left, bool): |
| 347 | raise ValueError( |
| 348 | 'invalid "and" operand %r (inside %r)' % (left, condition)) |
| 349 | if not isinstance(right, bool): |
| 350 | raise ValueError( |
| 351 | 'invalid "and" operand %r (inside %r)' % (right, condition)) |
| 352 | return left and right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 353 | elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not): |
Paweł Hajdan, Jr | e021474 | 2017-09-28 12:21:01 +0200 | [diff] [blame] | 354 | value = _convert(node.operand) |
| 355 | if not isinstance(value, bool): |
| 356 | raise ValueError( |
| 357 | 'invalid "not" operand %r (inside %r)' % (value, condition)) |
| 358 | return not value |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 359 | elif isinstance(node, ast.Compare): |
| 360 | if len(node.ops) != 1: |
| 361 | raise ValueError( |
| 362 | 'invalid compare: exactly 1 operator required (inside %r)' % ( |
| 363 | condition)) |
| 364 | if len(node.comparators) != 1: |
| 365 | raise ValueError( |
| 366 | 'invalid compare: exactly 1 comparator required (inside %r)' % ( |
| 367 | condition)) |
| 368 | |
| 369 | left = _convert(node.left) |
| 370 | right = _convert(node.comparators[0]) |
| 371 | |
| 372 | if isinstance(node.ops[0], ast.Eq): |
| 373 | return left == right |
Dirk Pranke | 77b7687 | 2017-10-05 18:29:27 -0700 | [diff] [blame] | 374 | if isinstance(node.ops[0], ast.NotEq): |
| 375 | return left != right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 21:46:57 +0200 | [diff] [blame] | 376 | |
| 377 | raise ValueError( |
| 378 | 'unexpected operator: %s %s (inside %r)' % ( |
| 379 | node.ops[0], ast.dump(node), condition)) |
| 380 | else: |
| 381 | raise ValueError( |
| 382 | 'unexpected AST node: %s %s (inside %r)' % ( |
| 383 | node, ast.dump(node), condition)) |
| 384 | return _convert(main_node) |
Edward Lesmes | 6f64a05 | 2018-03-20 17:35:49 -0400 | [diff] [blame^] | 385 | |
| 386 | |
| 387 | def RenderDEPSFile(gclient_dict): |
| 388 | contents = sorted(gclient_dict.tokens.values(), key=lambda token: token[2]) |
| 389 | return tokenize.untokenize(contents) |
| 390 | |
| 391 | |
| 392 | def _UpdateAstString(tokens, node, value): |
| 393 | position = node.lineno, node.col_offset |
| 394 | tokens[position][1] = repr(value) |
| 395 | node.s = value |
| 396 | |
| 397 | |
| 398 | def SetVar(gclient_dict, var_name, value): |
| 399 | if not isinstance(gclient_dict, _NodeDict) or gclient_dict.tokens is None: |
| 400 | raise ValueError( |
| 401 | "Can't use SetVar for the given gclient dict. It contains no " |
| 402 | "formatting information.") |
| 403 | tokens = gclient_dict.tokens |
| 404 | |
| 405 | if 'vars' not in gclient_dict or var_name not in gclient_dict['vars']: |
| 406 | raise ValueError( |
| 407 | "Could not find any variable called %s." % var_name) |
| 408 | |
| 409 | node = gclient_dict['vars'].GetNode(var_name) |
| 410 | if node is None: |
| 411 | raise ValueError( |
| 412 | "The vars entry for %s has no formatting information." % var_name) |
| 413 | |
| 414 | _UpdateAstString(tokens, node, value) |
| 415 | gclient_dict['vars']._SetNode(var_name, value, node) |
| 416 | |
| 417 | |
| 418 | def SetCIPD(gclient_dict, dep_name, package_name, new_version): |
| 419 | if not isinstance(gclient_dict, _NodeDict) or gclient_dict.tokens is None: |
| 420 | raise ValueError( |
| 421 | "Can't use SetCIPD for the given gclient dict. It contains no " |
| 422 | "formatting information.") |
| 423 | tokens = gclient_dict.tokens |
| 424 | |
| 425 | if 'deps' not in gclient_dict or dep_name not in gclient_dict['deps']: |
| 426 | raise ValueError( |
| 427 | "Could not find any dependency called %s." % dep_name) |
| 428 | |
| 429 | # Find the package with the given name |
| 430 | packages = [ |
| 431 | package |
| 432 | for package in gclient_dict['deps'][dep_name]['packages'] |
| 433 | if package['package'] == package_name |
| 434 | ] |
| 435 | if len(packages) != 1: |
| 436 | raise ValueError( |
| 437 | "There must be exactly one package with the given name (%s), " |
| 438 | "%s were found." % (package_name, len(packages))) |
| 439 | |
| 440 | # TODO(ehmaldonado): Support Var in package's version. |
| 441 | node = packages[0].GetNode('version') |
| 442 | if node is None: |
| 443 | raise ValueError( |
| 444 | "The deps entry for %s:%s has no formatting information." % |
| 445 | (dep_name, package_name)) |
| 446 | |
| 447 | new_version = 'version:' + new_version |
| 448 | _UpdateAstString(tokens, node, new_version) |
| 449 | packages[0]._SetNode('version', new_version, node) |
| 450 | |
| 451 | |
| 452 | def SetRevision(gclient_dict, global_scope, dep_name, new_revision): |
| 453 | if not isinstance(gclient_dict, _NodeDict) or gclient_dict.tokens is None: |
| 454 | raise ValueError( |
| 455 | "Can't use SetRevision for the given gclient dict. It contains no " |
| 456 | "formatting information.") |
| 457 | tokens = gclient_dict.tokens |
| 458 | |
| 459 | if 'deps' not in gclient_dict or dep_name not in gclient_dict['deps']: |
| 460 | raise ValueError( |
| 461 | "Could not find any dependency called %s." % dep_name) |
| 462 | |
| 463 | def _UpdateRevision(dep_dict, dep_key): |
| 464 | dep_node = dep_dict.GetNode(dep_key) |
| 465 | if dep_node is None: |
| 466 | raise ValueError( |
| 467 | "The deps entry for %s has no formatting information." % dep_name) |
| 468 | |
| 469 | node = dep_node |
| 470 | if isinstance(node, ast.BinOp): |
| 471 | node = node.right |
| 472 | if isinstance(node, ast.Call): |
| 473 | SetVar(gclient_dict, node.args[0].s, new_revision) |
| 474 | else: |
| 475 | _UpdateAstString(tokens, node, new_revision) |
| 476 | value = _gclient_eval(dep_node, global_scope) |
| 477 | dep_dict._SetNode(dep_key, value, dep_node) |
| 478 | |
| 479 | if isinstance(gclient_dict['deps'][dep_name], _NodeDict): |
| 480 | _UpdateRevision(gclient_dict['deps'][dep_name], 'url') |
| 481 | else: |
| 482 | _UpdateRevision(gclient_dict['deps'], dep_name) |