示例#1
0
def show_packages_info(pacman_args: 'PacmanArgs',
                       packages_of_user_names: List[str]) -> None:
    """
    shows the information of packages, just as pacman -Si
    :param pacman_args:             the parsed args
    :param packages_of_user_names:  the targets to show the information of
    """
    # pacman output
    run(["pacman"] + pacman_args.args_as_list(), stderr=DEVNULL)

    # output for aur packages
    for package_dict in get_aur_info(packages_of_user_names):
        for key, value in package_dict.items():
            if type(value) is list:
                value = '  '.join(value)
            elif key in ["OutOfDate", "FirstSubmitted", "LastModified"
                         ] and value is not None:
                value = datetime.fromtimestamp(value).replace(
                    tzinfo=tzlocal()).strftime('%c')
            print("{}{} {}".format(Colors.BOLD(key.ljust(16)),
                                   Colors.BOLD(':'), value))
        print()

    sys.exit(0)
示例#2
0
def search_and_print(names: Sequence[str], installed_system,
                     pacman_params: str, repo: bool, aur: bool):
    """
    Searches for something and prints the results

    :param names:               The things to search for
    :param installed_system:    A system containing the installed packages
    :param pacman_params:       parameters for pacman as string
    :param repo:                search only in repo
    :param aur:                 search only in aur
    """
    if not names:
        return

    if not aur:
        # escape for pacman
        to_escape = list("()+?|{}")
        for char in to_escape:
            pacman_params = pacman_params.replace(char, "\{}".format(char))

        run("pacman {}".format(pacman_params), shell=True)

    if not repo:
        # see: https://docs.python.org/3/howto/regex.html
        regex_chars = list("^.+*?$[](){}\|")

        regex_patterns = [
            regex.compile(name, regex.IGNORECASE) for name in names
        ]
        names_beginnings_without_regex = []
        for name in names:
            index_start = -1
            index_end = len(name)
            for i, char in enumerate(name):
                if char not in regex_chars and index_start == -1:
                    index_start = i
                elif char in regex_chars and index_start != -1:
                    # must be at least two consecutive non regex chars
                    if i - index_start < 2:
                        index_start = -1
                        continue
                    index_end = i
                    break

            if index_start == -1 or index_end - index_start < 2:
                aurman_error(
                    "Your query {} contains not enough non regex chars!".
                    format(Colors.BOLD(Colors.LIGHT_MAGENTA(name))))
                raise InvalidInput(
                    "Your query {} contains not enough non regex chars!".
                    format(Colors.BOLD(Colors.LIGHT_MAGENTA(name))))

            names_beginnings_without_regex.append(name[index_start:index_end])

        found_names = set(
            ret_dict['Name'] for ret_dict in get_aur_info(
                [names_beginnings_without_regex[0]], True)
            if regex_patterns[0].findall(ret_dict['Name'])
            or isinstance(ret_dict['Description'], str)
            and regex_patterns[0].findall(ret_dict['Description']))

        for i in range(1, len(names)):
            found_names &= set(
                ret_dict['Name'] for ret_dict in get_aur_info(
                    [names_beginnings_without_regex[i]], True)
                if regex_patterns[i].findall(ret_dict['Name'])
                or isinstance(ret_dict['Description'], str)
                and regex_patterns[i].findall(ret_dict['Description']))

        search_return = get_aur_info(found_names)

        for ret_dict in sorted(search_return,
                               key=lambda x: float(x['Popularity']),
                               reverse=True):
            repo_with_slash = Colors.BOLD(Colors.LIGHT_MAGENTA("aur/"))
            name = Colors.BOLD(ret_dict['Name'])
            if ret_dict['OutOfDate'] is None:
                version = Colors.BOLD(Colors.GREEN(ret_dict['Version']))
            else:
                version = Colors.BOLD(Colors.RED(ret_dict['Version']))

            first_line = "{}{} {} ({}, {})".format(repo_with_slash, name,
                                                   version,
                                                   ret_dict['NumVotes'],
                                                   ret_dict['Popularity'])
            if ret_dict['Name'] in installed_system.all_packages_dict:
                if version_comparison(
                        ret_dict['Version'], "=",
                        installed_system.all_packages_dict[
                            ret_dict['Name']].version):
                    first_line += " {}".format(
                        Colors.BOLD(Colors.CYAN("[installed]")))
                else:
                    first_line += " {}".format(
                        Colors.BOLD(
                            Colors.CYAN("[installed: {}]".format(
                                installed_system.all_packages_dict[
                                    ret_dict['Name']].version))))
            print(first_line)
            print("    {}".format(ret_dict['Description']))
示例#3
0
def possible_completions():
    if argv[3] == "--auto_complete_index":
        cur: str = ''
        index: int = int(argv[4]) - 1
        line: List[str] = argv[7:]
    else:
        cur: str = argv[3]
        index: int = int(argv[5]) - 1
        line: List[str] = argv[8:]

    for word in line:
        # sync operation is for us, everything else is not
        if word.startswith("-") and "S" in word or word == "--sync":
            break
    else:
        print("call_pacman")
        return

    # fetch valid options
    options = []
    for option in pacman_options:
        if len(option) == 1:
            options.append("-{}".format(option))
        else:
            options.append("--{}".format(option))

    # show options
    if cur.startswith("-"):
        # remove already used options
        for word in line:
            if not word.startswith("-"):
                continue

            if word.startswith("--"):
                if word in options:
                    options.remove(word)
                continue

            values = word[1:]
            for value in values:
                if "-{}".format(value) in options:
                    options.remove("-{}".format(value))

        print(" ".join(options))
        return

    # get valid option before cur
    option = None
    opt_index = None
    for i in reversed(range(0, index)):
        word = line[i]
        if not word.startswith("-"):
            continue

        if word.startswith("--"):
            if word in options:
                option = word.replace("-", "")
                opt_index = i
                break
            continue

        for value in reversed(word[1:]):
            if "-{}".format(value) in options:
                option = value
                opt_index = i
                break

    # decide if we can show the available sync packages or not
    if option is not None:
        option_num_args = pacman_options[option][1]
        if option_num_args == 2 or (index - opt_index) <= option_num_args:
            return

    results = run(
        "expac -Ss '%n' ^{}".format(cur), shell=True, stdout=PIPE, stderr=DEVNULL, universal_newlines=True
    ).stdout.splitlines()

    results.extend([ret_dict["Name"] for ret_dict in get_aur_info((cur,), True, True)])

    print(" ".join(results))