Torsten Rasmussen | 7e9d1bd | 2019-02-05 10:36:22 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2019, Nordic Semiconductor ASA |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
| 7 | '''Tool for a simple parsing of YAML files and return a ;-list of <key>=<value> |
| 8 | pairs to use within a CMake build file. |
| 9 | ''' |
| 10 | |
| 11 | import argparse |
| 12 | import sys |
| 13 | import yaml |
| 14 | import pykwalify.core |
| 15 | |
| 16 | |
| 17 | METADATA_SCHEMA = '''\ |
| 18 | ## A pykwalify schema for basic validation of the structure of a |
| 19 | ## metadata YAML file. |
| 20 | ## |
| 21 | # The zephyr/module.yml file is a simple list of key value pairs to be used by |
| 22 | # the build system. |
| 23 | type: map |
| 24 | mapping: |
| 25 | build: |
| 26 | required: true |
| 27 | type: map |
| 28 | mapping: |
| 29 | cmake: |
| 30 | required: false |
| 31 | type: str |
| 32 | kconfig: |
| 33 | required: false |
| 34 | type: str |
| 35 | ''' |
| 36 | |
| 37 | |
| 38 | def main(): |
| 39 | parser = argparse.ArgumentParser(description=''' |
| 40 | Converts YAML to a CMake list''') |
| 41 | |
| 42 | parser.add_argument('-i', '--input', required=True, |
| 43 | help='YAML file with data') |
| 44 | parser.add_argument('-o', '--output', required=True, |
| 45 | help='File to write with CMake data') |
| 46 | parser.add_argument('-s', '--section', required=True, |
| 47 | help='Section in YAML file to parse') |
| 48 | args = parser.parse_args() |
| 49 | |
| 50 | with open(args.input, 'r') as f: |
| 51 | meta = yaml.safe_load(f.read()) |
| 52 | |
| 53 | pykwalify.core.Core(source_data=meta, |
| 54 | schema_data=yaml.safe_load(METADATA_SCHEMA)).validate() |
| 55 | |
| 56 | val_str = '' |
| 57 | |
| 58 | section = meta.get(args.section) |
| 59 | if section is not None: |
| 60 | for key in section: |
| 61 | val_str += '{}={}\n'.format(key, section[key]) |
| 62 | |
| 63 | with open(args.output, 'w') as f: |
| 64 | f.write(val_str) |
| 65 | |
| 66 | |
| 67 | if __name__ == "__main__": |
| 68 | main() |