| Index: build/gn_helpers.py
|
| diff --git a/build/gn_helpers.py b/build/gn_helpers.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..cb7531d9dfa119ef5926c239418ffa04c91e14a9
|
| --- /dev/null
|
| +++ b/build/gn_helpers.py
|
| @@ -0,0 +1,73 @@
|
| +# Copyright 2014 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +import sys
|
| +
|
| +# Helper functions useful when writing scripts that are run from GN's
|
| +# exec_script function.
|
| +
|
| +class Error(Exception):
|
| + pass
|
| +
|
| +
|
| +def ExceptionAppend(e, msg):
|
| + """Append a message to the given exception's message."""
|
| + if not e.args:
|
| + e.args = (msg,)
|
| + elif len(e.args) == 1:
|
| + e.args = (str(e.args[0]) + ' ' + msg,)
|
| + else:
|
| + e.args = (str(e.args[0]) + ' ' + msg,) + e.args[1:]
|
| +
|
| +
|
| +def PrintString(value):
|
| + """Writes a python string as a GN string."""
|
| + if not isinstance(value, str):
|
| + raise Error("Argument to PrintList is not a Python list.")
|
| + if value.find('\n') >= 0:
|
| + raise Error("Trying to print a string with a newline in it.")
|
| + sys.stdout.write('"' + value.replace('"', '\\"') + '"')
|
| +
|
| +
|
| +def PrintList(value):
|
| + """Writes a python list as a GN list."""
|
| + if not isinstance(value, list):
|
| + raise Error("Argument to PrintList is not a Python list.")
|
| + sys.stdout.write('[ ')
|
| + for i in value:
|
| + PrintValue(i, False)
|
| + sys.stdout.write(', ')
|
| + sys.stdout.write(']')
|
| +
|
| +
|
| +def PrintDict(value):
|
| + """Writes a python dictionary as a GN scope."""
|
| + for key in value:
|
| + if not isinstance(key, str):
|
| + raise Error("Dictionary key is not a string.")
|
| + sys.stdout.write(key + " = ")
|
| + PrintValue(value[key], False) # No recursive dictionaries.
|
| + sys.stdout.write('\n')
|
| +
|
| +
|
| +def PrintValue(value, allow_dicts = True):
|
| + """Prints the given value to stdout.
|
| +
|
| + allow_dicts indicates if this function will allow converting dictionaries
|
| + to GN scopes. This is only possible at the top level, you can't nest a
|
| + GN scope in a list, so this should be set to False for recursive calls."""
|
| + if isinstance(value, str):
|
| + PrintString(value)
|
| + elif isinstance(value, list):
|
| + PrintList(value)
|
| + elif isinstance(value, dict):
|
| + if not allow_dicts:
|
| + raise Error("Attempting to recursively print a dictionary.")
|
| + PrintDict(value)
|
| + elif isinstance(value, int):
|
| + sys.stdout.write(str(value))
|
| + else:
|
| + raise Error("Unsupported type when printing to GN.")
|
| +
|
| +
|
|
|