mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-17 21:43:45 +02:00
Add support for setting up wifi credentials with switchBoardType tool, rework the diff logic a bit
This commit is contained in:
@@ -17,6 +17,9 @@ parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-b", "--board", help="Board to switch to", choices=supported_boards)
|
||||
parser.add_argument("--dry-run", help="Dry run, won't modify files", action="store_true", required=False)
|
||||
parser.add_argument("--diff", help="Show the difference between base config and selected board", action="store_true", required=False)
|
||||
parser.add_argument("--ssid", help="Set the SSID for the selected board", required=False, type=str, default="")
|
||||
parser.add_argument("--password", help="Set the password For the provided network", required=False, type=str, default="")
|
||||
parser.add_argument("--clear-wifi", help="Should we clear the wifi details", action="store_true", required=False)
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
@@ -48,39 +51,62 @@ def parse_config(config_file) -> dict:
|
||||
return config
|
||||
|
||||
|
||||
def compute_diff(parsed_base_config: dict, parsed_board_config: dict) -> dict:
|
||||
diff = {}
|
||||
for key in parsed_board_config:
|
||||
if key not in parsed_base_config:
|
||||
if parsed_board_config[key] != "":
|
||||
diff[key] = f"{OKGREEN}+{ENDC} {parsed_board_config[key]}"
|
||||
def handle_wifi_config(_new_config: dict, _main_config: dict) -> dict:
|
||||
if args.ssid:
|
||||
_new_config["CONFIG_WIFI_SSID"] = f"\"{args.ssid}\""
|
||||
_new_config["CONFIG_WIFI_PASSWORD"] = f"\"{args.password}\""
|
||||
else:
|
||||
_new_config["CONFIG_WIFI_SSID"] = _main_config["CONFIG_WIFI_SSID"]
|
||||
_new_config["CONFIG_WIFI_PASSWORD"] = _main_config["CONFIG_WIFI_PASSWORD"]
|
||||
|
||||
if args.clear_wifi:
|
||||
_new_config["CONFIG_WIFI_SSID"] = "\"\""
|
||||
_new_config["CONFIG_WIFI_PASSWORD"] = "\"\""
|
||||
return _new_config
|
||||
|
||||
|
||||
def compute_diff(_parsed_base_config: dict, _parsed_board_config: dict) -> dict:
|
||||
_diff = {}
|
||||
for _key in _parsed_board_config:
|
||||
if _key not in _parsed_base_config:
|
||||
if _parsed_board_config[_key] != "":
|
||||
_diff[_key] = f"{OKGREEN}+{ENDC} {_parsed_board_config[_key]}"
|
||||
else:
|
||||
if parsed_board_config[key] != parsed_base_config[key]:
|
||||
diff[key] = f"{OKGREEN}{parsed_base_config[key]}{ENDC} -> {OKBLUE}{parsed_board_config[key]}{ENDC}"
|
||||
return diff
|
||||
if _parsed_board_config[_key] != _parsed_base_config[_key]:
|
||||
_diff[_key] = f"{OKGREEN}{_parsed_base_config[_key]}{ENDC} -> {OKBLUE}{_parsed_board_config[_key]}{ENDC}"
|
||||
return _diff
|
||||
|
||||
|
||||
print(f"{OKGREEN}Switching configuration to board:{ENDC} {OKBLUE}{args.board}{ENDC}")
|
||||
print(f"{OKGREEN}Using defaults from :{ENDC} {get_base_config_path()}", )
|
||||
print(f"{OKGREEN}Using board config from :{ENDC} {get_board_config_path()}")
|
||||
|
||||
main_config = open(get_main_config_path(), "a+")
|
||||
main_config = open(get_main_config_path(), "r+")
|
||||
parsed_main_config = parse_config(main_config)
|
||||
main_config.close()
|
||||
|
||||
base_config = open(get_base_config_path(), "r")
|
||||
board_config = open(get_board_config_path(), "r")
|
||||
|
||||
parsed_main_config = parse_config(main_config)
|
||||
parsed_base_config = parse_config(base_config)
|
||||
parsed_board_config = parse_config(board_config)
|
||||
|
||||
base_config.close()
|
||||
board_config.close()
|
||||
|
||||
new_board_config = {**parsed_base_config, **parsed_board_config}
|
||||
new_board_config = handle_wifi_config(new_board_config, parsed_main_config)
|
||||
|
||||
if args.diff:
|
||||
diff = compute_diff(parsed_main_config, parsed_board_config)
|
||||
|
||||
print("---"*5, f"{WARNING}DIFF{ENDC}", "---"*5)
|
||||
diff = compute_diff(parsed_main_config, new_board_config)
|
||||
if not diff:
|
||||
print(f"{HEADER_COLOR}[DIFF]{ENDC} Nothing has changed between the base config and {OKBLUE}{args.board}{ENDC} config")
|
||||
else:
|
||||
print(f"{HEADER_COLOR}[DIFF]{ENDC} The following keys have changed between the base config and {OKBLUE}{args.board}{ENDC} config:")
|
||||
for key in diff:
|
||||
print(f"{HEADER_COLOR}[DIFF]{ENDC} {key} : {diff[key]}")
|
||||
print("---"*14)
|
||||
|
||||
if not args.dry_run:
|
||||
# the main idea is to always replace the main config with the base config
|
||||
@@ -88,20 +114,14 @@ if not args.dry_run:
|
||||
# This way we can have known working defaults safe from accidental modifications by espidf
|
||||
# with know working per-board config
|
||||
# and a still modifiable sdkconfig for espidf
|
||||
main_config.truncate(0)
|
||||
for key in parsed_board_config:
|
||||
parsed_base_config[key] = parsed_board_config[key]
|
||||
|
||||
print(f"{WARNING}Writing changes to main config file{ENDC}")
|
||||
for key, value in parsed_base_config.items():
|
||||
if value:
|
||||
main_config.write(f"{key}={value}\n")
|
||||
else:
|
||||
main_config.write(f"{key}\n")
|
||||
with open(get_main_config_path(), "w") as main_config:
|
||||
for key, value in new_board_config.items():
|
||||
if value:
|
||||
main_config.write(f"{key}={value}\n")
|
||||
else:
|
||||
main_config.write(f"{key}\n")
|
||||
else:
|
||||
print(f"{WARNING}[DRY-RUN]{ENDC} Skipping writing to files")
|
||||
|
||||
main_config.close()
|
||||
base_config.close()
|
||||
board_config.close()
|
||||
print(f"{OKGREEN}Done. ESP-IDF is setup to build for:{ENDC} {OKBLUE}{args.board}{ENDC}")
|
||||
|
||||
Reference in New Issue
Block a user