| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. 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 """Converts a given gypi file to a python scope and writes the result to stdout. | 5 """Converts a given gypi file to a python scope and writes the result to stdout. |
| 6 | 6 |
| 7 It is assumed that the file contains a toplevel dictionary, and this script | 7 It is assumed that the file contains a toplevel dictionary, and this script |
| 8 will return that dictionary as a GN "scope" (see example below). This script | 8 will return that dictionary as a GN "scope" (see example below). This script |
| 9 does not know anything about GYP and it will not expand variables or execute | 9 does not know anything about GYP and it will not expand variables or execute |
| 10 conditions. | 10 conditions. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 try: | 78 try: |
| 79 file_data = eval(file_string, {'__builtins__': None}, None) | 79 file_data = eval(file_string, {'__builtins__': None}, None) |
| 80 except SyntaxError, e: | 80 except SyntaxError, e: |
| 81 e.filename = path | 81 e.filename = path |
| 82 raise | 82 raise |
| 83 except Exception, e: | 83 except Exception, e: |
| 84 raise Exception("Unexpected error while reading %s: %s" % (path, str(e))) | 84 raise Exception("Unexpected error while reading %s: %s" % (path, str(e))) |
| 85 | 85 |
| 86 assert isinstance(file_data, dict), "%s does not eval to a dictionary" % path | 86 assert isinstance(file_data, dict), "%s does not eval to a dictionary" % path |
| 87 | 87 |
| 88 # Flatten any variables to the top level. |
| 89 if 'variables' in file_data: |
| 90 file_data.update(file_data['variables']) |
| 91 del file_data['variables'] |
| 92 |
| 88 # Strip any conditions. | 93 # Strip any conditions. |
| 89 if 'conditions' in file_data: | 94 if 'conditions' in file_data: |
| 90 del file_data['conditions'] | 95 del file_data['conditions'] |
| 91 if 'target_conditions' in file_data: | 96 if 'target_conditions' in file_data: |
| 92 del file_data['target_conditions'] | 97 del file_data['target_conditions'] |
| 93 | 98 |
| 94 # Flatten any varaiables to the top level. | |
| 95 if 'variables' in file_data: | |
| 96 file_data.update(file_data['variables']) | |
| 97 del file_data['variables'] | |
| 98 | |
| 99 # If the contents of the root is a dictionary with exactly one kee | |
| 100 # "variables", promote the contents of that to the top level. Some .gypi | |
| 101 # files contain this and some don't depending on how they expect to be | |
| 102 # embedded in a .gyp file. We don't actually care either way so collapse it | |
| 103 # away. | |
| 104 if len(file_data) == 1 and 'variables' in file_data: | |
| 105 return file_data['variables'] | |
| 106 | |
| 107 return file_data | 99 return file_data |
| 108 | 100 |
| 109 | 101 |
| 110 def ReplaceSubstrings(values, search_for, replace_with): | 102 def ReplaceSubstrings(values, search_for, replace_with): |
| 111 """Recursively replaces substrings in a value. | 103 """Recursively replaces substrings in a value. |
| 112 | 104 |
| 113 Replaces all substrings of the "search_for" with "repace_with" for all | 105 Replaces all substrings of the "search_for" with "repace_with" for all |
| 114 strings occurring in "values". This is done by recursively iterating into | 106 strings occurring in "values". This is done by recursively iterating into |
| 115 lists as well as the keys and values of dictionaries.""" | 107 lists as well as the keys and values of dictionaries.""" |
| 116 if isinstance(values, str): | 108 if isinstance(values, str): |
| (...skipping 27 matching lines...) Expand all Loading... |
| 144 if options.replace: | 136 if options.replace: |
| 145 # Do replacements for all specified patterns. | 137 # Do replacements for all specified patterns. |
| 146 for replace in options.replace: | 138 for replace in options.replace: |
| 147 split = replace.split('=') | 139 split = replace.split('=') |
| 148 # Allow "foo=" to replace with nothing. | 140 # Allow "foo=" to replace with nothing. |
| 149 if len(split) == 1: | 141 if len(split) == 1: |
| 150 split.append('') | 142 split.append('') |
| 151 assert len(split) == 2, "Replacement must be of the form 'key=value'." | 143 assert len(split) == 2, "Replacement must be of the form 'key=value'." |
| 152 data = ReplaceSubstrings(data, split[0], split[1]) | 144 data = ReplaceSubstrings(data, split[0], split[1]) |
| 153 | 145 |
| 146 # Sometimes .gypi files use the GYP syntax with percents at the end of the |
| 147 # variable name (to indicate not to overwrite a previously-defined value): |
| 148 # 'foo%': 'bar', |
| 149 # Convert these to regular variables. |
| 150 for key in data: |
| 151 if len(key) > 1 and key[len(key) - 1] == '%': |
| 152 data[key[:-1]] = data[key] |
| 153 del data[key] |
| 154 |
| 154 print gn_helpers.ToGNString(data) | 155 print gn_helpers.ToGNString(data) |
| 155 | 156 |
| 156 if __name__ == '__main__': | 157 if __name__ == '__main__': |
| 157 try: | 158 try: |
| 158 main() | 159 main() |
| 159 except Exception, e: | 160 except Exception, e: |
| 160 print str(e) | 161 print str(e) |
| 161 sys.exit(1) | 162 sys.exit(1) |
| OLD | NEW |