How to Return List of Packages from Pacman/Yaourt Search

11,430

You've just re-invented the wheel. pacman, packer and yaourt all have the -q flag.

For example:

yaourt -Ssq coreutils

Results:

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
Share:
11,430
GreenRaccoon23
Author by

GreenRaccoon23

I know 6 verbal languages and 4-ish programming languages (Go, JavaScript, Python, and Bash), plus SQL, HTML, and CSS. Some JS frameworks I've used include Node, jQuery, Backbone, Mithril, Angular, and React. My best language is JavaScript/Node, and I do a lot of personal programming in Go. I like JavaScript/Node because it's like reading and writing a book, which is my favorite and IMO the most important part of programming. I use Go when I want something insanely fast. I haven't gotten around to learning C or C++ because I haven't found a good enough reason to warrant the time and effort needed to write a program in C++ when I can use Node or Go. It's like if C++ is a Bugatti and Go is a Mercedes, yeah the Bugatti is faster but I'd rather get the Mercedes for 1/5 of the price. -EDIT- Technically, a Mercedes is only 1/200 of the price, which doesn't fit the analogy, but is 12% slower, which does fit the analogy. Linux is my preferred environment, but I like and use Mac and Windows too. I mainly use Arch Linux, but I use Ubuntu in some environments. I switch back and forth from Bash and ZSH because I can't decide on one.

Updated on June 16, 2022

Comments

  • GreenRaccoon23
    GreenRaccoon23 about 2 years

    ----EDIT----
    Changed the name of the script from pacsearch to pacdot.
    Apparently yaourt -Ssaq does this, so this script isn't as necessary as I thought. Although, I still find using pacdot -w to open the results in a text document helpful.
    ----/EDIT----

    This isn't a question, but I thought someone else might find this useful. Someone may end up on stackoverflow trying to find a solution like this.

    On Arch Linux, I keep finding myself searching with pacman or yaourt and wishing I could get just the package names, not all of the extra stuff. For example, I'd love to be able to run yaourt -Sa $(yaourt -Ssa package). Oddly enough, pacman and yaourt don't seem have this option (not that I can tell, at least), so I wrote a python script to do it. Copy it if you'd like. You can name it what you want, but I'll refer to it as pacdot.py.

    pacdot.py package will be like yaourt -Ssa package but only list the package names.

    I added a few extra options:

    • pacdot.py -o package will only list results from the official Arch repositories, not the AUR.
    • pacdot.py -i package will install all the found packages. If you've ever thought about running something like yaourt -Sa $(yaourt -Ssa package), that's what this command does.

    • pacdot.py -w package will:

      1. Create a file called 'the-package-you-searched.txt',
      2. Write an example command that would install the found packages,
        (yaourt -Sa all-of-the-results),
      3. Write each result on a new line, and
      4. Open the file for you (with your default text editor).

    Here's the code:

    #!/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()
    
  • GreenRaccoon23
    GreenRaccoon23 over 9 years
    Well that's unfortunate haha. I'm not sure how I've never noticed that -q argument before. At least pacsearch -i is less messy than yaourt -Sa $(yaourt -Ssaq), so I still have a use for the script. Opening the results in a text document is helpful too.
  • Steve
    Steve over 9 years
    @GreenRaccoon23: There's a page on pacman tips in the Arch wiki that's worth a read. If you're after a 'less messy' solution, but still want the convenience of a wrapper, you'd be better off just using a shell function or alias instead. And if you want to open the results in a text document, why not just pipe to vim?
  • Steve
    Steve over 9 years
    @GreenRaccoon23: Also, pacsearch is already part of core/pacman, so you may want to consider changing the name of your tool.