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..3268698380a25c5b610ab9f3de0037449840c769 |
| --- /dev/null |
| +++ b/remoting/tools/build/remoting_c_text_inliner.py |
| @@ -0,0 +1,84 @@ |
| +#!/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: |
|
Jamie
2016/06/04 00:24:37
Make it clear here that a.vert and b.frag are inpu
Yuwei
2016/06/04 01:31:42
Done.
|
| + |
| +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" |
|
Jamie
2016/06/04 00:24:37
You can't append string like this in C. Just omitt
Yuwei
2016/06/04 01:31:42
Done.
|
| + + "3\\"; |
| + |
| +const char kBFrag[] = "4\n" |
| + + "5\n" |
| + + "6"; |
| + |
| +#endif // OUTPUT_H |
| + |
| +""" |
| + |
| +import os.path |
| +import sys |
| + |
| +def escape_line(line): |
| + # encode('string-escape') doesn't escape double quote so you need to manually |
| + # escape it. |
| + return line.encode('string-escape').replace('"', '\\"') |
| + |
| +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] |
|
Jamie
2016/06/04 00:24:37
Don't need blank lines between these.
Yuwei
2016/06/04 01:31:42
Removed blank line between output_path and include
|
| + |
| + 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') |
|
Jamie
2016/06/04 00:24:37
You can use the "with" keyword to automatically cl
Yuwei
2016/06/04 01:31:42
Done.
|
| + |
| + # hello_world.vert -> kHelloWorldVert |
| + const_name = 'k' + os.path.basename(input_path).title().replace('_', '') \ |
| + .replace('.', '') |
|
Jamie
2016/06/04 00:24:37
Since you're losing path information when converti
Yuwei
2016/06/04 01:31:42
Done. Added checks.
|
| + text = input_file.read() |
| + lines = text.splitlines() |
| + |
| + # a, b -> "a, "b -> "a\n" + "b -> "a\n" + "b" |
|
Jamie
2016/06/04 00:24:37
I don't understand this comment.
Yuwei
2016/06/04 01:31:42
Just trying to explain how the strings are concate
|
| + 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()) |