attachconfig.py 3.9 KB

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