#!/usr/bin/python

import os
import sys
from debian_bundle.debfile import DebFile

def check_file(f):
    try:
        deb = DebFile(f)
    except:
        print >>sys.stderr, "Error scanning %s: %s" % (f, sys.exc_info()[1])
        return True
    else:
        if 'md5sums' in deb.control:
            return True
        else:
            # No md5sums, check that the package has regular files
            # (if not, it's normal not to have md5sums).
            has_files = (True in [m.isfile() for m
                                  in deb.data.tgz().getmembers()])
            return not has_files

def check_dir(dir):
    ok = nok = 0
    for root, dirs, files in os.walk(dir):
        for file in [f for f in files if f.endswith("_amd64.deb")
                     or f.endswith("_all.deb")]:
            if check_file(os.path.join(root, file)):
                ok += 1
            else:
                print file.split('_')[0]
                nok += 1
    return ok, nok

if __name__ == '__main__':
    print >>sys.stderr, "%d files ok, %d files not ok" % check_dir(sys.argv[1])
