OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Generates a C++ source file which defines a string constant containing the |
| 7 contents of a catalog manifest file. Useful for baking catalogs into binaries |
| 8 which don't want to hit disk before initializing the catalog.""" |
| 9 |
| 10 import argparse |
| 11 import json |
| 12 import os.path |
| 13 import sys |
| 14 |
| 15 |
| 16 # Token used to delimit raw strings in the generated source file. It's illegal |
| 17 # for this token to appear within the contents of the input manifest itself. |
| 18 _RAW_STRING_DELIMITER = "#CATALOG_JSON#" |
| 19 |
| 20 |
| 21 def main(): |
| 22 parser = argparse.ArgumentParser( |
| 23 description="Generates a C++ constant containing a catalog manifest.") |
| 24 parser.add_argument("--input") |
| 25 parser.add_argument("--output") |
| 26 parser.add_argument("--symbol-name") |
| 27 parser.add_argument("--pretty", action="store_true") |
| 28 args, _ = parser.parse_known_args() |
| 29 |
| 30 if args.input is None or args.output is None or args.symbol_name is None: |
| 31 raise Exception("--input, --output, and --symbol-name are required") |
| 32 |
| 33 with open(args.input, 'r') as input_file: |
| 34 manifest_contents = input_file.read() |
| 35 |
| 36 if manifest_contents.find(_RAW_STRING_DELIMITER) >= 0: |
| 37 raise Exception( |
| 38 "Unexpected '%s' found in input manifest." % _RAW_STRING_DELIMITER) |
| 39 |
| 40 qualified_symbol_name = args.symbol_name.split("::") |
| 41 namespace = qualified_symbol_name[0:-1] |
| 42 symbol_name = qualified_symbol_name[-1] |
| 43 |
| 44 with open(args.output, 'w') as output_file: |
| 45 output_file.write( |
| 46 "// This is a generated file produced by\n" |
| 47 "// src/services/catalog/public/tools/sourcify_manifest.py.\n\n") |
| 48 for name in namespace: |
| 49 output_file.write("namespace %s {\n" % name) |
| 50 output_file.write("\nextern const char %s[];" % symbol_name) |
| 51 output_file.write("\nconst char %s[] = R\"%s(%s)%s\";\n\n" % |
| 52 (symbol_name, _RAW_STRING_DELIMITER, manifest_contents, |
| 53 _RAW_STRING_DELIMITER)) |
| 54 for name in reversed(namespace): |
| 55 output_file.write("} // %s\n" % name) |
| 56 |
| 57 return 0 |
| 58 |
| 59 if __name__ == "__main__": |
| 60 sys.exit(main()) |
OLD | NEW |