#!/usr/bin/python

import KernelSymbolList as ks

USAGE="""%s [oldfile] [newfile] [symlist]

Where:
oldfile is the current exagrid/patches/Module.kabi_x86_64 in the kernel tree
newfile is the Module.symvers file from the kernel RPM build tree
symlist is a text file containing the list of symbols to be updated

This script will print the updated Module.kabi_x86_64 to stdout.

If symlist is a .ko file (e.g. gridfs.ko), this script will use the
external symbols which that .ko file uses as the symlist.
"""

def print_usage(name):
    print USAGE % (sys.argv[0])

if __name__ == "__main__":
    import sys
    try:
        oldfile = sys.argv[1]
        if oldfile == "--help" or oldfile == "-h":
            print_usage(sys.argv[0])
            sys.exit(0)

        newfile = sys.argv[2]
        symfile = sys.argv[3]
        if symfile[-3:] == ".ko":
            symlist = ks.make_symlist_from_kmod(symfile)
        else:
            symlist = file(symfile).read().strip().split('\n')
    except:
        print_usage(sys.argv[0])
        sys.exit(1)

    curlist = ks.SymbolList(file(oldfile).read())
    newlist = ks.SymbolList(file(newfile).read())
    updated_list = ks.update_from_list(curlist, newlist, symlist)

    print updated_list.get_sym_list()
