Skip to content

Commit

Permalink
Add --all-shells flag to ensure_path
Browse files Browse the repository at this point in the history
  • Loading branch information
gh271 committed Dec 24, 2024
1 parent 86e0701 commit f37da73
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.d/1585.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `--all-shells` flag to `pipx ensurepath`.
12 changes: 7 additions & 5 deletions src/pipx/commands/ensure_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_pipx_user_bin_path() -> Optional[Path]:
return pipx_bin_path


def ensure_path(location: Path, *, force: bool, prepend: bool = False) -> Tuple[bool, bool]:
def ensure_path(location: Path, *, force: bool, prepend: bool = False, all_shells: bool = False) -> Tuple[bool, bool]:
"""Ensure location is in user's PATH or add it to PATH.
If prepend is True, location will be prepended to PATH, else appended.
Returns True if location was added to PATH
Expand All @@ -63,9 +63,9 @@ def ensure_path(location: Path, *, force: bool, prepend: bool = False) -> Tuple[

if force or (not in_current_path and not need_shell_restart):
if prepend:
path_added = userpath.prepend(location_str, "pipx")
path_added = userpath.prepend(location_str, "pipx", all_shells=all_shells)
else:
path_added = userpath.append(location_str, "pipx")
path_added = userpath.append(location_str, "pipx", all_shells=all_shells)
if not path_added:
print(
pipx_wrap(
Expand Down Expand Up @@ -100,7 +100,7 @@ def ensure_path(location: Path, *, force: bool, prepend: bool = False) -> Tuple[
return (path_added, need_shell_restart)


def ensure_pipx_paths(force: bool, prepend: bool = False) -> ExitCode:
def ensure_pipx_paths(force: bool, prepend: bool = False, all_shells: bool = False) -> ExitCode:
"""Returns pipx exit code."""
bin_paths = {paths.ctx.bin_dir}

Expand All @@ -113,7 +113,9 @@ def ensure_pipx_paths(force: bool, prepend: bool = False) -> ExitCode:
path_action_str = "prepended to" if prepend else "appended to"

for bin_path in bin_paths:
(path_added_current, need_shell_restart_current) = ensure_path(bin_path, prepend=prepend, force=force)
(path_added_current, need_shell_restart_current) = ensure_path(
bin_path, prepend=prepend, force=force, all_shells=all_shells
)
path_added |= path_added_current
need_shell_restart |= need_shell_restart_current

Expand Down
7 changes: 6 additions & 1 deletion src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar
return commands.run_pip(package, venv_dir, args.pipargs, args.verbose)
elif args.command == "ensurepath":
try:
return commands.ensure_pipx_paths(prepend=args.prepend, force=args.force)
return commands.ensure_pipx_paths(prepend=args.prepend, force=args.force, all_shells=args.all_shells)
except Exception as e:
logger.debug("Uncaught Exception:", exc_info=True)
raise PipxError(str(e), wrap_message=False) from None
Expand Down Expand Up @@ -906,6 +906,11 @@ def _add_ensurepath(subparsers: argparse._SubParsersAction, shared_parser: argpa
"PATH already contains paths to pipx and pipx-install apps."
),
)
p.add_argument(
"--all-shells",
action="store_true",
help=("Add directories to PATH in all shells instead of just the current one."),
)


def _add_environment(subparsers: argparse._SubParsersAction, shared_parser: argparse.ArgumentParser) -> None:
Expand Down

0 comments on commit f37da73

Please sign in to comment.