#!/usr/bin/env python
#---------------------------------------------------------------
# Project         : Linux-Mandrake
# Module          : rpm-rebuilder
# File            : check-distrib
# Version         : $Id: check-distrib,v 1.4 2000/10/01 17:59:46 flepied Exp $
# Author          : Frederic Lepied
# Created On      : Tue May 16 06:55:15 2000
#---------------------------------------------------------------

import os
import rpm
import sys

def check(srcdir, bindir, arch):
    srpms = os.listdir(srcdir)
    rpms = os.listdir(bindir)
    invalid_srpms = srpms[:]

    for p in rpms:
        try:
            # Create a package object from the file name
            fd = os.open(bindir + "/" + p, os.O_RDONLY)
            (header, is_source) = rpm.headerFromPackage(fd)
            os.close(fd)
        except:
            print "E: invalid-file", p
            continue
        
	try:
            src = header[rpm.RPMTAG_SOURCERPM]
            if src in srpms:
                try:
                    invalid_srpms.remove(src)
                except ValueError:
                    pass
            else:
                print "E: no-source", p
        except:
            print "E: invalid-binary", p

    for p in invalid_srpms:
        try:
            # Create a package object from the file name
            fd = os.open(srcdir + "/" + p, os.O_RDONLY)
            (header, is_source) = rpm.headerFromPackage(fd)
            os.close(fd)
        except:
            print "E: invalid-file", p
            continue

        try:
            exclusivearch = header[rpm.RPMTAG_EXCLUSIVEARCH]
            if exclusivearch == None or arch in exclusivearch:
                print "E: no-binary", p
        except:
            print "E: invalid-source", p

if len(sys.argv) != 4:
    print "usage:", sys.argv[0], "<src dir> <bin dir> <arch>"
    print "check coherence of binary and source rpms"
    sys.exit(1)
    
check(sys.argv[1], sys.argv[2], sys.argv[3])

# Local variables:
# mode: python
# End:
#
# check-distrib ends here
