blob: 7da027ec8ee494f5e7b3b58a0835d35ca1d4c82f [file] [log] [blame]
Torsten Rasmussen7e9d1bd2019-02-05 10:36:22 +01001#!/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>
8pairs to use within a CMake build file.
9'''
10
11import argparse
12import sys
13import yaml
14import pykwalify.core
15
16
17METADATA_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.
23type: map
24mapping:
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
38def 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
67if __name__ == "__main__":
68 main()