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