#!/usr/bin/python

"""Verify that all symbols listed in userfile (output of nm gridfs.ko)
are in symfile (the Module.kabi_x86_64 from the kernel tree)"""

import re

import KernelSymbolList as ks

USAGE="""%s [symfile] [module]

Where:
symfile is the current exagrid/patches/Module.kabi_x86_64 in the kernel tree
module is a Linux kernel module, probably gridfs.ko

This script will list any symbols used by the module which are NOT in
the symfile, that is the list of approved kABI symbols.
"""

if __name__ == "__main__":
    import sys
    try:
        symfile = sys.argv[1]
        if symfile == "--help" or symfile == "-h":
            print_usage(sys.argv[0])
            sys.exit(0)
        userfile = sys.argv[2]
    except:
        print USAGE % (sys.argv[0])
        sys.exit(1)

    allowed = ks.SymbolList(file(symfile).read())
    in_use_list = ks.make_symlist_from_kmod(userfile)
    nonallowed = [symbol for symbol in in_use_list \
                      if not allowed.has_symbol(symbol)]
    if len(nonallowed):
        print "Found", len(nonallowed), "symbols not in kabi list:"
        print '\n'.join(nonallowed)

    sys.exit(len(nonallowed))
