Jeremy Johnson | be1a940 | 2021-12-15 17:14:56 +0000 | [diff] [blame] | 1 | """Conversion utility from flatbuffer JSON files to binary and the reverse.""" |
| 2 | # Copyright (c) 2021-2022, ARM Limited. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | from pathlib import Path |
| 5 | from typing import Optional |
| 6 | |
Jeremy Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 7 | from runner.run_command import run_sh_command |
| 8 | from runner.run_command import RunShCommandError |
Jeremy Johnson | be1a940 | 2021-12-15 17:14:56 +0000 | [diff] [blame] | 9 | |
| 10 | |
| 11 | def fbbin_to_json(flatc: Path, fbs: Path, t_path: Path, o_path: Optional[Path] = None): |
| 12 | """Convert the binary flatbuffer to JSON. |
| 13 | |
| 14 | flatc: the Path to the flatc compiler program |
| 15 | fbs: the Path to the fbs (flatbuffer schema) file |
| 16 | t_path: the Path to the binary flatbuffer file |
| 17 | o_path: the output Path where JSON file will be put, if None, it is same as t_path |
| 18 | """ |
| 19 | if o_path is None: |
| 20 | o_path = t_path.parent |
| 21 | cmd = [ |
| 22 | str(flatc.absolute()), |
| 23 | "-o", |
| 24 | str(o_path.absolute()), |
| 25 | "--json", |
| 26 | "--defaults-json", |
| 27 | "--raw-binary", |
| 28 | str(fbs.absolute()), |
| 29 | "--", |
| 30 | str(t_path.absolute()), |
| 31 | ] |
| 32 | run_sh_command(verbose=False, full_cmd=cmd) |
| 33 | |
| 34 | |
| 35 | def json_to_fbbin(flatc: Path, fbs: Path, j_path: Path, o_path: Optional[Path] = None): |
| 36 | """Convert JSON flatbuffer to binary. |
| 37 | |
| 38 | flatc: the Path to the flatc compiler program |
| 39 | fbs: the Path to the fbs (flatbuffer schema) file |
| 40 | j_path: the Path to the JSON flatbuffer file |
| 41 | o_path: the output Path where JSON file will be put, if None, it is same as j_path |
| 42 | """ |
| 43 | if o_path is None: |
| 44 | o_path = j_path.parent |
| 45 | cmd = [ |
| 46 | str(flatc.absolute()), |
| 47 | "-o", |
| 48 | str(o_path.absolute()), |
| 49 | "--binary", |
| 50 | str(fbs.absolute()), |
| 51 | str(j_path.absolute()), |
| 52 | ] |
| 53 | run_sh_command(verbose=False, full_cmd=cmd) |
| 54 | |
| 55 | |
| 56 | # ------------------------------------------------------------------------------ |
| 57 | |
| 58 | |
| 59 | def main(argv=None): |
| 60 | """Load and convert supplied file based on file suffix.""" |
| 61 | import argparse |
| 62 | |
| 63 | parser = argparse.ArgumentParser() |
| 64 | parser.add_argument( |
| 65 | "--flatc", |
| 66 | type=Path, |
Jeremy Johnson | 5c1364c | 2022-01-13 15:04:21 +0000 | [diff] [blame] | 67 | default=( |
| 68 | "reference_model/build/thirdparty/serialization_lib/" |
| 69 | "third_party/flatbuffers/flatc" |
| 70 | ), |
Jeremy Johnson | be1a940 | 2021-12-15 17:14:56 +0000 | [diff] [blame] | 71 | help="the path to the flatc compiler program", |
| 72 | ) |
| 73 | parser.add_argument( |
| 74 | "--fbs", |
| 75 | type=Path, |
| 76 | default="conformance_tests/third_party/serialization_lib/schema/tosa.fbs", |
| 77 | help="the path to the flatbuffer schema", |
| 78 | ) |
| 79 | parser.add_argument("path", type=Path, help="the path to the file to convert") |
| 80 | args = parser.parse_args(argv) |
| 81 | path = args.path |
| 82 | |
| 83 | if not path.is_file(): |
| 84 | print(f"Invalid file to convert - {path}") |
| 85 | return 2 |
| 86 | |
| 87 | if not args.flatc.is_file(): |
| 88 | print(f"Invalid flatc compiler - {args.flatc}") |
| 89 | return 2 |
| 90 | |
| 91 | if not args.fbs.is_file(): |
| 92 | print(f"Invalid flatbuffer schema - {args.fbs}") |
| 93 | return 2 |
| 94 | |
| 95 | try: |
| 96 | if path.suffix == ".json": |
| 97 | json_to_fbbin(args.flatc, args.fbs, path) |
| 98 | else: |
| 99 | # Have to assume this is a binary flatbuffer file as could have any suffix |
| 100 | fbbin_to_json(args.flatc, args.fbs, path) |
| 101 | except RunShCommandError as e: |
| 102 | print(e) |
| 103 | return 1 |
| 104 | |
| 105 | return 0 |
| 106 | |
| 107 | |
| 108 | if __name__ == "__main__": |
| 109 | exit(main()) |