如何从Pacman/Yaourt搜索返回包列表

Gre*_*n23 2 linux archlinux python-3.x

----编辑----
将脚本名称从更改pacsearchpacdot.
显然yaourt -Ssaq这样做,所以这个脚本没有我想象的那么必要.虽然,我仍然发现使用pacdot -w在文本文档中打开结果很有帮助.
- - /编辑 - -

这不是一个问题,但我认为其他人可能会觉得这很有用.有人可能最终会在stackoverflow上试图找到这样的解决方案.

在Arch Linux上,我一直在寻找自己用pacman或yaourt搜索并希望我能得到包名,而不是所有额外的东西.例如,我希望能够跑步yaourt -Sa $(yaourt -Ssa package).奇怪的是,pacman和yaourt似乎没有这个选项(至少我不能说),所以我写了一个python脚本来做到这一点.如果你愿意,可以复制它.你可以把它命名为你想要的,但我会把它称为pacdot.py.

pacdot.py packageyaourt -Ssa package只是列出包名称.

我添加了一些额外的选项:

  • pacdot.py -o package 只列出官方Arch存储库的结果,而不是AUR.
  • pacdot.py -i package将安装所有找到的包.如果你曾经想过运行类似的东西yaourt -Sa $(yaourt -Ssa package),那就是这个命令的作用.

  • pacdot.py -w package 将:

    1. 创建一个名为'the-package-you-searching.txt'的文件,
    2. 编写一个可以安装找到的包的示例命令,
      (yaourt -Sa all-of-results),
    3. 将每个结果写在一个新行上,然后
    4. 为您打开文件(使用默认文本编辑器).

这是代码:

#!/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()
Run Code Online (Sandbox Code Playgroud)

Ste*_*eve 7

你刚刚重新发明了轮子.pacman,packer并且yaourt都具有-q标志.

例如:

yaourt -Ssq coreutils
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)