1import os
2import sys
3import shutil
4import yaml
5
6from SCons.Script import *
7
8# SCons AttachConfig Command Function
9def GenAttachConfigProject(program = None):
10    Rtt_Root = os.getcwd()
11    config_file = os.path.join(os.getcwd(), 'rtt_root', Rtt_Root, '.config')
12    config_bacakup = config_file+'.origin'
13    rtconfig_file = os.path.join(os.getcwd(), 'rtt_root', Rtt_Root, 'rtconfig.h')
14    rtconfig__bacakup = rtconfig_file+'.origin'
15    if GetOption('attach') == '?':
16        attachconfig=[]
17        GetAttachConfig("get",attachconfig,0)
18        print("\033[32m✅ AttachConfig has: \033[0m")
19        prefix=attachconfig[0]
20        for line in attachconfig:
21            temp_prefix=line.split(".", 1)
22            if prefix!=temp_prefix[0]:
23                print("\033[42m \033[30m------"+temp_prefix[0]+"------\033[0m")
24                prefix=temp_prefix[0]
25            print(line)
26
27
28    elif GetOption('attach') == 'default':
29        if os.path.exists(config_bacakup):
30            shutil.copyfile(config_bacakup, config_file)
31            os.remove(config_bacakup)
32        if os.path.exists(rtconfig__bacakup):
33            shutil.copyfile(rtconfig__bacakup, rtconfig_file)
34            os.remove(rtconfig__bacakup)
35        print("\033[32m✅ Default .config and rtconfig.h recovery success!\033[0m")
36    else:
37        attachconfig=GetOption('attach')
38        attachconfig_result=[]
39        GetAttachConfig("search",attachconfig,attachconfig_result)
40        if attachconfig_result==[]:
41            print("❌\033[31m Without this AttachConfig:"+attachconfig+"\033[0m")
42            return
43        if os.path.exists(config_bacakup)==False:
44            shutil.copyfile(config_file, config_bacakup)
45        if os.path.exists(rtconfig__bacakup)==False:
46            shutil.copyfile(rtconfig_file, rtconfig__bacakup)
47        with open(config_file, 'a') as destination:
48            for line in attachconfig_result:
49                destination.write(line + '\n')
50        from env_utility import defconfig
51        defconfig(Rtt_Root)
52        print("\033[32m✅ AttachConfig add success!\033[0m")
53
54def GetAttachConfig(action,attachconfig,attachconfig_result):
55    rtt_root = os.getcwd()
56    yml_files_content = []
57    directory = os.path.join(rtt_root, 'rtt_root', rtt_root, '.ci/attachconfig')
58    if os.path.exists(directory):
59            for root, dirs, files in os.walk(directory):
60                for filename in files:
61                    if filename.endswith('attachconfig.yml'):
62                        file_path = os.path.join(root, filename)
63                        if os.path.exists(file_path):
64                            try:
65                                with open(file_path, 'r') as file:
66                                    content = yaml.safe_load(file)
67                                    if content is None:
68                                        continue
69                                    yml_files_content.append(content)
70                            except yaml.YAMLError as e:
71                                print(f"::error::Error parsing YAML file: {e}")
72                                continue
73                            except Exception as e:
74                                print(f"::error::Error reading file: {e}")
75                                continue
76    for projects in yml_files_content:
77            for name, details in projects.items():
78                if details.get("kconfig") is None:
79                    continue
80                if(projects.get(name) is not None):
81                    if action == "get":
82                        attachconfig.append(name)
83                    if action == "search" and name == attachconfig:
84                        from ci.bsp_buildings import get_details_and_dependencies
85                        detail_list=get_details_and_dependencies([name],projects)
86                        for line in detail_list:
87                            attachconfig_result.append(line)