Last active
September 15, 2016 09:45
-
-
Save fujii/b355be46e9aec9a62377df5f80fcbdeb to your computer and use it in GitHub Desktop.
dnf-search.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
import sys | |
import subprocess | |
import functools | |
from optparse import OptionParser | |
def dnf_search(keyword): | |
SEPARATOR = ' : ' | |
text = subprocess.check_output(['dnf', 'search', '--all', keyword]).decode('utf-8') | |
prev_name = '' | |
packages = [] | |
descriptions = {} | |
for line in text.splitlines()[2:]: | |
if (line[0].isspace()): | |
descriptions[prev_name].append(line) | |
else: | |
name, _ = line.split(SEPARATOR, 1) | |
packages.append(name); | |
descriptions[name] = [line] | |
prev_name = name; | |
return packages, descriptions | |
def and_filter(x, y): | |
return [ i for i in x if i in y ] | |
def main(): | |
usage = "Usage: %prog [options] PATTERN ..." | |
parser = OptionParser(usage=usage) | |
options, args = parser.parse_args() | |
if len(args) < 1: | |
parser.print_help() | |
exit(1) | |
try: | |
packages, descriptions = dnf_search(sys.argv[1]) | |
matches = [ dnf_search(x)[0] for x in sys.argv[2:] ] | |
packages = sorted(functools.reduce(and_filter, matches, packages)) | |
for p in packages: | |
for d in descriptions[p]: | |
print(d) | |
except subprocess.CalledProcessError: | |
return 1; | |
return 0; | |
if __name__ == '__main__': | |
exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment