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

时间:2014-12-21 05:22:37

标签: linux python-3.x archlinux

---- ---- EDIT
已将脚本名称从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将:

      
        
    1. 创建一个名为' the-package-you-searching.txt',
    2. 的文件   
    3. 编写一个示例命令,用于安装找到的软件包,
         (yaourt -Sa所有结果),
    4.   
    5. 将每个结果写在新行上,
    6.   
    7. 为您打开文件(使用默认文本编辑器)。
    8.   

以下是代码:

#!/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()

1 个答案:

答案 0 :(得分:6)

你刚刚重新发明了轮子。 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