#!/usr/bin/python

import sys

from gi.repository import PackageKitGlib as packagekit
from gi.repository import GObject

if len(sys.argv) != 2:
    print >>sys.stderr, 'Usage: %s PACKAGE' % sys.argv[0]
    sys.exit(1)

client = packagekit.Client()

pfilter = 1 << packagekit.FilterEnum.NOT_SOURCE
pkgname = sys.argv[1]

OUTPUT_COUNT = 0
def debug_output(pkgs):
    global OUTPUT_COUNT
    OUTPUT_COUNT += 1

    print "[output %d] length=%d" % (OUTPUT_COUNT, len(pkgs))
    i = 0
    for pkg in pkgs:
        print "[output %d] %d: id=%s" % (OUTPUT_COUNT, i, id(pkg))
        print "[output %d] %d: obj=%s" % (OUTPUT_COUNT, i, pkg)
        i += 1

def progress(client, progress, ptype, data=None):
    pass

def get_pkgs(pkgname):
    pkgs = []

    try:
        result = client.search_names(pfilter, (pkgname,), None, progress, None)
    except GObject.GError as e:
        print 'Error while looking for %s: %s' % (pkgname, e)
    else:
        pkgs = result.get_package_array()

    debug_output(pkgs)

    return pkgs

pkgs = get_pkgs(pkgname)

debug_output(pkgs)

if pkgs:
    print 'Found %d package(s):' % len(pkgs)
    print '\n'.join([' ' + pkg.get_property('package-id') for pkg in pkgs])
else:
    print 'Found no package for %s.' % pkgname
