【问题标题】:How to Return List of Packages from Pacman/Yaourt Search如何从 Pacman/Yaourt 搜索返回包列表
【发布时间】:2014-12-23 03:26:08
【问题描述】:

----编辑----
将脚本名称从 pacsearch 更改为 pacdot
显然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-searched.txt”的文件,
    2. 编写一个示例命令来安装找到的包,
      (yaourt -Sa 所有结果),
    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()

【问题讨论】:

    标签: linux python-3.x archlinux


    【解决方案1】:

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

    【讨论】:

    • 那很不幸哈哈。我不确定我以前是怎么注意到 -q 参数的。至少pacsearch -i没有yaourt -Sa $(yaourt -Ssaq)那么乱,所以脚本还是有用的。在文本文档中打开结果也很有帮助。
    • @GreenRaccoon23:在 Arch wiki 中有一个关于 pacman tips 的页面值得一读。如果您想要一个“不那么凌乱”的解决方案,但仍然想要包装器的便利,那么最好使用 shell functionalias 代替。如果你想在文本文档中打开结果,为什么不直接pipe to vim
    • @GreenRaccoon23:另外,pacsearch 已经是 core/pacman 的一部分,因此您可能需要考虑更改工具的名称。
    猜你喜欢
    • 2019-02-15
    • 2013-04-16
    • 1970-01-01
    • 2013-04-25
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    相关资源
    最近更新 更多