import os, os.path, pdb
from functools import partial
import utils
import config
import class_parser

def build_python_bindings(target, source, env, initialization=''):
    """ A command to generate python bindings """
    module_name = os.path.splitext(os.path.basename(target[0].name))[0]
    utils.warn("Generating automatic python bindings for module %s" % module_name)

    p = class_parser.HeaderParser(module_name, verbose=config.V)
    p.module.init_string = initialization
    p.parse_filenames([s.get_abspath() for s in source])

    fd = open(target[0].get_abspath(), 'w')
    p.write(fd)
    fd.close()



nenv = utils.ExtendedEnvironment()

BOUND_FILES = Split("""
    ../include/regfi.h
    regfi/pyregfi.h
    """)

nenv.config = config
if config.DEBUG:
    nenv.Append(CFLAGS = "-std=gnu99 -pedantic -Wall -fPIC -ggdb -O0")
    nenv.Append(CPPPATH=['../include', 'include'])
    nenv.python_cppflags = '-I/usr/include/python2.5_d'
else:
    nenv.Append(CFLAGS = "-std=gnu99 -pedantic -Wall -fPIC")
    nenv.Append(CPPPATH=['../include', 'include'])
    
nenv.Append(LIBPATH="../lib")
nenv.Append(LINKFLAGS="")
# XXX: why should I need to call regfi_init() when it should be called only once automatically? 
nenv.Command('regfi/pyregfi.c', BOUND_FILES, partial(build_python_bindings,
                                                     initialization='pyregfi_init();regfi_init();'))
nenv.Depends('regfi/pyregfi.c', 'class_parser.py')

nenv.PythonModule("pyregfi", ['regfi/pyregfi.c', 'regfi/regfi.c', 'regfi/class.c', 'regfi/error.c'],
                  LIBS=['regfi', 'talloc'])
