#!/usr/bin/python
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright 2004, 2005 Red Hat, Inc.
#
# Author: Phil Knirsch
#

import sys, os

PYRPMDIR = "/usr/share/pyrpm"
if not PYRPMDIR in sys.path:
    sys.path.append(PYRPMDIR)
from pyrpm import __version__
from pyrpm import *

def usage():
    print """
    pyrpmcheckrepo [options] install [dirs | regex] update [repo] [dirs | regex]

    The dirs and regex following the install command will be used to simulate
    the original installed system.
    The first option following the update command can be a repo, rest is the
    same as for install.

options:
    [-?, --help] [--version]
    [--quiet] [-v, --verbose] [-y]
    [-c CONFIGFILE] [--dbpath DIRECTORY] [-r, --root DIRECTORY]
    [-h, --hash] [--force] [--oldpackage] [--justdb] [--test]
    [--ignoresize] [--ignorearch] [--exactarch]
    [--noconflicts] [--fileconflicts]
    [--nodeps] [--signature]
    [--noorder] [--noscripts] [--notriggers]
    [--autoerase] [--installpkgs="pkg1 pkg2 pkg2 ..."]
    [--enablerepo=repoid|repoglob] [--disablerepo=repoid|repoglob]

Warning: Some of the options are not evaluated yet"""


#
# Main program
#
def main():
    # Our yum worker object
    yum = RpmYum(rpmconfig)

    # Disabled fileconflicts per default in yum
    rpmconfig.nofileconflicts = 1

    # Default is to be a little verbose.
    rpmconfig.verbose = 1

    # We always do run in test mode
    rpmconfig.test = 1

    # No default repo
    rpmconfig.yumconf = ['/etc/nosuchconf']

    # Don't do any diskchecks
    rpmconfig.ignoresize = 1

    # Do autoerase by default
    yum.setAutoerase(1)

    # Don't ask if we really want to do this
    yum.setConfirm(0)

    # Argument parsing
    args = parseYumOptions(sys.argv[1:], yum)
    if not args:
        usage()
        return 0

    if args[0] != "install":
        print "No install command found."
        usage()
        return 0

    for pos in xrange(len(args)):
        if args[pos] == "update":
            break
    else:
        print "No update command found."
        usage()
        return 0

    instargs = args[1:pos]
    upargs = args[pos+1:]

    if os.path.isfile(upargs[0]) and not upargs[0].endswith(".rpm"):
        rpmconfig.yumconf = [upargs[0]]
        upargs = upargs[1:]

    yum.setCommand("update")
    if yum.prepareTransaction() == 0:
        return 0
    if yum.runArgs(instargs) == 0:
        return 0
    yum.runDepRes()
    if yum.runCommand() == 0:
        return 0

    installed = yum.opresolver.getList()
    for pkg in installed:
        # Error checking is not really necessary, what could go wrong?
        yum.pydb.addPkg(pkg, 1)

    yum.opresolver = RpmResolver(rpmconfig, installed)
    if yum.runArgs(upargs) == 0:
        return 0
    yum.runDepRes()
    if yum.runCommand() == 0:
        return 0
    resolver = yum.opresolver

    ihash = { }
    for pkg in installed:
        if not pkg.has_key("epoch"):
            pkg["epoch"] = (0,)
        ihash[pkg.getNEVRA()] = 1

    llist = []
    for pkg in resolver.getList():
        nevra = pkg.getNEVRA()
        if not pkg.has_key("epoch"):
            pkg["epoch"] = (0,)
        if ihash.has_key(pkg.getNEVRA()):
            llist.append(nevra)

    if len(llist) == 0:
        return 1
    llist.sort()
    print "Leftover/repoless packages:"
    for nevra in llist:
        print "\t%s" % nevra
    return 1

if __name__ == '__main__':
    if not run_main(main):
        sys.exit(1)

# vim:ts=4:sw=4:showmatch:expandtab
