def test_logs_path_and_raises_when_decode_fails(self): with tempfile.TemporaryDirectory() as tmp_dir: text = "This is not valid JSON<>>" tmp_file = pathlib.Path(tmp_dir, "mbed_lib.json") tmp_file.write_text(text) with self.assertRaises(json.JSONDecodeError): with self.assertLogs(level="ERROR") as logger: decode_json_file(tmp_file) self.assertIn(str(tmp_file), logger.output)
def _load_raw_targets_data(program: MbedProgram) -> Any: targets_data = decode_json_file(program.mbed_os.targets_json_file) if program.files.custom_targets_json.exists(): custom_targets_data = decode_json_file(program.files.custom_targets_json) for custom_target in custom_targets_data: if custom_target in targets_data: raise MbedBuildError( f"Error found in {program.files.custom_targets_json}.\n" f"A target with the name '{custom_target}' already exists in targets.json. " "Please give your custom target a unique name so it can be identified." ) targets_data.update(custom_targets_data) return targets_data
def from_file(config_source_file_path: pathlib.Path, target_filters: Iterable[str], default_name: Optional[str] = None) -> dict: """Load a JSON config file and prepare the contents as a config source.""" return prepare(decode_json_file(config_source_file_path), source_name=default_name, target_filters=target_filters)
def from_mbed_app(cls, mbed_app_path: Path, target_labels: Iterable[str]) -> "Source": """Build Source from mbed_app.json file. Args: mbed_app_path: Path to mbed_app.json file target_labels: Labels for which "target_overrides" should apply """ file_contents = decode_json_file(mbed_app_path) return cls.from_file_contents( file_name=str(mbed_app_path), file_contents=file_contents, namespace="app", target_labels=target_labels )
def _extract_core_labels(target_core: Optional[str]) -> Set[str]: """Find the labels associated with the target's core. Args: target_core: the target core, set as a build attribute Returns: A list of labels associated with the target's core, or an empty set if either core is undefined or no labels found for the core. """ if target_core: mbed_os_metadata = decode_json_file(MBED_OS_METADATA_FILE) return set(mbed_os_metadata["CORE_LABELS"].get(target_core, [])) return set()
def apply_requires_filter( requires_filtered_files: set, files: Iterable[Path], requires: Iterable[str] ) -> Iterable[Path]: """Remove mbed_lib.json files not required by application.""" for required_mbed_lib in requires: for mbed_lib_path in files: lib_contents = decode_json_file(mbed_lib_path) if required_mbed_lib == RequiresFilter.get_mbed_lib_name(lib_contents): requires_filtered_files.add(mbed_lib_path) RequiresFilter.apply_requires_filter( requires_filtered_files, files, RequiresFilter.get_mbed_lib_requires(lib_contents) ) return requires_filtered_files
def _assemble_config_from_sources_and_lib_files( target_attributes: dict, mbed_lib_files: Iterable[Path], mbed_app_file: Optional[Path] = None) -> Config: previous_cumulative_data = None requires = list() target_source = Source.from_target(target_attributes) current_cumulative_data = CumulativeData.from_sources([target_source]) if mbed_app_file: app_data = decode_json_file(mbed_app_file) requires = app_data["requires"] if "requires" in app_data else [] while previous_cumulative_data != current_cumulative_data: current_labels = current_cumulative_data.labels | current_cumulative_data.extra_labels filtered_files = _filter_files( mbed_lib_files, current_labels, current_cumulative_data.features, current_cumulative_data.components, requires, ) mbed_lib_sources = [ Source.from_mbed_lib(file, current_labels) for file in filtered_files ] all_sources = [target_source] + mbed_lib_sources if mbed_app_file: all_sources = all_sources + [ Source.from_mbed_app(mbed_app_file, current_labels) ] previous_cumulative_data = current_cumulative_data current_cumulative_data = CumulativeData.from_sources(all_sources) _update_target_attributes(target_attributes, current_cumulative_data) return Config.from_sources(all_sources)
def test_invalid_json(tmp_path): lib_json_path = tmp_path / "mbed_lib.json" lib_json_path.write_text("name") with pytest.raises(json.JSONDecodeError): decode_json_file(lib_json_path)
def __call__(self, path: Path) -> bool: """Return True if no requires are specified or our lib name is in the list of required libs.""" return decode_json_file(path).get( "name", "") in self._requires or not self._requires
def test_assembles_config_using_all_relevant_files(self): target = { "config": { "foo": { "value": None } }, "macros": [], "labels": ["A"], "extra_labels": [], "features": ["RED"], "components": [], "c_lib": "std", "printf_lib": "minimal-printf", } mbed_lib_files = [ { "path": Path("subdir", "FEATURE_RED", "mbed_lib.json"), "json_contents": { "name": "red", "config": { "bool": { "value": False } }, "target_overrides": { "A": { "bool": True, "target.features_add": ["BLUE"], "target.components_add": ["LEG"] } }, "macros": ["RED_MACRO"], }, }, { "path": Path("TARGET_A", "mbed_lib.json"), "json_contents": { "name": "a", "config": { "number": { "value": 123 } }, "target_overrides": { "*": { "target.features_add": ["RED"] } }, }, }, { "path": Path("COMPONENT_LEG", "mbed_lib.json"), "json_contents": { "name": "leg", "config": { "number-of-fingers": { "value": 5 } }, "macros": ["LEG_MACRO"], }, }, ] unused_mbed_lib_file = { "path": Path("subdir", "FEATURE_BROWN", "mbed_lib.json"), "json_contents": { "name": "brown", "target_overrides": { "*": { "red.bool": "DON'T USE ME" } }, "macros": ["DONT_USE_THIS_MACRO"], }, } mbed_app_file = { "path": Path("mbed_app.json"), "json_contents": { "target_overrides": { "*": { "target.foo": "bar" } } }, } with TemporaryDirectory() as directory: created_mbed_lib_files = create_files(directory, mbed_lib_files) created_mbed_app_file = create_files(directory, [mbed_app_file])[0] create_files(directory, [unused_mbed_lib_file]) subject = _assemble_config_from_sources( target, find_files("mbed_lib.json", Path(directory)), created_mbed_app_file) mbed_lib_sources = [ prepare(decode_json_file(Path(directory, file)), target_filters=["A"]) for file in created_mbed_lib_files ] mbed_app_source = prepare(decode_json_file( Path(directory, created_mbed_app_file)), target_filters=["A"]) expected_config = Config(prepare(target, source_name="target")) for source in mbed_lib_sources + [mbed_app_source]: expected_config.update(source) subject["config"].sort(key=lambda x: x.name) expected_config["config"].sort(key=lambda x: x.name) assert subject == expected_config