blob: 4a71da1dff4fe4a1563cee3333a1965fea2c4e61 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001#
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 Tice5790e062010-10-28 21:51:20 +00006# 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 Lattner24943d22010-06-08 16:52:24 +000021#
22
23
24import os
25
Caroline Tice5790e062010-10-28 21:51:20 +000026input_dir_name = os.environ["SRCROOT"]
27full_input_name = input_dir_name + "/source/LLDBWrapPython.cpp"
Chris Lattner24943d22010-06-08 16:52:24 +000028full_output_name = full_input_name + ".edited"
29
30try:
31 f_in = open (full_input_name, 'r')
32except IOError:
33 print "Error: Unable to open file for reading: " + full_input_name
34else:
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 Tice5790e062010-10-28 21:51:20 +000040 include_line_found = False
Chris Lattner24943d22010-06-08 16:52:24 +000041
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 Tice5790e062010-10-28 21:51:20 +000050 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 Lattner24943d22010-06-08 16:52:24 +000062 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"