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