def from_existing(cls, dir_path: Path, check_mbed_os: bool = True) -> "MbedProgram": """Create an MbedProgram from an existing program directory. Args: dir_path: Directory containing an Mbed program. check_mbed_os: If True causes an exception to be raised if the Mbed OS source directory does not exist. Raises: ProgramNotFound: An existing program was not found in the path. """ program_root = _find_program_root(dir_path) logger.info(f"Found existing Mbed program at path '{program_root}'") program = MbedProgramFiles.from_existing(program_root) try: mbed_os = MbedOS.from_existing(program_root / MBED_OS_DIR_NAME, check_mbed_os) except ValueError as mbed_os_err: raise MbedOSNotFound( f"Mbed OS was not found due to the following error: {mbed_os_err}" "\nYou may need to resolve the mbed-os.lib reference. You can do this by performing a `checkout`." ) return cls(program, mbed_os)
def test_from_existing_finds_existing_mbed_os_data(self, tmp_path): root_path = pathlib.Path(tmp_path, "my-version-of-mbed-os") make_mbed_os_files(root_path) mbed_os = MbedOS.from_existing(root_path) assert mbed_os.targets_json_file == root_path / "targets" / "targets.json"
def test_from_existing_finds_existing_mbed_os_data(self, fs): root_path = pathlib.Path(fs, "my-version-of-mbed-os") make_mbed_os_files(root_path) mbed_os = MbedOS.from_existing(root_path) self.assertEqual(mbed_os.targets_json_file, root_path / "targets" / "targets.json")
def from_url(cls, url: str, dst_path: Path, check_mbed_os: bool = True) -> "MbedProgram": """Fetch an Mbed program from a remote URL. Args: url: URL of the remote program repository. dst_path: Destination path for the cloned program. Raises: ExistingProgram: `dst_path` already contains an Mbed program. """ if _tree_contains_program(dst_path): raise ExistingProgram( f"The destination path '{dst_path}' already contains an Mbed program. Please set the destination path " "to an empty directory.") logger.info(f"Cloning Mbed program from URL '{url}'.") repo = git_utils.clone(url, dst_path) try: program_files = MbedProgramFiles.from_existing(dst_path) except ValueError as e: raise ProgramNotFound( f"This repository does not contain a valid Mbed program at the top level. {e} " "Cloned programs must contain an mbed-os.lib file containing the URL to the Mbed OS repository. It is " "possible you have cloned a repository containing multiple mbed-programs. If this is the case, you " "should cd to a directory containing a program before performing any other operations." ) try: mbed_os = MbedOS.from_existing(dst_path / MBED_OS_DIR_NAME, check_mbed_os) except ValueError as mbed_err: raise MbedOSNotFound(f"{mbed_err}") return cls(repo, program_files, mbed_os)
def test_raises_if_files_missing(self, fs): root_path = pathlib.Path(fs, "my-version-of-mbed-os") root_path.mkdir() with self.assertRaises(ValueError): MbedOS.from_existing(root_path)
def test_raises_if_files_missing(self, tmp_path): root_path = pathlib.Path(tmp_path, "my-version-of-mbed-os") root_path.mkdir() with pytest.raises(ValueError): MbedOS.from_existing(root_path)