示例#1
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!')
示例#2
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)
示例#3
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])
示例#4
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')
示例#5
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)
示例#6
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)
示例#7
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)
示例#8
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)
示例#9
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.")
示例#10
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)
示例#11
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)