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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 result = {} | 121 result = {} |
122 for key, value in values.items(): | 122 for key, value in values.items(): |
123 new_key = ReplaceSubstrings(key, search_for, replace_with) | 123 new_key = ReplaceSubstrings(key, search_for, replace_with) |
124 new_value = ReplaceSubstrings(value, search_for, replace_with) | 124 new_value = ReplaceSubstrings(value, search_for, replace_with) |
125 result[new_key] = new_value | 125 result[new_key] = new_value |
126 return result | 126 return result |
127 | 127 |
128 # Assume everything else is unchanged. | 128 # Assume everything else is unchanged. |
129 return values | 129 return values |
130 | 130 |
| 131 def KeepOnly(values, filters): |
| 132 """Recursively filters out strings not ending in "f" from "values""" |
| 133 |
| 134 if isinstance(values, list): |
| 135 return [v for v in values if v.endswith(tuple(filters))] |
| 136 |
| 137 if isinstance(values, dict): |
| 138 result = {} |
| 139 for key, value in values.items(): |
| 140 new_key = KeepOnly(key, filters) |
| 141 new_value = KeepOnly(value, filters) |
| 142 result[new_key] = new_value |
| 143 return result |
| 144 |
| 145 return values |
| 146 |
131 def main(): | 147 def main(): |
132 parser = OptionParser() | 148 parser = OptionParser() |
133 parser.add_option("-r", "--replace", action="append", | 149 parser.add_option("-r", "--replace", action="append", |
134 help="Replaces substrings. If passed a=b, replaces all substrs a with b.") | 150 help="Replaces substrings. If passed a=b, replaces all substrs a with b.") |
| 151 parser.add_option("-k", "--keep_only", default = [], action="append", |
| 152 help="Keeps only files ending with the listed strings.") |
135 (options, args) = parser.parse_args() | 153 (options, args) = parser.parse_args() |
136 | 154 |
137 if len(args) != 1: | 155 if len(args) != 1: |
138 raise Exception("Need one argument which is the .gypi file to read.") | 156 raise Exception("Need one argument which is the .gypi file to read.") |
139 | 157 |
140 data = LoadPythonDictionary(args[0]) | 158 data = LoadPythonDictionary(args[0]) |
141 if options.replace: | 159 if options.replace: |
142 # Do replacements for all specified patterns. | 160 # Do replacements for all specified patterns. |
143 for replace in options.replace: | 161 for replace in options.replace: |
144 split = replace.split('=') | 162 split = replace.split('=') |
145 # Allow "foo=" to replace with nothing. | 163 # Allow "foo=" to replace with nothing. |
146 if len(split) == 1: | 164 if len(split) == 1: |
147 split.append('') | 165 split.append('') |
148 assert len(split) == 2, "Replacement must be of the form 'key=value'." | 166 assert len(split) == 2, "Replacement must be of the form 'key=value'." |
149 data = ReplaceSubstrings(data, split[0], split[1]) | 167 data = ReplaceSubstrings(data, split[0], split[1]) |
150 | 168 |
| 169 if options.keep_only != []: |
| 170 data = KeepOnly(data, options.keep_only) |
| 171 |
151 # Sometimes .gypi files use the GYP syntax with percents at the end of the | 172 # Sometimes .gypi files use the GYP syntax with percents at the end of the |
152 # variable name (to indicate not to overwrite a previously-defined value): | 173 # variable name (to indicate not to overwrite a previously-defined value): |
153 # 'foo%': 'bar', | 174 # 'foo%': 'bar', |
154 # Convert these to regular variables. | 175 # Convert these to regular variables. |
155 for key in data: | 176 for key in data: |
156 if len(key) > 1 and key[len(key) - 1] == '%': | 177 if len(key) > 1 and key[len(key) - 1] == '%': |
157 data[key[:-1]] = data[key] | 178 data[key[:-1]] = data[key] |
158 del data[key] | 179 del data[key] |
159 | 180 |
160 print gn_helpers.ToGNString(data) | 181 print gn_helpers.ToGNString(data) |
161 | 182 |
162 if __name__ == '__main__': | 183 if __name__ == '__main__': |
163 try: | 184 try: |
164 main() | 185 main() |
165 except Exception, e: | 186 except Exception, e: |
166 print str(e) | 187 print str(e) |
167 sys.exit(1) | 188 sys.exit(1) |
OLD | NEW |