Skip to content

Commit 28ac456

Browse files
committed
refactor(ci): common configuration file for update scripts
avoid multiple json files. Signed-off-by: Frederic Pillon <[email protected]>
1 parent eeb4237 commit 28ac456

File tree

4 files changed

+48
-59
lines changed

4 files changed

+48
-59
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ boards.local.txt
55
platform.local.txt
66
path_config.json
77
update_config.json
8-
variant_config.json
98

109
# Backup
1110
*.bak

CI/update/stm32cube.py

+9-19
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
script_path = Path(__file__).parent.resolve()
1616
sys.path.append(str(script_path.parent))
1717
from utils import copyFile, copyFolder, createFolder, deleteFolder, genSTM32List
18-
from utils import execute_cmd, getRepoBranchName
18+
from utils import defaultConfig, execute_cmd, getRepoBranchName
1919

2020
if sys.platform.startswith("win32"):
2121
from colorama import init
2222

2323
init(autoreset=True)
2424

2525
home = Path.home()
26-
path_config_filename = "update_config.json"
2726

2827
# GitHub
2928
gh_st = "https://github.com/STMicroelectronics/"
@@ -85,19 +84,6 @@
8584
out_separator = "-" * 70
8685

8786

88-
def create_config(config_file_path):
89-
global repo_local_path
90-
91-
# Create a Json file for a better path management
92-
print(f"'{config_file_path}' file created. Please check the configuration.")
93-
path_config_file = open(config_file_path, "w")
94-
path_config_file.write(
95-
json.dumps({"REPO_LOCAL_PATH": str(repo_local_path)}, indent=2)
96-
)
97-
path_config_file.close()
98-
exit(1)
99-
100-
10187
def checkConfig():
10288
global repo_local_path
10389
global hal_dest_path
@@ -107,14 +93,18 @@ def checkConfig():
10793
global md_CMSIS_path
10894
global stm32_def
10995

110-
config_file_path = script_path / path_config_filename
96+
config_file_path = script_path / "update_config.json"
11197
if config_file_path.is_file():
11298
try:
11399
config_file = open(config_file_path, "r")
114100
path_config = json.load(config_file)
115-
# Common path
116-
repo_local_path = Path(path_config["REPO_LOCAL_PATH"])
117101
config_file.close()
102+
# Common path
103+
if "REPO_LOCAL_PATH" not in path_config:
104+
path_config["REPO_LOCAL_PATH"] = str(repo_local_path)
105+
defaultConfig(config_file_path, path_config)
106+
else:
107+
repo_local_path = Path(path_config["REPO_LOCAL_PATH"])
118108
hal_dest_path = repo_local_path / repo_core_name / hal_dest_path
119109
md_HAL_path = hal_dest_path / md_HAL_path
120110
cmsis_dest_path = repo_local_path / repo_core_name / cmsis_dest_path
@@ -130,7 +120,7 @@ def checkConfig():
130120
except IOError:
131121
print(f"Failed to open {config_file}!")
132122
else:
133-
create_config(config_file_path)
123+
defaultConfig(config_file_path, {"REPO_LOCAL_PATH": str(repo_local_path)})
134124
createFolder(repo_local_path)
135125

136126

CI/update/stm32variant.py

+29-39
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
script_path = Path(__file__).parent.resolve()
1616
sys.path.append(str(script_path.parent))
17-
from utils import deleteFolder, execute_cmd, getRepoBranchName
17+
from utils import defaultConfig, deleteFolder, execute_cmd, getRepoBranchName
1818

1919
mcu_list = [] # 'name'
2020
io_list = [] # 'PIN','name'
@@ -1274,9 +1274,9 @@ def print_pinamevar():
12741274

12751275
# Variant files generation
12761276
def spi_pins_variant():
1277-
ss_pin = (
1278-
ss1_pin
1279-
) = ss2_pin = ss3_pin = mosi_pin = miso_pin = sck_pin = "PNUM_NOT_DEFINED"
1277+
ss_pin = ss1_pin = ss2_pin = ss3_pin = mosi_pin = miso_pin = sck_pin = (
1278+
"PNUM_NOT_DEFINED"
1279+
)
12801280

12811281
# Iterate to find match instance if any
12821282
for mosi in spimosi_list:
@@ -2385,51 +2385,41 @@ def default_cubemxdir():
23852385

23862386

23872387
# Config management
2388-
def create_config():
2389-
# Create a Json file for a better path management
2390-
try:
2391-
print(f"Please set your configuration in '{config_filename}' file")
2392-
config_file = open(config_filename, "w", newline="\n")
2393-
config_file.write(
2394-
json.dumps(
2395-
{
2396-
"CUBEMX_DIRECTORY": str(cubemxdir),
2397-
"REPO_LOCAL_PATH": str(repo_local_path),
2398-
},
2399-
indent=2,
2400-
)
2401-
)
2402-
config_file.close()
2403-
except IOError:
2404-
print(f"Failed to open {config_filename}")
2405-
exit(1)
2406-
2407-
2408-
def check_config():
2388+
def checkConfig():
24092389
global cubemxdir
24102390
global repo_local_path
24112391
global repo_path
24122392
default_cubemxdir()
24132393
if config_filename.is_file():
24142394
try:
24152395
config_file = open(config_filename, "r")
2416-
config = json.load(config_file)
2396+
path_config = json.load(config_file)
24172397
config_file.close()
24182398

2419-
if "REPO_LOCAL_PATH" in config:
2420-
conf = config["REPO_LOCAL_PATH"]
2421-
if conf:
2422-
if conf != "":
2423-
repo_local_path = Path(conf)
2424-
repo_path = repo_local_path / repo_name
2425-
if "CUBEMX_DIRECTORY" in config:
2426-
conf = config["CUBEMX_DIRECTORY"]
2427-
if conf:
2428-
cubemxdir = Path(conf)
2399+
if "REPO_LOCAL_PATH" not in path_config:
2400+
path_config["REPO_LOCAL_PATH"] = str(repo_local_path)
2401+
defaultConfig(config_filename, path_config)
2402+
else:
2403+
conf = path_config["REPO_LOCAL_PATH"]
2404+
if conf != "":
2405+
repo_local_path = Path(conf)
2406+
repo_path = repo_local_path / repo_name
2407+
2408+
if "CUBEMX_DIRECTORY" not in path_config:
2409+
path_config["CUBEMX_DIRECTORY"] = str(cubemxdir)
2410+
defaultConfig(config_filename, path_config)
2411+
else:
2412+
cubemxdir = Path(path_config["CUBEMX_DIRECTORY"])
24292413
except IOError:
24302414
print(f"Failed to open {config_filename}")
24312415
else:
2432-
create_config()
2416+
defaultConfig(
2417+
config_filename,
2418+
{
2419+
"CUBEMX_DIRECTORY": str(cubemxdir),
2420+
"REPO_LOCAL_PATH": str(repo_local_path),
2421+
},
2422+
)
24332423

24342424

24352425
def manage_repo():
@@ -2487,7 +2477,7 @@ def manage_repo():
24872477
refname_filter = ["STM32MP13", "STM32H7R", "STM32H7S"]
24882478
periph_c_filename = "PeripheralPins.c"
24892479
pinvar_h_filename = "PinNamesVar.h"
2490-
config_filename = script_path / "variant_config.json"
2480+
config_filename = script_path / "update_config.json"
24912481
variant_h_filename = "variant_generic.h"
24922482
variant_cpp_filename = "variant_generic.cpp"
24932483
boards_entry_filename = "boards_entry.txt"
@@ -2499,7 +2489,7 @@ def manage_repo():
24992489
repo_path = repo_local_path / repo_name
25002490
db_release = "Unknown"
25012491

2502-
check_config()
2492+
checkConfig()
25032493

25042494
# By default, generate for all mcu xml files description
25052495
parser = argparse.ArgumentParser(

CI/utils/common_ext.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import json
12
import re
23
import subprocess
34
import shutil
45
import sys
56

67

8+
# Add default key/value pair to config file
9+
def defaultConfig(config_file_path, data):
10+
print(f"Please check the default configuration '{config_file_path}'.")
11+
config_file = open(config_file_path, "w")
12+
config_file.write(json.dumps(data, indent=2))
13+
config_file.close()
14+
exit(1)
15+
16+
717
# Create a folder if not exists
818
def createFolder(path):
919
try:

0 commit comments

Comments
 (0)