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 from_new(cls, dir_path: Path) -> "MbedProgram": """Create an MbedProgram from an empty directory. Creates the directory if it doesn't exist. Args: dir_path: Directory in which to create the program. Raises: ExistingProgram: An existing program was found in the path. """ if _tree_contains_program(dir_path): raise ExistingProgram( f"An existing Mbed program was found in the directory tree {dir_path}. It is not possible to nest Mbed " "programs. Please ensure there is no .mbed file in the cwd hierarchy." ) logger.info(f"Creating Mbed program at path '{dir_path.resolve()}'") dir_path.mkdir(exist_ok=True) program_files = MbedProgramFiles.from_new(dir_path) logger.info( f"Creating git repository for the Mbed program '{dir_path}'") repo = git_utils.init(dir_path) mbed_os = MbedOS.from_new(dir_path / MBED_OS_DIR_NAME) return cls(repo, program_files, 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 test_checks_for_unresolved_libraries(self, fs): root = pathlib.Path(fs, "root") make_mbed_lib_reference(root, resolved=True, ref_url="https://blah") make_mbed_lib_reference(root, name="my-unresolved-lib.lib", resolved=False, ref_url="https://blah") mbed_os_root = root / "mbed-os" mbed_os_root.mkdir() program = MbedProgram( None, MbedProgramFiles(None, pathlib.Path(root / ".mbed"), None), MbedOS(mbed_os_root, None) ) self.assertTrue(program.has_unresolved_libraries())
def test_lists_all_known_libraries(self, fs): root = pathlib.Path(fs, "root") lib_ref = make_mbed_lib_reference(root, resolved=True, ref_url="https://blah") lib_ref_unresolved = make_mbed_lib_reference( root, name="my-unresolved-lib.lib", resolved=False, ref_url="https://blah" ) mbed_os_root = root / "mbed-os" mbed_os_root.mkdir() program = MbedProgram( None, MbedProgramFiles(None, pathlib.Path(root, ".mbed"), None), MbedOS(mbed_os_root, None) ) libs = program.list_known_library_dependencies() self.assertEqual(str(lib_ref_unresolved), str(libs[0])) self.assertEqual(str(lib_ref), str(libs[1]))
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_checkout_libraries(self, mock_lib_refs): program = MbedProgram(None, MbedProgramFiles(None, pathlib.Path(), None), MbedOS(pathlib.Path(), None)) program.checkout_libraries() program.lib_references.checkout.assert_called_once()
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)