# # SWIG interface generator # # N. Devillard - March 2000 # # This script does the following: # # - Parse all header files included in ../include/eclipse.h to find # out which symbols need to be exported to Python. # - Add definitions from qfits.i # - Create an interface file to SWIG from that: c_eclipse.i # - Run SWIG on the interface file to generate c_eclipse_wrap.c # # The recognized tokens in header files are: # Start: any line containing '' # Stop : any line containing '' # All the code in between these lines (not including them) is # reproduced in the generated c_eclipse.i file. # import string, os elib = '../../../src/' def get_qfits_dir(conf_make): lines = open(conf_make, 'r').readlines() for line in lines: key, val = line.split('=') if key.strip() == 'QFITSDIR': return val.strip() qlib = get_qfits_dir('../../../config.make') def parse_eclipse_h(pathname): print 'Parsing eclipse.h ...' lines = open(os.path.join(pathname, 'eclipse.h'), 'r').readlines() include_files = [] for line in lines: if line[:8] == '#include': filename = line[8:].strip().replace('"', '') include_files.append(os.path.join(pathname, filename)) return include_files def parse_include(filename): lines = open(filename, 'r').readlines() new_lines = [] do_append = 0 for line in lines: if line.find('') != -1: do_append = 0 if do_append: new_lines.append(line) if line.find('') != -1: do_append = 1 return new_lines def build_interface(): output_name = 'c_eclipse.i' interface = open(output_name, 'w') interface.write(""" /* * This file is automatically generated! * Modifications will be lost. */ %module c_eclipse %{ #include "eclipse.h" #include "qfits.h" #include #include %} %include etypemaps.i %include qfits.i """) include_files = parse_eclipse_h(os.path.join(elib, 'include')) for filename in include_files: lines = parse_include(filename) for line in lines: interface.write(line) interface.close() print 'done' def apply_swig(): print 'Applying SWIG...' os.system('swig -python c_eclipse.i') print 'done' def cleanup(): print 'Cleaning up' try: # os.remove('c_eclipse.i') os.remove('c_eclipse_wrap.doc') except OSError: pass if __name__ == '__main__': build_interface() apply_swig() cleanup()