Chromium Code Reviews| Index: remoting/tools/build/remoting_c_text_inliner.py |
| diff --git a/remoting/tools/build/remoting_c_text_inliner.py b/remoting/tools/build/remoting_c_text_inliner.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..5e3317f1d19bec98662b1ba6e56ea01dc7793681 |
| --- /dev/null |
| +++ b/remoting/tools/build/remoting_c_text_inliner.py |
| @@ -0,0 +1,82 @@ |
| +#!/usr/bin/python |
| +# Copyright 2016 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. |
| + |
| +"""Helper script to inline files as const char[] into a C header file. |
| + |
| +Example: |
| + |
| +a.vert: |
| +1" |
| +2 |
| +3\ |
| + |
| +b.frag: |
| +4 |
| +5 |
| +6 |
| + |
| +remoting_c_text_inliner.py "OUTPUT_H_" "output.h" "a.vert" "b.frag": |
| +#ifndef OUTPUT_H |
| +#define OUTPUT_H |
| + |
| +const char kAVert[] = "1\"\n" |
| + + "2\n" |
| + + "3\\"; |
| + |
| +const char kBFrag[] = "4\n" |
| + + "5\n" |
| + + "6"; |
| + |
| +#endif //OUTPUT_H |
| + |
| +""" |
| + |
| +import os.path |
| +import sys |
| + |
| +def escape_line(line): |
| + return line.encode('string-escape').replace('"', '\\"') |
|
Yuwei
2016/06/03 23:33:00
Forgot to leave comment for this. encode() escapes
|
| + |
| +def main(): |
| + if len(sys.argv) < 4: |
| + print 'Usage: remoting_c_text_inliner.py <include-guard> ' + \ |
| + '<output-file> <input-files...>' |
| + return 1 |
| + |
| + include_guard = sys.argv[1] |
| + |
| + output_path = sys.argv[2] |
| + |
| + output_file = open(output_path, 'w') |
| + |
| + output_file.write('#ifndef ' + include_guard + '\n' + |
| + '#define ' + include_guard + '\n\n') |
| + |
| + argc = len(sys.argv) |
| + |
| + for i in xrange(3, argc): |
| + input_path = sys.argv[i] |
| + input_file = open(input_path, 'r') |
| + |
| + # hello_world.vert -> kHelloWorldVert |
| + const_name = 'k' + os.path.basename(input_path).title().replace('_', '') \ |
| + .replace('.', '') |
| + text = input_file.read() |
| + lines = text.splitlines() |
| + |
| + # a, b -> "a, "b -> "a\n" |
|
Yuwei
2016/06/03 23:33:00
oops... Should be "a, "b -> "a\n", "b -> "a\n", "b
|
| + inlined = '\\n"\n + '.join(map(lambda x: '"' + escape_line(x), lines)) + '"' |
| + output_file.write('const char ' + const_name + '[]' + ' = ' + inlined + |
| + ';\n\n') |
| + input_file.close() |
| + |
| + output_file.write('#endif //' + include_guard + '\n') |
| + output_file.close() |
| + |
| + return 0 |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |