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

Unified Diff: mojo/tools/embed/embed_data.py

Issue 979043003: Fix shell_apptest for android. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 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 | « mojo/tools/embed/data.h ('k') | mojo/tools/embed/rules.gni » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/tools/embed/embed_data.py
diff --git a/mojo/tools/embed/embed_data.py b/mojo/tools/embed/embed_data.py
new file mode 100755
index 0000000000000000000000000000000000000000..8156fa474de240d3df55dcaf9ab3aeedceb46bab
--- /dev/null
+++ b/mojo/tools/embed/embed_data.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+# Copyright 2015 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 argparse
+import hashlib
+import os
+import sys
+
+def main():
+ """Command line utility to embed file in a C executable."""
+ parser = argparse.ArgumentParser(
+ description='Generate a source file to embed the content of a file')
+
+ parser.add_argument('source')
+ parser.add_argument('out_dir')
+ parser.add_argument('variable')
+
+ opts = parser.parse_args()
+
+ if not os.path.exists(opts.out_dir):
+ os.makedirs(opts.out_dir)
+
+ header = os.path.join(opts.out_dir, '%s.h' % opts.variable)
+ c_file = os.path.join(opts.out_dir, '%s.cc' % opts.variable)
+
+ data = None
+ with open(opts.source, "rb") as f:
+ data = f.read()
+
+ with open(header, "w") as f:
+ f.write('// Generated file. Do not modify.\n')
+ f.write('\n')
+ f.write('#include "mojo/tools/embed/data.h"\n')
+ f.write('\n')
+ f.write('namespace embed {\n')
+ f.write('extern const mojo::embed::Data %s;\n' % opts.variable);
+ f.write('} // namespace embed\n')
+
+ sha1hash = hashlib.sha1(data).hexdigest()
+ values = ["0x%02x" % ord(c) for c in data]
+ lines = []
+ chunk_size = 16
+ for i in range(0, len(values), chunk_size):
+ lines.append(" " + ", ".join(values[i: i + chunk_size]))
+
+ with open(c_file, "w") as f:
+ f.write('// Generated file. Do not modify.\n')
+ f.write('\n')
+ f.write('#include "mojo/tools/embed/data.h"\n')
+ f.write('\n')
+ f.write('namespace embed {\n')
abarth-chromium 2015/03/06 21:41:14 Seems odd to put this all into the same namespace.
+ f.write('namespace {\n')
+ f.write("const char data[%d] = {\n" % len(data))
+ f.write(",\n".join(lines))
+ f.write("\n};\n")
+ f.write('} // namespace\n')
+ f.write('\n')
+ f.write('extern const mojo::embed::Data %s;\n' % opts.variable);
+ f.write('const mojo::embed::Data %s = {\n' % opts.variable);
+ f.write(' "%s",\n' % sha1hash)
+ f.write(' data,\n')
+ f.write(' sizeof(data)\n')
+ f.write('};\n');
+ f.write('\n')
+ f.write('} // namespace embed\n')
+
+if __name__ == '__main__':
+ main()
« no previous file with comments | « mojo/tools/embed/data.h ('k') | mojo/tools/embed/rules.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698