Skip to content

Commit 44c7dc1

Browse files
authored
Update cmsis_mcu_descr.py - rework for remove global var
1 parent cf07b76 commit 44c7dc1

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

tools/python/mbed_tools/cli/cmsis_mcu_descr.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@
3333

3434
# Calculate path to Mbed OS JSON files
3535
THIS_SCRIPT_DIR = pathlib.Path(os.path.dirname(__file__))
36+
PROJECT_ROOT = THIS_SCRIPT_DIR.parent.parent.parent.parent.parent
3637
MBED_OS_DIR = THIS_SCRIPT_DIR.parent.parent.parent.parent
3738
TARGETS_JSON5_PATH = MBED_OS_DIR / "targets" / "targets.json5"
3839
CMSIS_MCU_DESCRIPTIONS_JSON_PATH = MBED_OS_DIR / "targets" / "cmsis_mcu_descriptions.json5"
3940

40-
PROJECT_ROOT = THIS_SCRIPT_DIR.parent.parent.parent.parent.parent
41-
CUSTOM_TARGETS_JSON_PATH = None
42-
4341
# Top-level command
4442
@click.group(
4543
name="cmsis-mcu-descr",
@@ -70,6 +68,7 @@ def open_cmsis_cache(*, must_exist: bool = True) -> cmsis_pack_manager.Cache:
7068

7169
return cmsis_cache
7270

71+
7372
def find_json_files(root_dir, exclude_dirs=[], file_pattern=r".*\.(json|json5)"):
7473
"""
7574
Recursively searches for files matching the specified pattern in a given directory, excluding specified directories.
@@ -107,19 +106,19 @@ def get_mcu_names_used_by_targets_json5() -> Set[str]:
107106
file_pattern = r"custom_targets\.(json|json5)"
108107
custom_targets_file = find_json_files(PROJECT_ROOT, exclude_dirs, file_pattern)
109108

109+
custom_targets_json_path = {}
110110
for file in custom_targets_file:
111111
if os.path.exists(file):
112-
global CUSTOM_TARGETS_JSON_PATH
113-
CUSTOM_TARGETS_JSON_PATH = file
114-
print(f"File exist {CUSTOM_TARGETS_JSON_PATH}")
112+
custom_targets_json_path = file
113+
LOGGER.info(f"Custom_targets file detected - {custom_targets_json_path}")
115114

116115

117116
used_mcu_names = set()
118117
LOGGER.info("Scanning targets.json5 for used MCU names...")
119118
json_contents = decode_json_file(TARGETS_JSON5_PATH)
120119
if custom_targets_file:
121120
LOGGER.info("Scanning custom_targets.json/json5. for used MCU names...")
122-
json_contents.update(decode_json_file(CUSTOM_TARGETS_JSON_PATH))
121+
json_contents.update(decode_json_file(custom_targets_json_path))
123122

124123
for target_details in json_contents.values():
125124
if "device_name" in target_details:
@@ -235,10 +234,8 @@ def fetch_missing():
235234
f"to be added manually?")
236235
missing_mcus_dict[mcu] = cmsis_cache.index[mcu]
237236

238-
if os.path.exists(CUSTOM_TARGETS_JSON_PATH):
239-
LOGGER.info(f"Remove 'device_name' and add the 'memories' section as 'memory_banks' section\nfrom following entries to {CUSTOM_TARGETS_JSON_PATH}:")
240-
else:
241-
LOGGER.info(f"Add the following entries to {CMSIS_MCU_DESCRIPTIONS_JSON_PATH}:")
242-
237+
LOGGER.info(f"In case of Custom target remove 'device_name' from your custom_targets.json5 file and add\n" +
238+
"just the 'memories' section as 'memory_banks' section from content below.\n" +
239+
"Otherwise add the whole following entries to {CMSIS_MCU_DESCRIPTIONS_JSON_PATH}:")
243240
print(json.dumps(missing_mcus_dict, indent=4, sort_keys=True))
244241
sys.exit(1)

0 commit comments

Comments
 (0)