1#!/usr/bin/env python3 2# coding=utf-8 3 4''' 5* Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 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* Description: .config parser, bases on kconfig format. 19''' 20 21import os 22import sys 23from inspect import getsourcefile 24 25__all__ = ["usr_cfg_main", "sys_cfg_main"] 26 27class UsrCfgParserError(Exception): 28 """ 29 Parser exception 30 """ 31 pass 32 33class Translator: 34 def __init__(self): 35 ''' 36 Insert MARCOS if there are different. 37 ''' 38 self.marco_table = { 39 } 40 pass 41 42 def translate(self, key): 43 if key in self.marco_table.keys(): 44 return self.marco_table[key] 45 return key 46 47class UsrCfgParser: 48 """ 49 Read menuconfig settings. 50 """ 51 def __init__(self): 52 #print('execute with exe...') if getattr(sys, 'frozen', False) else print('execute with python...') 53 self.config_file = self.get_default_config_file() 54 #print('config file:',self.config_file) 55 if os.path.exists(self.config_file) is False: 56 raise UsrCfgParserError("Config file not found! Please execute 'python usr_config.py' first!") 57 self.translate = Translator() 58 59 def local_main(self): 60 pass 61 62 def get_default_config_file(self): 63 if getattr(sys, 'frozen', False): 64 dir_path = os.path.dirname(os.path.dirname(os.path.realpath(getsourcefile(self.local_main)))) 65 return os.path.join(dir_path, 'config', 'usr_config.mk') 66 else: 67 return os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'config', 'usr_config.mk') 68 69 def do_parse(self): 70 cfg_options = {} 71 if self.config_file is None: 72 raise UsrCfgParserError("Config file not found! Please execute \'python usr_config.py\' first!") 73 with open(self.config_file, 'r') as cfg: 74 for option in cfg.readlines(): 75 option = option.strip() 76 if self.cfg_is_valid(option) is True: 77 marco, value = self.parse_option(option) 78 marco = self.translate.translate(marco) 79 cfg_options[marco.strip()] = value.strip() 80 return cfg_options 81 82 def cfg_is_valid(self, option): 83 if option is None: 84 return False 85 if option == '': 86 return False 87 if option.startswith('#') is True: 88 return False 89 if option.find('HiSilicon menuconfig tool') > 0: 90 return False 91 if option.find('#', 1) >= 0: 92 raise UsrCfgParserError("Unknown format! Please execute \'python usr_config.py\' first!") 93 return True 94 95 def parse_option(self, option): 96 cfg = option.split('=') 97 if len(cfg) == 2: #Just one setting 98 return cfg[0], cfg[1] 99 else: 100 raise UsrCfgParserError("Unknown format! Please execute \'python usr_config.py\' first!") 101 102def usr_cfg_main(): 103 return UsrCfgParser().do_parse() 104 105class SysCfgParser(UsrCfgParser): 106 """ 107 Read system settings 108 """ 109 def get_default_config_file(self): 110 if getattr(sys, 'frozen', False): 111 dir_path = os.path.dirname(os.path.dirname(os.path.realpath(getsourcefile(self.local_main)))) 112 return os.path.join(dir_path, 'config', 'sdk.mk') 113 else: 114 return os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'config', 'sdk.mk') 115 116def sys_cfg_main(): 117 return SysCfgParser().do_parse() 118 119