- - 编辑 - -
将脚本的名称从pacsearch更改为pacdot
显然yaourt -Ssaq可以做到这一点,因此该脚本不是我想的那样必要。虽然,我仍然发现使用pacdot -w在文本文档中打开结果很有帮助。
- -/编辑 - -

这不是问题,但我认为其他人可能会觉得有用。有人可能最终会在stackoverflow上尝试寻找这样的解决方案。

在Arch Linux上,我一直发现自己在用pacman或yaourt搜索,希望我能得到软件包名称,而不是所有多余的东西。例如,我很希望能够运行yaourt -Sa $(yaourt -Ssa package)。奇怪的是,pacman和yaourt似乎没有这个选项(至少我不能告诉我),所以我编写了一个python脚本来做到这一点。如果需要,请复制它。您可以根据需要命名,但是我将其称为pacdot.py
pacdot.py package类似于yaourt -Ssa package,但仅列出软件包名称。

我添加了一些其他选项:

  • pacdot.py -o package将仅列出来自官方Arch存储库的结果,而不列出AUR。
  • pacdot.py -i package将安装所有找到的软件包。如果您曾经考虑过运行类似yaourt -Sa $(yaourt -Ssa package)的命令,那么此命令就是这样做的。
  • pacdot.py -w package将:


  • 这是代码:
    #!/bin/python3
    import argparse
    import re
    from subprocess import Popen, PIPE, call
    from collections import deque
    
    
    desc = ''.join(('Search the official Arch and AUR databases ',
                    'and return package names only. ',
                    'e.g.: `pacdot.py arch` will return "arch", ',
                    'whereas `$ yaourt -Ssa arch` will return ',
                    '"community/arch 1.3.5-10',
                    '    A modern and remarkable revision control system."'
                    ))
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument('package',
                        help='Package to search with pacman')
    parser.add_argument('-o', '--official', action='store_true',
                        help='Search official repositories only, not the AUR')
    parser.add_argument('-i', '--install', action='store_true',
                        help='Install found packages')
    parser.add_argument('-w', '--write', action='store_true',
                        help='Write to file')
    
    #Set args strings.
    args = parser.parse_args()
    pkg = args.package
    official_only = args.official
    install = args.install
    write = args.write
    
    # Do yaourt search.
    package_search = Popen(['yaourt', '-Ssa', '%s' % pkg], stdout=PIPE).communicate()
    # Put each found package into a list.
    package_titles_descs = str(package_search[0]).split('\\n')
    # Strip off the packages descriptions.
    package_titles = [package_titles_descs[i]
                      for i in range(0, len(package_titles_descs), 2)]
    # Remove empty item in list.
    del(package_titles[-1])
    
    # Make a separate list of the non-aur packages.
    package_titles_official = deque(package_titles)
    [package_titles_official.remove(p)
        for p in package_titles if p.startswith('aur')]
    
    # Strip off extra stuff like repository names and version numbers.
    packages_all = [re.sub('([^/]+)/([^\s]+) (.*)',
                           r'\2', str(p))
                    for p in package_titles]
    packages_official = [re.sub('([^/]+)/([^\s]+) (.*)',
                                r'\2', str(p))
                         for p in package_titles_official]
    
    # Mark the aur packages.
    #     (Not needed, just in case you want to modify this script.)
    #packages_aur = packages_all[len(packages_official):]
    
    # Set target packages to 'all' or 'official repos only'
    #     based on argparse arguments.
    if official_only:
        packages = packages_official
    else:
        packages = packages_all
    
    # Print the good stuff.
    for p in packages:
        print(p)
    
    if write:
        # Write results to file.
        filename = ''.join((pkg, '.txt'))
        with open(filename, 'a') as f:
            print(''.join(('Yaourt search for "', pkg, '"\n')), file=f)
            print('To install:', file=f)
            packages_string = ' '.join(packages)
            print(' '.join(('yaourt -Sa', packages_string)), file=f)
            print('\nPackage list:', file=f)
            for p in packages:
                print(p, file=f)
        # Open file.
        call(('xdg-open', filename))
    
    if install:
        # Install packages with yaourt.
        for p in packages:
            print(''.join(('\n\033[1;32m==> ', '\033[1;37m', p,
                           '\033[0m')))
            Popen(['yaourt', '-Sa', '%s' % p]).communicate()
    

    最佳答案

    您刚刚重新发明了轮子。 pacmanpackeryaourt都具有-q标志。

    例如:

    yaourt -Ssq coreutils
    

    结果:
    coreutils
    busybox-coreutils
    coreutils-git
    coreutils-icp
    coreutils-selinux
    coreutils-static
    cv
    cv-git
    ecp
    gnu2busybox-coreutils
    gnu2plan9-coreutils
    gnu2posix2001-coreutils
    gnu2sysv-coreutils
    gnu2ucb-coreutils
    policycoreutils
    selinux-usr-policycoreutils-old
    smack-coreutils
    xml-coreutils
    

    关于linux - 如何从Pacman/Yaourt搜索返回包裹 list ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27586964/

    10-11 21:22