Home | History | Annotate | Download | only in Python
      1 #
      2 # edit-swig-python-wrapper-file.py
      3 #
      4 # This script performs some post-processing editing on the C++ file that
      5 # SWIG generates for python, after running on 'lldb.swig'.   In
      6 # particular, on Apple systems we want to include the Python.h file that
      7 # is used in the /System/Library/Frameworks/Python.framework, but on other
      8 # systems we want to include plain <Python.h>.  So we need to replace:
      9 #
     10 # #include <Python.h>
     11 #
     12 # with:
     13 #
     14 # #if defined (__APPLE__)
     15 # #include <Python/Python.h>
     16 # #else
     17 # #include <Python.h>
     18 # #endif
     19 #
     20 # That's what this python script does.
     21 #
     22 
     23 import os, sys
     24 
     25 include_python = '#include <Python.h>'
     26 include_python_ifdef = '''#if defined (__APPLE__)
     27 #include <Python/Python.h>
     28 #else
     29 #include <Python.h>
     30 #endif
     31 '''
     32 
     33 if len (sys.argv) > 1:
     34     input_dir_name = sys.argv[1]
     35     full_input_name = input_dir_name + "/LLDBWrapPython.cpp"
     36 else:
     37     input_dir_name = os.environ["SRCROOT"]
     38     full_input_name = input_dir_name + "/source/LLDBWrapPython.cpp"
     39 full_output_name = full_input_name + ".edited"
     40 
     41 with open(full_input_name, 'r') as f_in:
     42     with open(full_output_name, 'w') as f_out:
     43         include_python_found = False
     44         for line in f_in:
     45             if not include_python_found:
     46                 if line.startswith(include_python):
     47                     # Write out the modified lines.
     48                     f_out.write(include_python_ifdef)
     49                     include_python_found = True
     50                     continue
     51 
     52             # Otherwise, copy the line verbatim to the output file.
     53             f_out.write(line)
     54