Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 3 | import os, os.path, sys, argparse |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 4 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 5 | SOURCE_TEMPLATE = """ |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 6 | #include "internal/Embedded.h" |
| 7 | |
| 8 | #include <string> |
| 9 | #include <unordered_map> |
| 10 | |
| 11 | namespace { |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 12 | %s |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 13 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 14 | const std::unordered_map<std::string, EmbeddedContent> embedded = { |
| 15 | %s |
| 16 | }; |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 17 | |
| 18 | } // namespace |
| 19 | |
| 20 | const EmbeddedContent* findEmbeddedContent(const std::string& name) { |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 21 | const auto found = embedded.find(name); |
| 22 | if (found == embedded.end()) { |
| 23 | return nullptr; |
| 24 | } |
| 25 | return &found->second; |
| 26 | }\n |
Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame] | 27 | """ |
| 28 | |
Austin Schuh | 9d82300 | 2019-04-14 12:53:17 -0700 | [diff] [blame^] | 29 | MAX_SLICE = 70 |
| 30 | |
| 31 | |
| 32 | def as_byte(data): |
| 33 | if sys.version_info < (3,): |
| 34 | return ord(data) |
| 35 | else: |
| 36 | return data |
| 37 | |
| 38 | |
| 39 | def parse_arguments(): |
| 40 | parser = argparse.ArgumentParser(description="Embedded content generator") |
| 41 | parser.add_argument('--output', '-o', action='store', dest='output_file', type=str, help='Output File', required=True) |
| 42 | parser.add_argument('--file', '-f', action='store', nargs='+', dest='input_file', type=str, help='Output File', required=True) |
| 43 | return parser.parse_args() |
| 44 | |
| 45 | |
| 46 | def create_file_byte(name, file_bytes): |
| 47 | output = [] |
| 48 | output.append(' const char %s[] = {' % name) |
| 49 | |
| 50 | for start in range(0, len(file_bytes), MAX_SLICE): |
| 51 | output.append('' + "".join(["'\\x%02x'," % as_byte(x) for x in file_bytes[start:start+MAX_SLICE]]) + "\n") |
| 52 | output.append('0};\n') |
| 53 | return ''.join(output) |
| 54 | |
| 55 | |
| 56 | def create_file_info(file_list): |
| 57 | output = [] |
| 58 | for name, base, length in file_list: |
| 59 | output.append(' {"/%s", { %s, %d }},\n' % (base, name, length)) |
| 60 | return ''.join(output) |
| 61 | |
| 62 | |
| 63 | def main(): |
| 64 | args = parse_arguments() |
| 65 | |
| 66 | files = [] |
| 67 | index = 1 |
| 68 | file_byte_entries = [] |
| 69 | |
| 70 | for file_name in args.input_file: |
| 71 | with open(file_name, 'rb') as f: |
| 72 | file_bytes = f.read() |
| 73 | name = "fileData%d" % index |
| 74 | index += 1 |
| 75 | files.append((name, os.path.basename(file_name), len(file_bytes))) |
| 76 | file_byte_entries.append(create_file_byte(name, file_bytes)) |
| 77 | |
| 78 | with open(args.output_file, 'w') as output_file: |
| 79 | output_file.write(SOURCE_TEMPLATE % (''.join(file_byte_entries), create_file_info(files))) |
| 80 | |
| 81 | |
| 82 | if __name__ == '__main__': |
| 83 | main() |