def ensure_path(location: Path, *, force: bool): location_str = str(location) post_install_message = ( "You likely need to open a new terminal or re-login for " "the changes to take effect.") if userpath.in_current_path(location_str) or userpath.need_shell_restart( location_str): if not force: if userpath.need_shell_restart(location_str): print(f"{location_str} has been already been added to PATH. " f"{post_install_message}") else: logging.warning(( f"The directory `{location_str}` is already in PATH. If you " "are sure you want to proceed, try again with " "the '--force' flag.\n\n" f"Otherwise pipx is ready to go! {stars}")) return userpath.append(location_str) print(f"Success! Added {location_str} to the PATH environment variable.") print("Consider adding shell completions for pipx. " "Run 'pipx completions' for instructions.") print() print(f"{post_install_message} {stars}")
def ensure_path(location: Path, *, force: bool) -> Tuple[bool, bool]: """Ensure location is in user's PATH or add it to PATH. Returns True if location was added to PATH """ location_str = str(location) path_added = False need_shell_restart = userpath.need_shell_restart(location_str) in_current_path = userpath.in_current_path(location_str) if force or (not in_current_path and not need_shell_restart): userpath.append(location_str, "pipx") print( pipx_wrap( f"Success! Added {location_str} to the PATH environment variable.", subsequent_indent=" " * 4, )) path_added = True need_shell_restart = userpath.need_shell_restart(location_str) elif not in_current_path and need_shell_restart: print( pipx_wrap( f""" {location_str} has been been added to PATH, but you need to open a new terminal or re-login for this PATH change to take effect. """, subsequent_indent=" " * 4, )) else: print( pipx_wrap(f"{location_str} is already in PATH.", subsequent_indent=" " * 4)) return (path_added, need_shell_restart)
def test_append_multiple(): location1 = urlsafe_b64encode(urandom(5)).decode() location2 = urlsafe_b64encode(urandom(5)).decode() assert not userpath.in_current_path([location1, location2]) assert userpath.append([location1, location2]) assert userpath.in_new_path([location1, location2]) assert userpath.need_shell_restart([location1, location2])
def add_path_to_environment(path): path = str(path) post_install_message = ( "You likely need to open a new terminal or re-login for changes to your $PATH" "to take effect.") if userpath.in_current_path(path) or userpath.need_shell_restart(path): if userpath.need_shell_restart(path): print( f"{path} has already been added to PATH. " f"{post_install_message}", file=sys.stderr, ) return userpath.append(path) print(f"Success! Added {path} to the PATH environment variable.", file=sys.stderr) print(file=sys.stderr) print(post_install_message, file=sys.stderr)
def test_append_multiple(self, request, shell_test): if shell_test is None: locations = [get_random_path(), get_random_path()] assert not userpath.in_current_path(locations) assert userpath.append(locations, check=True) assert userpath.in_new_path(locations) assert userpath.need_shell_restart(locations) else: process = shell_test(request.node.name) stdout, stderr = process.communicate() assert process.returncode == 0, (stdout + stderr).decode('utf-8')
def test_prepend(): location = get_random_path() assert not userpath.in_current_path(location) assert userpath.prepend(location, check=True) assert userpath.in_new_path(location) assert userpath.need_shell_restart(location)
def test_append_multiple(): locations = [get_random_path(), get_random_path()] assert not userpath.in_current_path(locations) assert userpath.append(locations, check=True) assert userpath.in_new_path(locations) assert userpath.need_shell_restart(locations)
#!/usr/bin/env python """ This simple meta script just adds the `script` directory to path. """ import userpath from rich.console import Console from pathlib import Path us = Path(__file__) scripts_dir = (us / ".." / "scripts").resolve() scripts = str(scripts_dir) console = Console() print = console.print with console.status("Checking if scripts directory is in path") as status: if not userpath.in_current_path(scripts): status.update("Adding scripts directory to path") userpath.append(scripts) status.update("Checking if scripts directory is now in path") if userpath.in_new_path(scripts): print("Added scripts directory to path.") else: print("Scripts directory is already in path.") status.update("Checking if shell needs restarting") if userpath.need_shell_restart(scripts): print("You need to restart your shell for this to take effect.")
def test_prepend(): location = urlsafe_b64encode(urandom(5)).decode() assert not userpath.in_current_path(location) assert userpath.prepend(location) assert userpath.in_new_path(location) assert userpath.need_shell_restart(location)