Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [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 21:35:49 | [diff] [blame] | 6 | import cStringIO |
Paweł Hajdan, Jr | 7cf96a4 | 2017-05-26 18:28:35 | [diff] [blame] | 7 | import collections |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 8 | import tokenize |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 9 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 10 | from third_party import schema |
| 11 | |
| 12 | |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [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 19:51:05 | [diff] [blame] | 52 | # See https://github.com/keleshev/schema for docs how to configure schema. |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 53 | _GCLIENT_DEPS_SCHEMA = _NodeDictSchema({ |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 16:51:58 | [diff] [blame] | 54 | schema.Optional(basestring): schema.Or( |
| 55 | None, |
| 56 | basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 57 | _NodeDictSchema({ |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 16:51:58 | [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 23:46:17 | [diff] [blame] | 65 | |
| 66 | schema.Optional('dep_type', default='git'): basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 67 | }), |
John Budorick | 0f7b200 | 2018-01-19 23:46:17 | [diff] [blame] | 68 | # CIPD package. |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 69 | _NodeDictSchema({ |
John Budorick | 0f7b200 | 2018-01-19 23:46:17 | [diff] [blame] | 70 | 'packages': [ |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 71 | _NodeDictSchema({ |
John Budorick | 0f7b200 | 2018-01-19 23:46:17 | [diff] [blame] | 72 | 'package': basestring, |
| 73 | |
| 74 | 'version': basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 75 | }) |
John Budorick | 0f7b200 | 2018-01-19 23:46:17 | [diff] [blame] | 76 | ], |
| 77 | |
| 78 | schema.Optional('condition'): basestring, |
| 79 | |
| 80 | schema.Optional('dep_type', default='cipd'): basestring, |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 81 | }), |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 16:51:58 | [diff] [blame] | 82 | ), |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 83 | }) |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 16:51:58 | [diff] [blame] | 84 | |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 85 | _GCLIENT_HOOKS_SCHEMA = [_NodeDictSchema({ |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [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 15:11:56 | [diff] [blame] | 96 | |
| 97 | # Working directory where to execute the hook. |
| 98 | schema.Optional('cwd'): basestring, |
Paweł Hajdan, Jr | 032d545 | 2017-06-22 18:43:53 | [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 21:35:49 | [diff] [blame] | 103 | })] |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 104 | |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 105 | _GCLIENT_SCHEMA = schema.Schema(_NodeDictSchema({ |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [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 14:57:37 | [diff] [blame] | 109 | schema.Optional('allowed_hosts'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [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 19:51:05 | [diff] [blame] | 117 | # Var(): allows variable substitution (either from 'vars' dict below, |
| 118 | # or command-line override) |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 16:51:58 | [diff] [blame] | 119 | schema.Optional('deps'): _GCLIENT_DEPS_SCHEMA, |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 120 | |
| 121 | # Similar to 'deps' (see above) - also keyed by OS (e.g. 'linux'). |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 122 | # Also see 'target_os'. |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 123 | schema.Optional('deps_os'): _NodeDictSchema({ |
Paweł Hajdan, Jr | ad30de6 | 2017-06-26 16:51:58 | [diff] [blame] | 124 | schema.Optional(basestring): _GCLIENT_DEPS_SCHEMA, |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 125 | }), |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 126 | |
Paweł Hajdan, Jr | 5725373 | 2017-06-06 21:49:11 | [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 19:51:05 | [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 23:59:23 | [diff] [blame] | 138 | # Similar to 'hooks', also keyed by OS. |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 139 | schema.Optional('hooks_os'): _NodeDictSchema({ |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 140 | schema.Optional(basestring): _GCLIENT_HOOKS_SCHEMA |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 141 | }), |
Scott Graham | c482674 | 2017-05-11 23:59:23 | [diff] [blame] | 142 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [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 14:57:37 | [diff] [blame] | 145 | schema.Optional('include_rules'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [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 06:40:06 | [diff] [blame] | 150 | # Recursion limit for nested DEPS. |
| 151 | schema.Optional('recursion'): int, |
| 152 | |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 153 | # Whitelists deps for which recursion should be enabled. |
| 154 | schema.Optional('recursedeps'): [ |
Paweł Hajdan, Jr | 05fec03 | 2017-05-30 21:04:23 | [diff] [blame] | 155 | schema.Optional(schema.Or( |
| 156 | basestring, |
| 157 | (basestring, basestring), |
| 158 | [basestring, basestring] |
| 159 | )), |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 160 | ], |
| 161 | |
| 162 | # Blacklists directories for checking 'include_rules'. |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 163 | schema.Optional('skip_child_includes'): [schema.Optional(basestring)], |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [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 21:35:49 | [diff] [blame] | 167 | schema.Optional('specific_include_rules'): _NodeDictSchema({ |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 168 | schema.Optional(basestring): [basestring] |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 169 | }), |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [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 19:51:05 | [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 21:35:49 | [diff] [blame] | 180 | schema.Optional('vars'): _NodeDictSchema({ |
Paweł Hajdan, Jr | e021474 | 2017-09-28 10:21:01 | [diff] [blame] | 181 | schema.Optional(basestring): schema.Or(basestring, bool), |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 182 | }), |
| 183 | })) |
Paweł Hajdan, Jr | beec006 | 2017-05-10 19:51:05 | [diff] [blame] | 184 | |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 185 | |
Edward Lesmes | 9f53129 | 2018-03-21 01:27:15 | [diff] [blame^] | 186 | def _gclient_eval(node_or_string, filename='<unknown>'): |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 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 06:40:06 | [diff] [blame] | 196 | elif isinstance(node, ast.Num): |
| 197 | return node.n |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [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 21:35:49 | [diff] [blame] | 203 | return _NodeDict((_convert(k), (_convert(v), v)) |
Paweł Hajdan, Jr | 7cf96a4 | 2017-05-26 18:28:35 | [diff] [blame] | 204 | for k, v in zip(node.keys, node.values)) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [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): |
Edward Lesmes | 9f53129 | 2018-03-21 01:27:15 | [diff] [blame^] | 212 | if not isinstance(node.func, ast.Name) or node.func.id != 'Var': |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 213 | raise ValueError( |
Edward Lesmes | 9f53129 | 2018-03-21 01:27:15 | [diff] [blame^] | 214 | 'Var is the only allowed function (file %r, line %s)' % ( |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 215 | filename, getattr(node, 'lineno', '<unknown>'))) |
Edward Lesmes | 9f53129 | 2018-03-21 01:27:15 | [diff] [blame^] | 216 | if node.keywords or node.starargs or node.kwargs or len(node.args) != 1: |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 217 | raise ValueError( |
Edward Lesmes | 9f53129 | 2018-03-21 01:27:15 | [diff] [blame^] | 218 | 'Var takes exactly one argument (file %r, line %s)' % ( |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 219 | filename, getattr(node, 'lineno', '<unknown>'))) |
Edward Lesmes | 9f53129 | 2018-03-21 01:27:15 | [diff] [blame^] | 220 | arg = _convert(node.args[0]) |
| 221 | if not isinstance(arg, basestring): |
| 222 | raise ValueError( |
| 223 | 'Var\'s argument must be a variable name (file %r, line %s)' % ( |
| 224 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 225 | return '{%s}' % arg |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 226 | elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add): |
| 227 | return _convert(node.left) + _convert(node.right) |
Paweł Hajdan, Jr | b7e5333 | 2017-05-23 14:57:37 | [diff] [blame] | 228 | elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Mod): |
| 229 | return _convert(node.left) % _convert(node.right) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 230 | else: |
| 231 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 18:14:44 | [diff] [blame] | 232 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
| 233 | node, ast.dump(node), filename, |
| 234 | getattr(node, 'lineno', '<unknown>'))) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 235 | return _convert(node_or_string) |
| 236 | |
| 237 | |
Edward Lesmes | 9f53129 | 2018-03-21 01:27:15 | [diff] [blame^] | 238 | def Exec(content, filename='<unknown>'): |
Paweł Hajdan, Jr | c485d5a | 2017-06-02 10:08:09 | [diff] [blame] | 239 | """Safely execs a set of assignments. Mutates |local_scope|.""" |
| 240 | node_or_string = ast.parse(content, filename=filename, mode='exec') |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 241 | if isinstance(node_or_string, ast.Expression): |
| 242 | node_or_string = node_or_string.body |
| 243 | |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 244 | defined_variables = set() |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 245 | def _visit_in_module(node): |
| 246 | if isinstance(node, ast.Assign): |
| 247 | if len(node.targets) != 1: |
| 248 | raise ValueError( |
| 249 | 'invalid assignment: use exactly one target (file %r, line %s)' % ( |
| 250 | filename, getattr(node, 'lineno', '<unknown>'))) |
| 251 | target = node.targets[0] |
| 252 | if not isinstance(target, ast.Name): |
| 253 | raise ValueError( |
| 254 | 'invalid assignment: target should be a name (file %r, line %s)' % ( |
| 255 | filename, getattr(node, 'lineno', '<unknown>'))) |
Edward Lesmes | 9f53129 | 2018-03-21 01:27:15 | [diff] [blame^] | 256 | value = _gclient_eval(node.value, filename=filename) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 257 | |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 258 | if target.id in defined_variables: |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 259 | raise ValueError( |
| 260 | 'invalid assignment: overrides var %r (file %r, line %s)' % ( |
| 261 | target.id, filename, getattr(node, 'lineno', '<unknown>'))) |
| 262 | |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 263 | defined_variables.add(target.id) |
| 264 | return target.id, (value, node.value) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 265 | else: |
| 266 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 18:14:44 | [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 08:04:02 | [diff] [blame] | 270 | |
| 271 | if isinstance(node_or_string, ast.Module): |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 272 | data = [] |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 273 | for stmt in node_or_string.body: |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 274 | data.append(_visit_in_module(stmt)) |
| 275 | tokens = { |
| 276 | token[2]: list(token) |
| 277 | for token in tokenize.generate_tokens( |
| 278 | cStringIO.StringIO(content).readline) |
| 279 | } |
| 280 | local_scope = _NodeDict(data, tokens) |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 281 | else: |
| 282 | raise ValueError( |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 18:14:44 | [diff] [blame] | 283 | 'unexpected AST node: %s %s (file %r, line %s)' % ( |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 284 | node_or_string, |
Paweł Hajdan, Jr | 1ba610b | 2017-05-24 18:14:44 | [diff] [blame] | 285 | ast.dump(node_or_string), |
Paweł Hajdan, Jr | e2f9feec | 2017-05-09 08:04:02 | [diff] [blame] | 286 | filename, |
| 287 | getattr(node_or_string, 'lineno', '<unknown>'))) |
| 288 | |
John Budorick | 0f7b200 | 2018-01-19 23:46:17 | [diff] [blame] | 289 | return _GCLIENT_SCHEMA.validate(local_scope) |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 19:46:57 | [diff] [blame] | 290 | |
| 291 | |
| 292 | def EvaluateCondition(condition, variables, referenced_variables=None): |
| 293 | """Safely evaluates a boolean condition. Returns the result.""" |
| 294 | if not referenced_variables: |
| 295 | referenced_variables = set() |
| 296 | _allowed_names = {'None': None, 'True': True, 'False': False} |
| 297 | main_node = ast.parse(condition, mode='eval') |
| 298 | if isinstance(main_node, ast.Expression): |
| 299 | main_node = main_node.body |
| 300 | def _convert(node): |
| 301 | if isinstance(node, ast.Str): |
| 302 | return node.s |
| 303 | elif isinstance(node, ast.Name): |
| 304 | if node.id in referenced_variables: |
| 305 | raise ValueError( |
| 306 | 'invalid cyclic reference to %r (inside %r)' % ( |
| 307 | node.id, condition)) |
| 308 | elif node.id in _allowed_names: |
| 309 | return _allowed_names[node.id] |
| 310 | elif node.id in variables: |
Paweł Hajdan, Jr | e021474 | 2017-09-28 10:21:01 | [diff] [blame] | 311 | value = variables[node.id] |
| 312 | |
| 313 | # Allow using "native" types, without wrapping everything in strings. |
| 314 | # Note that schema constraints still apply to variables. |
| 315 | if not isinstance(value, basestring): |
| 316 | return value |
| 317 | |
| 318 | # Recursively evaluate the variable reference. |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 19:46:57 | [diff] [blame] | 319 | return EvaluateCondition( |
| 320 | variables[node.id], |
| 321 | variables, |
| 322 | referenced_variables.union([node.id])) |
| 323 | else: |
Paweł Hajdan, Jr | e021474 | 2017-09-28 10:21:01 | [diff] [blame] | 324 | # Implicitly convert unrecognized names to strings. |
| 325 | # If we want to change this, we'll need to explicitly distinguish |
| 326 | # between arguments for GN to be passed verbatim, and ones to |
| 327 | # be evaluated. |
| 328 | return node.id |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 19:46:57 | [diff] [blame] | 329 | elif isinstance(node, ast.BoolOp) and isinstance(node.op, ast.Or): |
| 330 | if len(node.values) != 2: |
| 331 | raise ValueError( |
| 332 | 'invalid "or": exactly 2 operands required (inside %r)' % ( |
| 333 | condition)) |
Paweł Hajdan, Jr | e021474 | 2017-09-28 10:21:01 | [diff] [blame] | 334 | left = _convert(node.values[0]) |
| 335 | right = _convert(node.values[1]) |
| 336 | if not isinstance(left, bool): |
| 337 | raise ValueError( |
| 338 | 'invalid "or" operand %r (inside %r)' % (left, condition)) |
| 339 | if not isinstance(right, bool): |
| 340 | raise ValueError( |
| 341 | 'invalid "or" operand %r (inside %r)' % (right, condition)) |
| 342 | return left or right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 19:46:57 | [diff] [blame] | 343 | elif isinstance(node, ast.BoolOp) and isinstance(node.op, ast.And): |
| 344 | if len(node.values) != 2: |
| 345 | raise ValueError( |
| 346 | 'invalid "and": exactly 2 operands required (inside %r)' % ( |
| 347 | condition)) |
Paweł Hajdan, Jr | e021474 | 2017-09-28 10:21:01 | [diff] [blame] | 348 | left = _convert(node.values[0]) |
| 349 | right = _convert(node.values[1]) |
| 350 | if not isinstance(left, bool): |
| 351 | raise ValueError( |
| 352 | 'invalid "and" operand %r (inside %r)' % (left, condition)) |
| 353 | if not isinstance(right, bool): |
| 354 | raise ValueError( |
| 355 | 'invalid "and" operand %r (inside %r)' % (right, condition)) |
| 356 | return left and right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 19:46:57 | [diff] [blame] | 357 | elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not): |
Paweł Hajdan, Jr | e021474 | 2017-09-28 10:21:01 | [diff] [blame] | 358 | value = _convert(node.operand) |
| 359 | if not isinstance(value, bool): |
| 360 | raise ValueError( |
| 361 | 'invalid "not" operand %r (inside %r)' % (value, condition)) |
| 362 | return not value |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 19:46:57 | [diff] [blame] | 363 | elif isinstance(node, ast.Compare): |
| 364 | if len(node.ops) != 1: |
| 365 | raise ValueError( |
| 366 | 'invalid compare: exactly 1 operator required (inside %r)' % ( |
| 367 | condition)) |
| 368 | if len(node.comparators) != 1: |
| 369 | raise ValueError( |
| 370 | 'invalid compare: exactly 1 comparator required (inside %r)' % ( |
| 371 | condition)) |
| 372 | |
| 373 | left = _convert(node.left) |
| 374 | right = _convert(node.comparators[0]) |
| 375 | |
| 376 | if isinstance(node.ops[0], ast.Eq): |
| 377 | return left == right |
Dirk Pranke | 77b7687 | 2017-10-06 01:29:27 | [diff] [blame] | 378 | if isinstance(node.ops[0], ast.NotEq): |
| 379 | return left != right |
Paweł Hajdan, Jr | 76c6ea2 | 2017-06-02 19:46:57 | [diff] [blame] | 380 | |
| 381 | raise ValueError( |
| 382 | 'unexpected operator: %s %s (inside %r)' % ( |
| 383 | node.ops[0], ast.dump(node), condition)) |
| 384 | else: |
| 385 | raise ValueError( |
| 386 | 'unexpected AST node: %s %s (inside %r)' % ( |
| 387 | node, ast.dump(node), condition)) |
| 388 | return _convert(main_node) |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 389 | |
| 390 | |
| 391 | def RenderDEPSFile(gclient_dict): |
| 392 | contents = sorted(gclient_dict.tokens.values(), key=lambda token: token[2]) |
| 393 | return tokenize.untokenize(contents) |
| 394 | |
| 395 | |
| 396 | def _UpdateAstString(tokens, node, value): |
| 397 | position = node.lineno, node.col_offset |
| 398 | tokens[position][1] = repr(value) |
| 399 | node.s = value |
| 400 | |
| 401 | |
| 402 | def SetVar(gclient_dict, var_name, value): |
| 403 | if not isinstance(gclient_dict, _NodeDict) or gclient_dict.tokens is None: |
| 404 | raise ValueError( |
| 405 | "Can't use SetVar for the given gclient dict. It contains no " |
| 406 | "formatting information.") |
| 407 | tokens = gclient_dict.tokens |
| 408 | |
| 409 | if 'vars' not in gclient_dict or var_name not in gclient_dict['vars']: |
| 410 | raise ValueError( |
| 411 | "Could not find any variable called %s." % var_name) |
| 412 | |
| 413 | node = gclient_dict['vars'].GetNode(var_name) |
| 414 | if node is None: |
| 415 | raise ValueError( |
| 416 | "The vars entry for %s has no formatting information." % var_name) |
| 417 | |
| 418 | _UpdateAstString(tokens, node, value) |
| 419 | gclient_dict['vars']._SetNode(var_name, value, node) |
| 420 | |
| 421 | |
| 422 | def SetCIPD(gclient_dict, dep_name, package_name, new_version): |
| 423 | if not isinstance(gclient_dict, _NodeDict) or gclient_dict.tokens is None: |
| 424 | raise ValueError( |
| 425 | "Can't use SetCIPD for the given gclient dict. It contains no " |
| 426 | "formatting information.") |
| 427 | tokens = gclient_dict.tokens |
| 428 | |
| 429 | if 'deps' not in gclient_dict or dep_name not in gclient_dict['deps']: |
| 430 | raise ValueError( |
| 431 | "Could not find any dependency called %s." % dep_name) |
| 432 | |
| 433 | # Find the package with the given name |
| 434 | packages = [ |
| 435 | package |
| 436 | for package in gclient_dict['deps'][dep_name]['packages'] |
| 437 | if package['package'] == package_name |
| 438 | ] |
| 439 | if len(packages) != 1: |
| 440 | raise ValueError( |
| 441 | "There must be exactly one package with the given name (%s), " |
| 442 | "%s were found." % (package_name, len(packages))) |
| 443 | |
| 444 | # TODO(ehmaldonado): Support Var in package's version. |
| 445 | node = packages[0].GetNode('version') |
| 446 | if node is None: |
| 447 | raise ValueError( |
| 448 | "The deps entry for %s:%s has no formatting information." % |
| 449 | (dep_name, package_name)) |
| 450 | |
| 451 | new_version = 'version:' + new_version |
| 452 | _UpdateAstString(tokens, node, new_version) |
| 453 | packages[0]._SetNode('version', new_version, node) |
| 454 | |
| 455 | |
Edward Lesmes | 9f53129 | 2018-03-21 01:27:15 | [diff] [blame^] | 456 | def SetRevision(gclient_dict, dep_name, new_revision): |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 457 | if not isinstance(gclient_dict, _NodeDict) or gclient_dict.tokens is None: |
| 458 | raise ValueError( |
| 459 | "Can't use SetRevision for the given gclient dict. It contains no " |
| 460 | "formatting information.") |
| 461 | tokens = gclient_dict.tokens |
| 462 | |
| 463 | if 'deps' not in gclient_dict or dep_name not in gclient_dict['deps']: |
| 464 | raise ValueError( |
| 465 | "Could not find any dependency called %s." % dep_name) |
| 466 | |
| 467 | def _UpdateRevision(dep_dict, dep_key): |
| 468 | dep_node = dep_dict.GetNode(dep_key) |
| 469 | if dep_node is None: |
| 470 | raise ValueError( |
| 471 | "The deps entry for %s has no formatting information." % dep_name) |
| 472 | |
| 473 | node = dep_node |
| 474 | if isinstance(node, ast.BinOp): |
| 475 | node = node.right |
| 476 | if isinstance(node, ast.Call): |
| 477 | SetVar(gclient_dict, node.args[0].s, new_revision) |
| 478 | else: |
| 479 | _UpdateAstString(tokens, node, new_revision) |
Edward Lesmes | 9f53129 | 2018-03-21 01:27:15 | [diff] [blame^] | 480 | value = _gclient_eval(dep_node) |
Edward Lesmes | 6f64a05 | 2018-03-20 21:35:49 | [diff] [blame] | 481 | dep_dict._SetNode(dep_key, value, dep_node) |
| 482 | |
| 483 | if isinstance(gclient_dict['deps'][dep_name], _NodeDict): |
| 484 | _UpdateRevision(gclient_dict['deps'][dep_name], 'url') |
| 485 | else: |
| 486 | _UpdateRevision(gclient_dict['deps'], dep_name) |