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}'") try: program = MbedProgramFiles.from_existing(program_root) except ValueError as program_files_err: raise ProgramNotFound( f"{dir_path} doesn't look like a path to a valid program. {program_files_err}" ) repo = git_utils.get_repo(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(repo, program, mbed_os)
def checkout(self, force: bool) -> None: """Check out all resolved libs to revision specified in .lib files.""" for lib in self.iter_resolved(): repo = git_utils.get_repo(lib.source_code_path) git_ref = lib.get_git_reference() if not git_ref.ref: git_ref.ref = git_utils.get_default_branch(repo) git_utils.fetch(repo, git_ref.ref) git_utils.checkout(repo, "FETCH_HEAD", force=force)
def checkout(self, force: bool) -> None: """Check out all resolved libs to revision specified in .lib files.""" for lib in self.iter_resolved(): repo = git_utils.get_repo(lib.source_code_path) git_ref = lib.get_git_reference() if not git_ref.ref: repo.git.fetch() git_ref.ref = git_utils.get_default_branch(repo) else: # Fetch only the requested ref repo.git.fetch("origin", git_ref.ref) git_utils.checkout(repo, git_ref.ref, force=force)
def _print_dependency_table(libs: List) -> None: click.echo("The following library dependencies were fetched: \n") table = [] for lib in libs: table.append([ lib.reference_file.stem, lib.get_git_reference().repo_url, lib.source_code_path, git_utils.get_default_branch( git_utils.get_repo(lib.source_code_path)) if not lib.get_git_reference().ref else lib.get_git_reference().ref, ]) headers = ("Library Name", "Repository URL", "Path", "Git Reference") click.echo(tabulate.tabulate(table, headers=headers))
def test_raises_version_control_error_when_no_git_repo_found( self, mock_repo): mock_repo.side_effect = git_utils.git.exc.InvalidGitRepositoryError with self.assertRaises(VersionControlError): git_utils.get_repo(Path())
def test_returns_repo_object(self, mock_repo): repo = git_utils.get_repo(Path()) self.assertTrue(isinstance(repo, mock_repo.__class__))