Prevent modifying existing targets.

A warning will be printed if it is attempted.
pull/4103/head
Andrew Leech 2017-06-10 21:45:14 +10:00
parent 6bd55a16fe
commit 4491d2e3f7
2 changed files with 9 additions and 21 deletions

View File

@ -128,23 +128,6 @@ class Target(namedtuple("Target", "name json_data resolution_order resolution_or
# Extra custom targets files
__extra_target_json_files = []
@staticmethod
def _merge_dict(dct, merge_dct):
""" Recursive dict merge. Inspired by `dict.update()` however instead of
updating only top-level keys, dict_merge recurses down into dicts nested
to an arbitrary depth, updating keys.
The provided ``merge_dct`` is merged into ``dct`` in place.
:param dct: dict onto which the merge is executed
:param merge_dct: dct merged into dct
:return: None
"""
for k, v in merge_dct.iteritems():
if (k in dct and isinstance(dct[k], dict)
and isinstance(merge_dct[k], Mapping)):
Target._merge_dict(dct[k], merge_dct[k])
else:
dct[k] = merge_dct[k]
@staticmethod
@cached
def get_json_target_data():
@ -153,7 +136,11 @@ class Target(namedtuple("Target", "name json_data resolution_order resolution_or
Target.__targets_json_location_default)
for extra_target in Target.__extra_target_json_files:
Target._merge_dict(targets, json_file_to_dict(extra_target))
for k, v in json_file_to_dict(extra_target).iteritems():
if k in targets:
print 'WARNING: Custom target "%s" cannot replace existing target.' % k
else:
targets[k] = v
return targets

View File

@ -76,7 +76,7 @@ class TestTargets(unittest.TestCase):
assert TARGET_MAP['Test_Target'].core is None, \
"attributes should be inherited from Target"
def test_modify_default_target(self):
def test_modify_existing_target(self):
"""Set default targets file, then override base Target definition"""
initial_target_json = """
{
@ -132,8 +132,9 @@ class TestTargets(unittest.TestCase):
update_target_data()
assert TARGET_MAP["Test_Target"].core == "Cortex-M4"
assert TARGET_MAP["Test_Target"].default_toolchain == 'GCC_ARM'
assert TARGET_MAP["Test_Target"].bootloader_supported == True
# The existing target should not be modified by custom targets
assert TARGET_MAP["Test_Target"].default_toolchain != 'GCC_ARM'
assert TARGET_MAP["Test_Target"].bootloader_supported != True
if __name__ == '__main__':