Skip to content

Commit 58c52fa

Browse files
committed
Recursively merge extra_targets into targets
Recursively merge any target configs in extra_targets.json rather than completely replacing keys at the top level
1 parent e6e3d08 commit 58c52fa

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

tools/targets/__init__.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import inspect
2323
import sys
2424
from copy import copy
25-
from collections import namedtuple
25+
from collections import namedtuple, Mapping
2626
from tools.targets.LPC import patch
2727
from tools.paths import TOOLS_BOOTLOADERS
2828
from tools.utils import json_file_to_dict
@@ -125,6 +125,23 @@ class Target(namedtuple("Target", "name json_data resolution_order resolution_or
125125
# Current/new location of the 'targets.json' file
126126
__targets_json_location = None
127127

128+
@staticmethod
129+
def _merge_dict(dct, merge_dct):
130+
""" Recursive dict merge. Inspired by `dict.update()` however instead of
131+
updating only top-level keys, dict_merge recurses down into dicts nested
132+
to an arbitrary depth, updating keys.
133+
The provided ``merge_dct`` is merged into ``dct`` in place.
134+
:param dct: dict onto which the merge is executed
135+
:param merge_dct: dct merged into dct
136+
:return: None
137+
"""
138+
for k, v in merge_dct.iteritems():
139+
if (k in dct and isinstance(dct[k], dict)
140+
and isinstance(merge_dct[k], Mapping)):
141+
Target._merge_dict(dct[k], merge_dct[k])
142+
else:
143+
dct[k] = merge_dct[k]
144+
128145
@staticmethod
129146
@cached
130147
def get_json_target_data():
@@ -135,7 +152,7 @@ def get_json_target_data():
135152
# If extra_targets.json exists in working directory load it over the top
136153
extra = os.path.join('.', 'extra_targets.json')
137154
if os.path.exists(extra):
138-
targets.update(json_file_to_dict(extra))
155+
Target._merge_dict(targets, json_file_to_dict(extra))
139156

140157
return targets
141158

0 commit comments

Comments
 (0)