From e17c92624685be3d8e9e2c5aff0bf79f0e152379 Mon Sep 17 00:00:00 2001 From: hydevcode Date: Wed, 22 Jan 2025 02:49:25 +0800 Subject: [PATCH] [scons] ci.attachconfig.yml is used in combination with scons --- tools/attachconfig.py | 78 +++++++++++++++++++++++++++++++++++++++++++ tools/building.py | 6 +++- tools/options.py | 7 ++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tools/attachconfig.py diff --git a/tools/attachconfig.py b/tools/attachconfig.py new file mode 100644 index 00000000000..7debefca383 --- /dev/null +++ b/tools/attachconfig.py @@ -0,0 +1,78 @@ +import os +import sys +import shutil +import yaml + +from SCons.Script import * + +# SCons AttachConfig Command Function +def GenAttachConfigProject(program = None): + Rtt_Root = os.getcwd() + config_file = os.path.join(os.getcwd(), 'rtt_root', Rtt_Root, '.config') + config_bacakup = config_file+'.origin' + rtconfig_file = os.path.join(os.getcwd(), 'rtt_root', Rtt_Root, 'rtconfig.h') + rtconfig__bacakup = rtconfig_file+'.origin' + if GetOption('attach') == '?': + attachconfig=[] + GetAttachConfig("get",attachconfig,0) + print("\033[32m✅ AttachConfig has: \033[0m"+','.join(attachconfig)) + elif GetOption('attach') == 'default': + if os.path.exists(config_bacakup): + shutil.copyfile(config_bacakup, config_file) + os.remove(config_bacakup) + if os.path.exists(rtconfig__bacakup): + shutil.copyfile(rtconfig__bacakup, rtconfig_file) + os.remove(rtconfig__bacakup) + print("\033[32m✅ Default .config and rtconfig.h recovery success!\033[0m") + else: + attachconfig=GetOption('attach') + attachconfig_result=[] + GetAttachConfig("search",attachconfig,attachconfig_result) + if attachconfig_result==[]: + print("❌\033[31m Without this AttachConfig:"+attachconfig+"\033[0m") + return + if os.path.exists(config_bacakup)==False: + shutil.copyfile(config_file, config_bacakup) + if os.path.exists(rtconfig__bacakup)==False: + shutil.copyfile(rtconfig_file, rtconfig__bacakup) + with open(config_file, 'a') as destination: + for line in attachconfig_result: + destination.write(line + '\n') + from env_utility import defconfig + defconfig(Rtt_Root) + print("\033[32m✅ AttachConfig add success!\033[0m") + +def GetAttachConfig(action,attachconfig,attachconfig_result): + rtt_root = os.getcwd() + yml_files_content = [] + directory = os.path.join(rtt_root, 'rtt_root', rtt_root, '.ci/attachconfig') + if os.path.exists(directory): + for root, dirs, files in os.walk(directory): + for filename in files: + if filename.endswith('attachconfig.yml'): + file_path = os.path.join(root, filename) + if os.path.exists(file_path): + try: + with open(file_path, 'r') as file: + content = yaml.safe_load(file) + if content is None: + continue + yml_files_content.append(content) + except yaml.YAMLError as e: + print(f"::error::Error parsing YAML file: {e}") + continue + except Exception as e: + print(f"::error::Error reading file: {e}") + continue + for projects in yml_files_content: + for name, details in projects.items(): + if details.get("kconfig") is None: + continue + if(projects.get(name) is not None): + if action == "get": + attachconfig.append(name) + if action == "search" and name == attachconfig: + from ci.bsp_buildings import get_details_and_dependencies + detail_list=get_details_and_dependencies([name],projects) + for line in detail_list: + attachconfig_result.append(line) \ No newline at end of file diff --git a/tools/building.py b/tools/building.py index c64c393ba00..6054490cf65 100644 --- a/tools/building.py +++ b/tools/building.py @@ -33,7 +33,6 @@ import rtconfig import platform import logging - from SCons.Script import * from utils import _make_path_relative from mkdist import do_copy_file @@ -336,6 +335,11 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [ else: print('--global-macros arguments are illegal!') + if GetOption('attach'): + from attachconfig import GenAttachConfigProject + GenAttachConfigProject() + exit(0) + if GetOption('genconfig'): from env_utility import genconfig genconfig() diff --git a/tools/options.py b/tools/options.py index b77b6756122..2e92de8bd3e 100644 --- a/tools/options.py +++ b/tools/options.py @@ -141,3 +141,10 @@ def AddOptions(): action = 'store_true', default = False, help = 'make compile_commands.json') + AddOption('--attach', + dest = 'attach', + type = 'string', + help = 'View attachconfig or add attach to.config.'+\ + 'e.g. scons --attach=? View all attachconfig for the current bsp.'+\ + ' or scons --attach=component.cherryusb_cdc Set option component.cherryusb_cdc inside attachconfig to.config.'+\ + ' or scons --attach=default Restore.config and rtconfig to before attch was set.') \ No newline at end of file