OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2017 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import argparse | |
6 import sys | |
7 import os | |
8 | |
9 COPYRIGHT="""// Copyright 2016 The Chromium Authors. All rights reserved. | |
Sami
2017/02/09 10:46:58
++year
altimin
2017/02/09 11:13:46
Done.
| |
10 // Use of this source code is governed by a BSD-style license that can be | |
11 // found in the LICENSE file. | |
12 """ | |
13 | |
14 HEADER="""{copyright} | |
15 | |
16 #include "headless/lib/util/embedded_file.h" | |
17 | |
18 namespace {namespace} {{ | |
19 | |
20 extern const headless::util::EmbeddedFile {variable_name}; | |
21 | |
22 }} // namespace {namespace} | |
23 """ | |
24 | |
25 SOURCE="""{copyright} | |
26 | |
27 #include "{header_file}" | |
28 | |
29 namespace {{ | |
30 | |
31 const uint8_t contents[] = {contents}; | |
32 | |
33 }} // anonymous namespace | |
34 | |
35 namespace {namespace} {{ | |
36 | |
37 const headless::util::EmbeddedFile {variable_name} = {{ {length}, contents }}; | |
38 | |
39 }} // namespace {namespace} | |
40 """ | |
41 | |
42 def ParseArguments(args): | |
43 cmdline_parser = argparse.ArgumentParser() | |
44 cmdline_parser.add_argument('--data_file', required=True) | |
45 cmdline_parser.add_argument('--gendir', required=True) | |
46 cmdline_parser.add_argument('--header_file', required=True) | |
47 cmdline_parser.add_argument('--source_file', required=True) | |
48 cmdline_parser.add_argument('--namespace', required=True) | |
49 cmdline_parser.add_argument('--variable_name', required=True) | |
50 | |
51 return cmdline_parser.parse_args(args) | |
52 | |
53 | |
54 def GenerateArray(filepath): | |
55 with open(filepath, 'rb') as f: | |
56 contents = f.read() | |
57 | |
58 contents = [ str(ord(char)) for char in contents ] | |
59 | |
60 return len(contents), '{' + ','.join(contents) + '}' | |
61 | |
62 | |
63 def GenerateHeader(args): | |
64 return HEADER.format( | |
65 copyright=COPYRIGHT, | |
66 namespace=args.namespace, | |
67 variable_name=args.variable_name) | |
68 | |
69 def GenerateSource(args): | |
70 length, contents = GenerateArray(args.data_file) | |
71 | |
72 return SOURCE.format( | |
73 copyright=COPYRIGHT, | |
74 header_file=args.header_file, | |
75 namespace=args.namespace, | |
76 length=length, | |
77 contents=contents, | |
78 variable_name=args.variable_name) | |
79 | |
80 | |
81 def WriteHeader(args): | |
82 with open(os.path.join(args.gendir, args.header_file), 'w') as f: | |
83 f.write(GenerateHeader(args)) | |
84 | |
85 | |
86 def WriteSource(args): | |
87 with open(os.path.join(args.gendir, args.source_file), 'w') as f: | |
88 f.write(GenerateSource(args)) | |
89 | |
90 | |
91 if __name__ == '__main__': | |
92 args = ParseArguments(sys.argv[1:]) | |
93 | |
94 WriteHeader(args) | |
95 WriteSource(args) | |
96 | |
OLD | NEW |