alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | #!env/bin/python3 |
| 2 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 3 | # SPDX-FileCopyrightText: Copyright 2021, 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | """ |
| 19 | Utility script to convert a given text file with labels (annotations for an |
| 20 | NN model output vector) into a vector list initialiser. The intention is for |
| 21 | this script to be called as part of the build framework to auto-generate the |
| 22 | cpp file with labels that can be used in the application without modification. |
| 23 | """ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 24 | from argparse import ArgumentParser |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 25 | from pathlib import Path |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 26 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 27 | from jinja2 import Environment, FileSystemLoader |
| 28 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 29 | from gen_utils import GenUtils |
| 30 | |
| 31 | # pylint: disable=duplicate-code |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 32 | parser = ArgumentParser() |
| 33 | |
| 34 | # Label file path |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 35 | parser.add_argument( |
| 36 | "--labels_file", |
| 37 | type=str, |
| 38 | help="Path to the label text file", |
| 39 | required=True |
| 40 | ) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 41 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 42 | # Output file to be generated |
| 43 | parser.add_argument( |
| 44 | "--source_folder_path", |
| 45 | type=str, |
| 46 | help="path to source folder to be generated.", |
| 47 | required=True |
| 48 | ) |
| 49 | |
| 50 | parser.add_argument( |
| 51 | "--header_folder_path", |
| 52 | type=str, |
| 53 | help="path to header folder to be generated.", |
| 54 | required=True |
| 55 | ) |
| 56 | |
| 57 | parser.add_argument( |
| 58 | "--output_file_name", |
| 59 | type=str, |
| 60 | help="Required output file name", |
| 61 | required=True |
| 62 | ) |
| 63 | |
| 64 | # Namespaces |
| 65 | parser.add_argument( |
| 66 | "--namespaces", |
| 67 | action='append', |
| 68 | default=[] |
| 69 | ) |
| 70 | |
| 71 | # License template |
| 72 | parser.add_argument( |
| 73 | "--license_template", |
| 74 | type=str, |
| 75 | help="Header template file", |
| 76 | default="header_template.txt" |
| 77 | ) |
| 78 | |
| 79 | parsed_args = parser.parse_args() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 80 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 81 | env = Environment(loader=FileSystemLoader(Path(__file__).parent / 'templates'), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 82 | trim_blocks=True, |
| 83 | lstrip_blocks=True) |
| 84 | |
| 85 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 86 | # pylint: enable=duplicate-code |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 87 | def main(args): |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 88 | """ |
| 89 | Generate labels .cpp |
| 90 | @param args: Parsed args |
| 91 | """ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | # Get the labels from text file |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 93 | with open(args.labels_file, "r", encoding="utf8") as f: |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 94 | labels = f.read().splitlines() |
| 95 | |
| 96 | # No labels? |
| 97 | if len(labels) == 0: |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 98 | raise ValueError(f"no labels found in {args.label_file}") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 99 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 100 | hdr = GenUtils.gen_header(env, args.license_template, Path(args.labels_file).name) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 101 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 102 | hpp_filename = Path(args.header_folder_path) / (args.output_file_name + ".hpp") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 103 | env.get_template('Labels.hpp.template').stream(common_template_header=hdr, |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 104 | filename=args.output_file_name.upper(), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 105 | namespaces=args.namespaces) \ |
| 106 | .dump(str(hpp_filename)) |
| 107 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 108 | cc_filename = Path(args.source_folder_path) / (args.output_file_name + ".cc") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 109 | env.get_template('Labels.cc.template').stream(common_template_header=hdr, |
| 110 | labels=labels, |
| 111 | labelsSize=len(labels), |
| 112 | namespaces=args.namespaces) \ |
| 113 | .dump(str(cc_filename)) |
| 114 | |
| 115 | |
| 116 | if __name__ == '__main__': |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 117 | main(parsed_args) |