示例#1
0
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}")
示例#2
0
文件: ensure_path.py 项目: pypa/pipx
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)
示例#3
0
文件: cli.py 项目: mmyjona/userpath
def append(path, force):
    """Appends to the user PATH. The shell must be restarted for the update to
    take effect.
    """
    if not force:
        if up.in_current_path(path):
            echo_warning((
                'The directory `{}` is already in PATH! If you '
                'are sure you want to proceed, try again with '
                'the -f/--force flag.'.format(path)
            ))
            sys.exit(2)
        elif up.in_new_path(path):
            echo_warning((
                'The directory `{}` is already in PATH, pending a shell '
                'restart! If you are sure you want to proceed, try again '
                'with the -f/--force flag.'.format(path)
            ))
            sys.exit(2)

    if up.append(path):
        echo_success('Success!')
    else:
        echo_failure('An unexpected failure seems to have occurred.')
        sys.exit(1)
示例#4
0
def prepend(locations, shells, all_shells, home, force, quiet):
    """Prepends to the user PATH. The shell must be restarted for the update to
    take effect.
    """
    if not force:
        for location in locations:
            if up.in_current_path(location):
                echo_warning((
                    'The directory `{}` is already in PATH! If you '
                    'are sure you want to proceed, try again with '
                    'the -f/--force flag.'.format(location)
                ))
                sys.exit(2)
            elif up.in_new_path(location, shells=shells, all_shells=all_shells, home=home):
                echo_warning((
                    'The directory `{}` is already in PATH, pending a shell '
                    'restart! If you are sure you want to proceed, try again '
                    'with the -f/--force flag.'.format(location)
                ))
                sys.exit(2)

    try:
        up.prepend(locations, shells=shells, all_shells=all_shells, home=home, check=True)
    except Exception as e:
        echo_failure(str(e))
        sys.exit(1)
    else:
        if not quiet:
            echo_success('Success!')
示例#5
0
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])
示例#6
0
文件: common.py 项目: abn/pipx
def warn_if_not_on_path(local_bin_dir: Path):
    if not userpath.in_current_path(str(local_bin_dir)):
        logging.warning(
            f"{hazard}  Note: {str(local_bin_dir)!r} is not on your PATH environment "
            "variable. These apps will not be globally accessible until "
            "your PATH is updated. Run `pipx ensurepath` to "
            "automatically add it, or manually modify your PATH in your shell's "
            "config file (i.e. ~/.bashrc).")
示例#7
0
    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')
示例#8
0
def verify(locations, shells, all_shells, home, quiet):
    """Checks if locations are in the user PATH."""
    for location in locations:
        if up.in_current_path(location):
            if not quiet:
                echo_success('The directory `{}` is in PATH!'.format(location))
        elif up.in_new_path(location, shells=shells, all_shells=all_shells, home=home):
            echo_warning('The directory `{}` is in PATH, pending a shell restart!'.format(location))
            sys.exit(2)
        else:
            echo_failure('The directory `{}` is not in PATH!'.format(location))
            sys.exit(1)
示例#9
0
文件: common.py 项目: pdecat/pipx
def warn_if_not_on_path(local_bin_dir: Path) -> None:
    if not userpath.in_current_path(str(local_bin_dir)):
        logger.warning(
            pipx_wrap(
                f"""
                {hazard}  Note: {str(local_bin_dir)!r} is not on your PATH
                environment variable. These apps will not be globally
                accessible until your PATH is updated. Run `pipx ensurepath` to
                automatically add it, or manually modify your PATH in your
                shell's config file (i.e. ~/.bashrc).
                """,
                subsequent_indent=" " * 4,
            ))
示例#10
0
def verify(locations):
    """Checks if locations are in the user PATH."""
    for location in locations:
        if up.in_current_path(location):
            echo_success(('The directory `{}` is in PATH!'.format(location)))
        elif up.in_new_path(location):
            echo_warning(
                ('The directory `{}` is in PATH, pending a shell restart!'.
                 format(location)))
            sys.exit(2)
        else:
            echo_failure(
                ('The directory `{}` is not in PATH!'.format(location)))
            sys.exit(1)
示例#11
0
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)
示例#12
0
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)
示例#13
0
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)
示例#14
0
#!/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.")
示例#15
0
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)
示例#16
0
def test_append():
    location = urlsafe_b64encode(urandom(5)).decode()
    assert not userpath.in_current_path(location)
    assert userpath.append(location)
    assert userpath.in_new_path(location)
    assert userpath.need_shell_restart(location)