def conda_update_all(name: str, channels: str, packages: Packages = (), yes: bool = False) -> str: """Conda update all packages.""" update_cmd = get_conda_update_all_command(name, packages, yes=yes) run_command(f"{update_cmd} {channels}", error=CondaEnvTrackerCondaError) return update_cmd
def update_conda_environment(env_dir: PathLike) -> None: """Update the given environment with using environment yaml file. This file only contains packages the user has specifically asked for and does not contain dependencies. """ env_dir = Path(env_dir) env_file = _get_env_file(env_dir) run_command(f'conda env update --prune --file "{env_file}"', error=CondaEnvTrackerCondaError)
def conda_install(name: str, packages: Packages, channel_command: str, yes: bool = False) -> str: """Conda install the packages""" install_cmd = get_conda_install_command(name, packages, yes) run_command(f"{install_cmd} {channel_command}", error=CondaEnvTrackerCondaError) return install_cmd
def conda_remove(name: str, packages: Packages, channel_command: str, yes: bool = False) -> str: """Conda remove the packages""" remove_cmd = get_conda_remove_command(name, packages, yes=yes) run_command(f"{remove_cmd} {channel_command}", error=CondaEnvTrackerCondaError) return remove_cmd
def pip_remove(name: str, packages: Packages, yes): """Pip uninstall package, can handle custom package removal""" commands = [] if not is_current_conda_env(name): commands.append(get_conda_activate_command(name=name)) commands.append(get_pip_remove_command(packages, yes)) command = " && ".join(commands) logger.debug(f"Pip remove command: {command}") run_command(command, error=PipRemoveError)
def test_exit_with_user_no(mocker): run_mock = mocker.patch("conda_env_tracker.gateways.utils.run") attrs = { "return_value.return_code": 0, "return_value.stderr": "", "return_value.stdout": "Proceed ([y]/n)? n\n", } run_mock.configure_mock(**attrs) with pytest.raises(SystemExit): run_command(command="command", error=CondaEnvTrackerCondaError)
def test_run_command_error_message(mocker, caplog): mocker.patch( "conda_env_tracker.gateways.utils.run", return_value=mocker.Mock(failed=True, stdout="", stderr="Error message"), ) class CustomException(Exception): """Custom Exception""" with pytest.raises(CustomException) as err: run_command("command", CustomException) assert caplog.records[0].message == "command" assert str(err.value) == "Error message"
def conda_create( name: str, packages: Packages, channels: ListLike = None, yes: bool = False, strict_channel_priority: bool = True, ) -> str: """Create a conda environment.""" create_cmd = get_conda_create_command( name, packages, channels, yes=yes, strict_channel_priority=strict_channel_priority, ) logger.debug(f"Conda creation command:\n{create_cmd}") run_command(create_cmd, error=CondaEnvTrackerCondaError) return create_cmd
def _run_r_remove(name: str, r_command: str) -> str: """Run r remove command""" command = get_shell_command(name=name, r_command=r_command) process = run_command(command, error=RError) if process.failed: raise RError( f"Error removing R packages:\n{process.stderr}\nenvironment='{name}' and command='{r_command}'." ) return command
def _run_r_install(name: str, packages: Packages, r_command: str) -> str: """Run r install command""" command = get_shell_command(name=name, r_command=r_command) process = run_command(command, error=RError) if process.failed or _cannot_install_r_package(process.stderr, packages): raise RError( f"Error installing R packages:\n{process.stderr}\nenvironment='{name}' and command='{r_command}'." ) return command
def update_r_environment(name: str, env_dir: PathLike) -> None: """Update the R packages in the environment.""" install_r = Path(env_dir) / "install.R" if install_r.exists(): r_update_command = f'source("{install_r.absolute()}")' command = get_shell_command(name=name, r_command=r_update_command) process = run_command(command, error=RError) if process.failed: raise RError( f"Error updating R packages in environment:\n{process.stderr}")