示例#1
0
def pip_update_from_pypy(package_name_or_link: str,
                         use_sudo: bool,
                         show_output: bool = True) -> bool:
    """
    :returns updated - True if updated, False if it was already up to date


    >>> assert pip_update_from_pypy('urllib3==1.24.1', use_sudo=True, show_output=False) is not None    # first Update to old Version
    >>> assert pip_update_from_pypy('urllib3', use_sudo=True, show_output=False) == True                # second Update to new Version
    >>> assert pip_update_from_pypy('urllib3', use_sudo=True, show_output=False) == False               # third Update - is already up to date
    >>> assert pip_update_from_pypy('chardet', use_sudo=True, show_output=False) is not None            # third Update - is already up to date

    """
    try:
        pip_command = lib_helpers.get_latest_pip_command().command_string
        ls_commands = lib_helpers.get_ls_commands_prepend_sudo(
            [pip_command, "install", "--upgrade", package_name_or_link],
            use_sudo=use_sudo)
        shell_response = lib_shell.run_shell_ls_command(
            ls_commands, pass_std_out_line_by_line=show_output)
        if f"Requirement already up-to-date: {package_name_or_link}" in shell_response.stdout:
            return False
        else:
            return True
    except subprocess.CalledProcessError as exc:
        if exc.returncode == 13:  # pip permission error
            raise PermissionError(exc.stderr)
        else:
            error = 'Package "{pypy_package}" can not be installed via pip:\n\n{stderr}'.format(
                pypy_package=package_name_or_link, stderr=exc.stderr)
            lib_log_utils.banner_error(error)
            raise ValueError(error)
示例#2
0
def install_linux_package(
    package: str,
    parameters: List[str] = [],
    quiet: bool = False,
    reinstall: bool = False,
    use_sudo: bool = True,
    raise_on_returncode_not_zero: bool = True
) -> lib_shell.ShellCommandResponse:
    """
    returns 0 if ok, otherwise returncode

    >>> is_dialog_installed = is_package_installed('dialog')
    >>> result = uninstall_linux_package('dialog', quiet=True)
    >>> assert is_package_installed('dialog') == False
    >>> result1 = install_linux_package('dialog', quiet=True)
    >>> assert is_package_installed('dialog') == True
    >>> result2 = install_linux_package('dialog', quiet=True, reinstall=True)
    >>> assert is_package_installed('dialog') == True
    >>> result3 = install_linux_package('unknown', quiet=True, raise_on_returncode_not_zero = False)
    >>> install_linux_package('unknown', quiet=True, raise_on_returncode_not_zero = True)     # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    subprocess.CalledProcessError: Command 'sudo apt-get install unknown -y' returned non-zero exit status ...

    >>> result = uninstall_linux_package('dialog', quiet=True)
    >>> if is_dialog_installed:
    ...     result = install_linux_package('dialog', quiet=True)
    ... else:
    ...     result = uninstall_linux_package('dialog', quiet=True)

    """

    result = lib_shell.ShellCommandResponse()
    if not is_package_installed(package) or reinstall:

        if reinstall:
            l_command = [
                conf_install.apt_command, 'install', '--reinstall', package,
                '-y'
            ]
        else:
            l_command = [conf_install.apt_command, 'install', package, '-y']

        l_command = l_command + parameters

        result = lib_shell.run_shell_ls_command(
            ls_command=l_command,
            shell=True,
            quiet=quiet,
            use_sudo=use_sudo,
            raise_on_returncode_not_zero=raise_on_returncode_not_zero,
            pass_stdout_stderr_to_sys=True)
    return result
def is_pip_package_installed(package_name: str) -> bool:
    """
    >>> assert is_pip_package_installed('pip') == True
    >>> assert is_pip_package_installed('unknown_package') == False


    """
    package_name = package_name.replace(
        '_', '-'
    )  # even when the package name is with underscore, it is installed and listed with dash !

    pip_command = get_latest_pip_command().command_string
    ls_commands = [pip_command, "list"]
    shell_response = lib_shell.run_shell_ls_command(ls_commands)
    line = lib_regexp.reg_grep(package_name, shell_response.stdout)
    if not line:
        return False
    else:
        return True
示例#4
0
def uninstall_linux_package(
    package: str,
    quiet: bool = False,
    use_sudo: bool = True,
    raise_on_returncode_not_zero: bool = True
) -> lib_shell.ShellCommandResponse:

    result = lib_shell.ShellCommandResponse()

    if is_package_installed(package) or is_wildcard_in_package_name(package):
        l_command = [conf_install.apt_command, 'purge', package, '-y']

        result = lib_shell.run_shell_ls_command(
            ls_command=l_command,
            shell=True,
            quiet=quiet,
            use_sudo=use_sudo,
            raise_on_returncode_not_zero=raise_on_returncode_not_zero,
            pass_stdout_stderr_to_sys=True)
    return result
def get_git_remote_hash(git_repository_slug: str) -> str:
    """
    >>> # https://github.com/pypa/pip.git | git+https://github.com/pypa/pip.git | https://github.com/pypa/archive/master.zip

    >>> git_repository_slug = get_git_repository_slug_from_link(package_link='https://github.com/pypa/pip.git')
    >>> assert len(get_git_remote_hash(git_repository_slug=git_repository_slug)) == len('59e6ce2847bda24b3f29683251d10ae5c3cab357')
    >>> git_repository_slug = get_git_repository_slug_from_link(package_link='git+https://github.com/pypa/pip.git')
    >>> assert len(get_git_remote_hash(git_repository_slug=git_repository_slug)) == len('59e6ce2847bda24b3f29683251d10ae5c3cab357')
    >>> git_repository_slug = get_git_repository_slug_from_link(package_link='https://github.com/pypa/pip/archive/master.zip')
    >>> assert len(get_git_remote_hash(git_repository_slug=git_repository_slug)) == len('59e6ce2847bda24b3f29683251d10ae5c3cab357')

    """
    url = 'https://github.com/{git_repository_slug}.git'.format(
        git_repository_slug=git_repository_slug)
    git_command = lib_bash.get_bash_command('git')
    result = lib_shell.run_shell_ls_command([
        git_command.command_string, '--no-pager', 'ls-remote', '--quiet', url
    ])
    line = lib_regexp.reg_grep('HEAD', result.stdout)[0].expandtabs()
    git_remote_hash = str(line.split()[0])
    return git_remote_hash
示例#6
0
def is_package_installed(package: str) -> bool:
    """
    returns True if installed, otherwise False

    >>> assert is_package_installed('apt') == True
    >>> assert is_package_installed('unknown') == False

    """
    log_settings = lib_shell.set_log_settings_to_level(logging.NOTSET)
    response = lib_shell.run_shell_ls_command(
        ['dpkg', '--list', package],
        raise_on_returncode_not_zero=False,
        log_settings=log_settings)
    result = lib_regexp.reg_grep(package + ' ', response.stdout)
    if not result:
        return False
    if len(result) != 1:
        raise RuntimeError(
            'can not determine if package "{package}" is installed'.format(
                package=package))
    if result[0].startswith('ii'):
        return True
    else:
        return False