|
33 | 33 | import rtconfig |
34 | 34 | import platform |
35 | 35 | import logging |
| 36 | +import shutil |
| 37 | +import yaml |
36 | 38 |
|
37 | 39 | from SCons.Script import * |
38 | 40 | from utils import _make_path_relative |
|
44 | 46 | Rtt_Root = '' |
45 | 47 | Env = None |
46 | 48 |
|
| 49 | +# SCons AttachConfig Command Function |
| 50 | +def GenAttachConfigProject(program = None): |
| 51 | + Rtt_Root = os.getcwd() |
| 52 | + config_file = os.path.join(os.getcwd(), 'rtt_root', Rtt_Root, '.config') |
| 53 | + config_bacakup = config_file+'.origin' |
| 54 | + rtconfig_file = os.path.join(os.getcwd(), 'rtt_root', Rtt_Root, 'rtconfig.h') |
| 55 | + rtconfig__bacakup = rtconfig_file+'.origin' |
| 56 | + if GetOption('attach') == '?': |
| 57 | + attachconfig=[] |
| 58 | + GetAttachConfig("get",attachconfig,0) |
| 59 | + print("\033[32m✅ AttachConfig has: \033[0m"+','.join(attachconfig)) |
| 60 | + elif GetOption('attach') == 'default': |
| 61 | + shutil.copyfile(config_bacakup, config_file) |
| 62 | + shutil.copyfile(rtconfig__bacakup, rtconfig_file) |
| 63 | + if os.path.exists(config_bacakup): |
| 64 | + os.remove(config_bacakup) |
| 65 | + if os.path.exists(rtconfig__bacakup): |
| 66 | + os.remove(rtconfig__bacakup) |
| 67 | + print("\033[32m✅ Default .config and rtconfig.h recovery success!\033[0m") |
| 68 | + else: |
| 69 | + attachconfig=GetOption('attach') |
| 70 | + attachconfig_result=[] |
| 71 | + GetAttachConfig("search",attachconfig,attachconfig_result) |
| 72 | + if attachconfig_result==[]: |
| 73 | + print("❌\033[31m Without this AttachConfig:"+attachconfig+"\033[0m") |
| 74 | + return |
| 75 | + if os.path.exists(config_bacakup)==False: |
| 76 | + shutil.copyfile(config_file, config_bacakup) |
| 77 | + if os.path.exists(rtconfig__bacakup)==False: |
| 78 | + shutil.copyfile(rtconfig_file, rtconfig__bacakup) |
| 79 | + with open(config_file, 'a') as destination: |
| 80 | + for line in attachconfig_result: |
| 81 | + destination.write(line + '\n') |
| 82 | + from env_utility import defconfig |
| 83 | + defconfig(Rtt_Root) |
| 84 | + print("\033[32m✅ AttachConfig add success!\033[0m") |
| 85 | + |
| 86 | +def GetAttachConfig(action,attachconfig,attachconfig_result): |
| 87 | + rtt_root = os.getcwd() |
| 88 | + yml_files_content = [] |
| 89 | + directory = os.path.join(rtt_root, 'rtt_root', rtt_root, '.ci/attachconfig') |
| 90 | + if os.path.exists(directory): |
| 91 | + for root, dirs, files in os.walk(directory): |
| 92 | + for filename in files: |
| 93 | + if filename.endswith('attachconfig.yml'): |
| 94 | + file_path = os.path.join(root, filename) |
| 95 | + if os.path.exists(file_path): |
| 96 | + try: |
| 97 | + with open(file_path, 'r') as file: |
| 98 | + content = yaml.safe_load(file) |
| 99 | + if content is None: |
| 100 | + continue |
| 101 | + yml_files_content.append(content) |
| 102 | + except yaml.YAMLError as e: |
| 103 | + print(f"::error::Error parsing YAML file: {e}") |
| 104 | + continue |
| 105 | + except Exception as e: |
| 106 | + print(f"::error::Error reading file: {e}") |
| 107 | + continue |
| 108 | + for projects in yml_files_content: |
| 109 | + for name, details in projects.items(): |
| 110 | + if details.get("kconfig") is None: |
| 111 | + continue |
| 112 | + if(projects.get(name) is not None): |
| 113 | + if action == "get": |
| 114 | + attachconfig.append(name) |
| 115 | + if action == "search" and name == attachconfig: |
| 116 | + from ci.bsp_buildings import get_details_and_dependencies |
| 117 | + detail_list=get_details_and_dependencies([name],projects) |
| 118 | + for line in detail_list: |
| 119 | + attachconfig_result.append(line) |
| 120 | + |
47 | 121 | # SCons PreProcessor patch |
48 | 122 | def start_handling_includes(self, t=None): |
49 | 123 | """ |
@@ -336,6 +410,10 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [ |
336 | 410 | else: |
337 | 411 | print('--global-macros arguments are illegal!') |
338 | 412 |
|
| 413 | + if GetOption('attach'): |
| 414 | + GenAttachConfigProject() |
| 415 | + exit(0) |
| 416 | + |
339 | 417 | if GetOption('genconfig'): |
340 | 418 | from env_utility import genconfig |
341 | 419 | genconfig() |
|
0 commit comments