Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(499)

Unified Diff: build/gypi_to_gn.py

Issue 231983002: Add support for substring replacement in gypi_to_gn (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | skia/BUILD.gn » ('j') | skia/skia_gn_files.gypi » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/gypi_to_gn.py
diff --git a/build/gypi_to_gn.py b/build/gypi_to_gn.py
index 1446d811630c1fd2023291aed5c1dd6194ca6c39..fb51be4140b60054a5e682b6147aabc3576d2573 100644
--- a/build/gypi_to_gn.py
+++ b/build/gypi_to_gn.py
@@ -46,6 +46,18 @@ directory than the current .gn file. In this case, you can rebase them to
be relative to the current directory.
sources = rebase_path(gypi_values.sources, ".",
"//path/gypi/input/values/are/relative/to")
+
+This script also has the ability to replace certain substrings in the input.
+Generally this is used to emulate GYP variable expansion. If you passed the
+argument "--replace=<(foo)=bar" then all instances of "<(foo)" in strings in
+the input will be replaced with "bar":
+
+ gypi_values = exec_script("//build/gypi_to_gn.py",
+ [ rebase_path("your_file.gypi"),
+ "--replace=<(foo)=bar"],
+ "scope",
+ [ "your_file.gypi" ])
+
"""
import gn_helpers
@@ -69,14 +81,52 @@ def LoadPythonDictionary(path):
return file_data
+def ReplaceSubstrings(values, search_for, replace_with):
+ """Recursively replaces substrings in a value.
+
+ Replaces all substrings of the "search_for" with "repace_with" for all
+ strings occurring in "values". This is done by recursively iterating into
+ lists as well as the keys and values of dictionaries."""
+ if isinstance(values, str):
+ return values.replace(search_for, replace_with)
+
+ if isinstance(values, list):
+ result = []
+ for v in values:
+ result.append(ReplaceSubstrings(v, search_for, replace_with))
+ return result
Dirk Pranke 2014/04/10 01:18:20 Nit: one line: "return [ReplaceSubstrings(v, searc
+
+ if isinstance(values, dict):
+ # For dictionaries, do the search for both the key and values.
+ result = {}
+ for v in values:
+ result[ReplaceSubstrings(v, search_for, replace_with)] = \
+ ReplaceSubstrings(values[v], search_for, replace_with)
Dirk Pranke 2014/04/10 01:18:20 Nit: I'd probably rewrite this to: result = {} fo
+ return result
+
+ # Assume everything else is unchanged.
+ return values
+
def main():
parser = OptionParser()
+ parser.add_option("-r", "--replace", action="append",
+ help="Replaces substrings. If passed a=b, replaces all substrs a with b.")
(options, args) = parser.parse_args()
if len(args) != 1:
raise Exception("Need one argument which is the .gypi file to read.")
data = LoadPythonDictionary(args[0])
+ if options.replace:
+ # Do replacements for all specified patterns.
+ for replace in options.replace:
+ split = replace.split('=')
+ # Allow "foo=" to replace with nothing.
+ if len(split) == 1:
+ split.append('')
+ assert len(split) == 2, "Replacement must be of the form 'key=value'."
+ data = ReplaceSubstrings(data, split[0], split[1])
+
print gn_helpers.ToGNString(data)
if __name__ == '__main__':
« no previous file with comments | « no previous file | skia/BUILD.gn » ('j') | skia/skia_gn_files.gypi » ('J')

Powered by Google App Engine
This is Rietveld 408576698