Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers generate_mbedcli_files.py Source File

generate_mbedcli_files.py

00001 #
00002 # DAPLink Interface Firmware
00003 # Copyright (c) 2009-2018, ARM Limited, All Rights Reserved
00004 # SPDX-License-Identifier: Apache-2.0
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License"); you may
00007 # not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 # http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00014 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 #
00018 
00019 import argparse
00020 import yaml
00021 import sys
00022 import logging
00023 from generate_custom_cli_files import generate_custom_targets
00024 from generate_custom_cli_files import generate_custom_profile
00025 from generate_mbedignore import generate_mbedignore
00026 
00027 logging.basicConfig(format='Line: %(lineno)d %(message)s')
00028 logger = logging.getLogger('yaml gen')
00029 logger.setLevel(logging.DEBUG)
00030 
00031 def parse_yaml_dicts(input_dicts, output_dicts):
00032     for key in input_dicts:
00033         if key in output_dicts: #merge
00034             if type(output_dicts[key]) is dict:
00035                 parse_yaml_dicts(input_dicts[key], output_dicts[key])
00036             elif type(output_dicts[key]) is list:
00037                 output_dicts[key].extend(input_dicts[key])
00038             else:
00039                 logger.error('Wrong type', typeof(output_dicts[key]))
00040         else:
00041             output_dicts[key] = input_dicts[key]
00042 
00043 def parse_yaml_files(list_of_list, data_dict):
00044     if type(list_of_list) is list:
00045         for entry in list_of_list:
00046             if type(entry) is list:
00047                 parse_yaml_files(entry, data_dict)
00048             elif type(entry) is str and entry.endswith('.yaml'):
00049                 try:
00050                     with open(entry, 'r') as yaml_file:
00051                         yaml_entries = yaml.load(yaml_file)
00052                         parse_yaml_dicts(yaml_entries, data_dict)
00053 
00054                 except yaml.YAMLError as ex:
00055                     logger.error("Found yaml parse error", ex)
00056             else:
00057                 logger.error('Wrong type', typeof(entry))
00058     else:
00059         logger.error('Wrong type', typeof(list_of_list))
00060 
00061 def generate_mbedcli_files(projects_yaml, target_project):
00062     with open(projects_yaml, 'r') as top_yaml:
00063         try:
00064             build_data = {}
00065             topdict = yaml.load(top_yaml)
00066             for dict_key in topdict:
00067                 if dict_key == 'projects':
00068                     for project in topdict[dict_key]:
00069                         if project == target_project:
00070                             parse_yaml_files(topdict[dict_key][project], build_data)
00071                             generate_custom_targets(project, build_data)
00072                             generate_custom_profile(build_data)
00073                             generate_mbedignore(build_data)
00074                             break
00075                     break
00076         except yaml.YAMLError as ex:
00077             logger.error("Found yaml parse error", ex)
00078 
00079 if __name__ == "__main__":
00080     parser = argparse.ArgumentParser(description='projects.yaml file parser')
00081     parser.add_argument("--projects_yaml", type=str, default='projects.yaml', help="top projects.yaml file")
00082     parser.add_argument("--target_project", type=str, help="target project to be compiled")
00083     args = parser.parse_args()
00084     
00085     if not args.target_project or args.target_project is '':
00086         sys.exit('Need a target project to compile DAPLink!')
00087 
00088     generate_mbedcli_files(args.projects_yaml, args.target_project)