| OLD | NEW |
| 1 # Copyright (c) 2012 Google Inc. All rights reserved. | 1 # Copyright (c) 2012 Google Inc. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from compiler.ast import Const | 5 import ast |
| 6 from compiler.ast import Dict | |
| 7 from compiler.ast import Discard | |
| 8 from compiler.ast import List | |
| 9 from compiler.ast import Module | |
| 10 from compiler.ast import Node | |
| 11 from compiler.ast import Stmt | |
| 12 import compiler | |
| 13 import gyp.common | 6 import gyp.common |
| 14 import gyp.simple_copy | 7 import gyp.simple_copy |
| 15 import multiprocessing | 8 import multiprocessing |
| 16 import optparse | 9 import optparse |
| 17 import os.path | 10 import os.path |
| 18 import re | 11 import re |
| 19 import shlex | 12 import shlex |
| 20 import signal | 13 import signal |
| 21 import subprocess | 14 import subprocess |
| 22 import sys | 15 import sys |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 | 168 |
| 176 def CheckedEval(file_contents): | 169 def CheckedEval(file_contents): |
| 177 """Return the eval of a gyp file. | 170 """Return the eval of a gyp file. |
| 178 | 171 |
| 179 The gyp file is restricted to dictionaries and lists only, and | 172 The gyp file is restricted to dictionaries and lists only, and |
| 180 repeated keys are not allowed. | 173 repeated keys are not allowed. |
| 181 | 174 |
| 182 Note that this is slower than eval() is. | 175 Note that this is slower than eval() is. |
| 183 """ | 176 """ |
| 184 | 177 |
| 185 ast = compiler.parse(file_contents) | 178 syntax_tree = ast.parse(file_contents) |
| 186 assert isinstance(ast, Module) | 179 assert isinstance(syntax_tree, ast.Module) |
| 187 c1 = ast.getChildren() | 180 c1 = syntax_tree.body |
| 188 assert c1[0] is None | 181 assert len(c1) == 1 |
| 189 assert isinstance(c1[1], Stmt) | 182 c2 = c1[0] |
| 190 c2 = c1[1].getChildren() | 183 assert isinstance(c2, ast.Expr) |
| 191 assert isinstance(c2[0], Discard) | 184 return CheckNode(c2.value, []) |
| 192 c3 = c2[0].getChildren() | |
| 193 assert len(c3) == 1 | |
| 194 return CheckNode(c3[0], []) | |
| 195 | 185 |
| 196 | 186 |
| 197 def CheckNode(node, keypath): | 187 def CheckNode(node, keypath): |
| 198 if isinstance(node, Dict): | 188 if isinstance(node, ast.Dict): |
| 199 c = node.getChildren() | |
| 200 dict = {} | 189 dict = {} |
| 201 for n in range(0, len(c), 2): | 190 for key, value in zip(node.keys, node.values): |
| 202 assert isinstance(c[n], Const) | 191 assert isinstance(key, ast.Str) |
| 203 key = c[n].getChildren()[0] | 192 key = key.s |
| 204 if key in dict: | 193 if key in dict: |
| 205 raise GypError("Key '" + key + "' repeated at level " + | 194 raise GypError("Key '" + key + "' repeated at level " + |
| 206 repr(len(keypath) + 1) + " with key path '" + | 195 repr(len(keypath) + 1) + " with key path '" + |
| 207 '.'.join(keypath) + "'") | 196 '.'.join(keypath) + "'") |
| 208 kp = list(keypath) # Make a copy of the list for descending this node. | 197 kp = list(keypath) # Make a copy of the list for descending this node. |
| 209 kp.append(key) | 198 kp.append(key) |
| 210 dict[key] = CheckNode(c[n + 1], kp) | 199 dict[key] = CheckNode(value, kp) |
| 211 return dict | 200 return dict |
| 212 elif isinstance(node, List): | 201 elif isinstance(node, ast.List): |
| 213 c = node.getChildren() | |
| 214 children = [] | 202 children = [] |
| 215 for index, child in enumerate(c): | 203 for index, child in enumerate(node.elts): |
| 216 kp = list(keypath) # Copy list. | 204 kp = list(keypath) # Copy list. |
| 217 kp.append(repr(index)) | 205 kp.append(repr(index)) |
| 218 children.append(CheckNode(child, kp)) | 206 children.append(CheckNode(child, kp)) |
| 219 return children | 207 return children |
| 220 elif isinstance(node, Const): | 208 elif isinstance(node, ast.Str): |
| 221 return node.getChildren()[0] | 209 return node.s |
| 222 else: | 210 else: |
| 223 raise TypeError("Unknown AST node at key path '" + '.'.join(keypath) + | 211 raise TypeError("Unknown AST node at key path '" + '.'.join(keypath) + |
| 224 "': " + repr(node)) | 212 "': " + repr(node)) |
| 225 | 213 |
| 226 | 214 |
| 227 def LoadOneBuildFile(build_file_path, data, aux_data, includes, | 215 def LoadOneBuildFile(build_file_path, data, aux_data, includes, |
| 228 is_target, check): | 216 is_target, check): |
| 229 if build_file_path in data: | 217 if build_file_path in data: |
| 230 return data[build_file_path] | 218 return data[build_file_path] |
| 231 | 219 |
| (...skipping 2658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2890 ValidateRunAsInTarget(target, target_dict, build_file) | 2878 ValidateRunAsInTarget(target, target_dict, build_file) |
| 2891 ValidateActionsInTarget(target, target_dict, build_file) | 2879 ValidateActionsInTarget(target, target_dict, build_file) |
| 2892 | 2880 |
| 2893 # Generators might not expect ints. Turn them into strs. | 2881 # Generators might not expect ints. Turn them into strs. |
| 2894 TurnIntIntoStrInDict(data) | 2882 TurnIntIntoStrInDict(data) |
| 2895 | 2883 |
| 2896 # TODO(mark): Return |data| for now because the generator needs a list of | 2884 # TODO(mark): Return |data| for now because the generator needs a list of |
| 2897 # build files that came in. In the future, maybe it should just accept | 2885 # build files that came in. In the future, maybe it should just accept |
| 2898 # a list, and not the whole data dict. | 2886 # a list, and not the whole data dict. |
| 2899 return [flat_list, targets, data] | 2887 return [flat_list, targets, data] |
| OLD | NEW |