Chromium Code Reviews| 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 (it will check for the presence of a "conditions" value in the | 10 conditions (it will check for the presence of a "conditions" value in the |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 component("mycomponent") { | 39 component("mycomponent") { |
| 40 sources = gypi_values.sources | 40 sources = gypi_values.sources |
| 41 defines = gypi_values.defines | 41 defines = gypi_values.defines |
| 42 } | 42 } |
| 43 | 43 |
| 44 Sometimes your .gypi file will include paths relative to a different | 44 Sometimes your .gypi file will include paths relative to a different |
| 45 directory than the current .gn file. In this case, you can rebase them to | 45 directory than the current .gn file. In this case, you can rebase them to |
| 46 be relative to the current directory. | 46 be relative to the current directory. |
| 47 sources = rebase_path(gypi_values.sources, ".", | 47 sources = rebase_path(gypi_values.sources, ".", |
| 48 "//path/gypi/input/values/are/relative/to") | 48 "//path/gypi/input/values/are/relative/to") |
| 49 | |
| 50 This script also has the ability to replace certain substrings in the input. | |
| 51 Generally this is used to emulate GYP variable expansion. If you passed the | |
| 52 argument "--replace=<(foo)=bar" then all instances of "<(foo)" in strings in | |
| 53 the input will be replaced with "bar": | |
| 54 | |
| 55 gypi_values = exec_script("//build/gypi_to_gn.py", | |
| 56 [ rebase_path("your_file.gypi"), | |
| 57 "--replace=<(foo)=bar"], | |
| 58 "scope", | |
| 59 [ "your_file.gypi" ]) | |
| 60 | |
| 49 """ | 61 """ |
| 50 | 62 |
| 51 import gn_helpers | 63 import gn_helpers |
| 52 from optparse import OptionParser | 64 from optparse import OptionParser |
| 53 import sys | 65 import sys |
| 54 | 66 |
| 55 def LoadPythonDictionary(path): | 67 def LoadPythonDictionary(path): |
| 56 file_string = open(path).read() | 68 file_string = open(path).read() |
| 57 try: | 69 try: |
| 58 file_data = eval(file_string, {'__builtins__': None}, None) | 70 file_data = eval(file_string, {'__builtins__': None}, None) |
| 59 except SyntaxError, e: | 71 except SyntaxError, e: |
| 60 e.filename = path | 72 e.filename = path |
| 61 raise | 73 raise |
| 62 except Exception, e: | 74 except Exception, e: |
| 63 raise Exception("Unexpected error while reading %s: %s" % (path, str(e))) | 75 raise Exception("Unexpected error while reading %s: %s" % (path, str(e))) |
| 64 | 76 |
| 65 assert isinstance(file_data, dict), "%s does not eval to a dictionary" % path | 77 assert isinstance(file_data, dict), "%s does not eval to a dictionary" % path |
| 66 assert 'conditions' not in file_data, \ | 78 assert 'conditions' not in file_data, \ |
| 67 "The file %s has conditions in it, these aren't supported." % path | 79 "The file %s has conditions in it, these aren't supported." % path |
| 68 | 80 |
| 69 return file_data | 81 return file_data |
| 70 | 82 |
| 71 | 83 |
| 84 def ReplaceSubstrings(values, search_for, replace_with): | |
| 85 """Recursively replaces substrings in a value. | |
| 86 | |
| 87 Replaces all substrings of the "search_for" with "repace_with" for all | |
| 88 strings occurring in "values". This is done by recursively iterating into | |
| 89 lists as well as the keys and values of dictionaries.""" | |
| 90 if isinstance(values, str): | |
| 91 return values.replace(search_for, replace_with) | |
| 92 | |
| 93 if isinstance(values, list): | |
| 94 result = [] | |
| 95 for v in values: | |
| 96 result.append(ReplaceSubstrings(v, search_for, replace_with)) | |
| 97 return result | |
|
Dirk Pranke
2014/04/10 01:18:20
Nit: one line: "return [ReplaceSubstrings(v, searc
| |
| 98 | |
| 99 if isinstance(values, dict): | |
| 100 # For dictionaries, do the search for both the key and values. | |
| 101 result = {} | |
| 102 for v in values: | |
| 103 result[ReplaceSubstrings(v, search_for, replace_with)] = \ | |
| 104 ReplaceSubstrings(values[v], search_for, replace_with) | |
|
Dirk Pranke
2014/04/10 01:18:20
Nit: I'd probably rewrite this to:
result = {}
fo
| |
| 105 return result | |
| 106 | |
| 107 # Assume everything else is unchanged. | |
| 108 return values | |
| 109 | |
| 72 def main(): | 110 def main(): |
| 73 parser = OptionParser() | 111 parser = OptionParser() |
| 112 parser.add_option("-r", "--replace", action="append", | |
| 113 help="Replaces substrings. If passed a=b, replaces all substrs a with b.") | |
| 74 (options, args) = parser.parse_args() | 114 (options, args) = parser.parse_args() |
| 75 | 115 |
| 76 if len(args) != 1: | 116 if len(args) != 1: |
| 77 raise Exception("Need one argument which is the .gypi file to read.") | 117 raise Exception("Need one argument which is the .gypi file to read.") |
| 78 | 118 |
| 79 data = LoadPythonDictionary(args[0]) | 119 data = LoadPythonDictionary(args[0]) |
| 120 if options.replace: | |
| 121 # Do replacements for all specified patterns. | |
| 122 for replace in options.replace: | |
| 123 split = replace.split('=') | |
| 124 # Allow "foo=" to replace with nothing. | |
| 125 if len(split) == 1: | |
| 126 split.append('') | |
| 127 assert len(split) == 2, "Replacement must be of the form 'key=value'." | |
| 128 data = ReplaceSubstrings(data, split[0], split[1]) | |
| 129 | |
| 80 print gn_helpers.ToGNString(data) | 130 print gn_helpers.ToGNString(data) |
| 81 | 131 |
| 82 if __name__ == '__main__': | 132 if __name__ == '__main__': |
| 83 try: | 133 try: |
| 84 main() | 134 main() |
| 85 except Exception, e: | 135 except Exception, e: |
| 86 print str(e) | 136 print str(e) |
| 87 sys.exit(1) | 137 sys.exit(1) |
| OLD | NEW |