#!/usr/bin/python
#
# Get a list of packages which are in the header list but aren't in the
# comps file explicitly or for dependencies
#
# Jeremy Katz <katzj@redhat.com>
#
# Copyright 2002, Red Hat, Inc.
#
# GPL blurb here

import os,sys,string
import rhpl.comps
import rpm

def usage():
    print "Usage: %s /path/to/tree arch [basepath]" % (sys.argv[0],)

def main():
    if len(sys.argv) < 3:
        usage()
        sys.exit(0)

    treedir = sys.argv[1]
    arch = sys.argv[2]
    if len(sys.argv) > 3:
        prod = sys.argv[3]
    else:
        prod = "RedHat/base"
    basedir = "%s/%s/%s/" % (treedir, arch, prod)

    hdlist = rpm.readHeaderListFromFile("%s/hdlist" %(basedir,))
    comps = rhpl.comps.Comps("%s/comps.xml" % (basedir,))
    usedpkgs = {}
    for group in comps.groups.values():
        new = group.packages
        while len(new) > 0:
            pkgs = new
            new = []
            for name in pkgs:
                if usedpkgs.has_key(name):
                    continue

                # if we don't have the key, go ahead and check it
                # and add its deps to new
                usedpkgs[name] = 1
                for pkg in comps.packages[name].dependencies:
                    if not usedpkgs.has_key(pkg) and pkg not in new:
                        new.append(pkg)

    for pkg in hdlist:
        if pkg[rpm.RPMTAG_NAME] not in usedpkgs.keys():
            print pkg[rpm.RPMTAG_NAME]
            
    

if __name__ == "__main__":
    main()
