Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 |
Caroline Tice | 5790e06 | 2010-10-28 21:51:20 +0000 | [diff] [blame] | 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. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | # |
| 22 | |
| 23 | |
| 24 | import os |
| 25 | |
Caroline Tice | 5790e06 | 2010-10-28 21:51:20 +0000 | [diff] [blame] | 26 | input_dir_name = os.environ["SRCROOT"] |
| 27 | full_input_name = input_dir_name + "/source/LLDBWrapPython.cpp" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | full_output_name = full_input_name + ".edited" |
| 29 | |
| 30 | try: |
| 31 | f_in = open (full_input_name, 'r') |
| 32 | except IOError: |
| 33 | print "Error: Unable to open file for reading: " + full_input_name |
| 34 | else: |
| 35 | try: |
| 36 | f_out = open (full_output_name, 'w') |
| 37 | except IOError: |
| 38 | print "Error: Unable to open file for writing: " + full_output_name |
| 39 | else: |
Caroline Tice | 5790e06 | 2010-10-28 21:51:20 +0000 | [diff] [blame] | 40 | include_line_found = False |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | |
| 42 | try: |
| 43 | line = f_in.readline() |
| 44 | except IOError: |
| 45 | print "Error occurred while reading file." |
| 46 | else: |
| 47 | while line: |
| 48 | # |
| 49 | # |
Caroline Tice | 5790e06 | 2010-10-28 21:51:20 +0000 | [diff] [blame] | 50 | if not include_line_found: |
| 51 | if (line.find ("#include <Python.h>") == 0): |
| 52 | f_out.write ("#if defined (__APPLE__)\n"); |
| 53 | f_out.write ("#include <Python/Python.h>\n"); |
| 54 | f_out.write ("#else\n"); |
| 55 | f_out.write (line); |
| 56 | f_out.write ("#endif\n"); |
| 57 | include_line_found = True |
| 58 | else: |
| 59 | f_out.write (line) |
| 60 | else: |
| 61 | f_out.write (line) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | try: |
| 63 | line = f_in.readline() |
| 64 | except IOError: |
| 65 | print "Error occurred while reading file." |
| 66 | |
| 67 | try: |
| 68 | f_in.close() |
| 69 | f_out.close() |
| 70 | except: |
| 71 | print "Error occurred while closing files" |