1import os
2import sys
3import shutil
4
5
6PIVY_HEADER = """\
7#ifdef __PIVY__
8%%include %s
9#endif
10
11"""
12
13def copy_and_swigify_headers(includedir, dirname, files):
14        """Copy the header files to the local include directories. Add an
15        #include line at the beginning for the SWIG interface files..."""
16
17        for file in files:
18            if not os.path.isfile(os.path.join(dirname, file)):
19                continue
20
21            if file[-2:] == ".i":
22                file = os.path.join(dirname, file)
23
24                file_i = file.split(os.path.sep)
25                file_i = [i for i in file_i if i != ".."]
26                file_i = os.path.join(*file_i)
27
28                file_h = file_i[:-2] + ".h"
29                from_file = os.path.join(includedir, file_h)
30
31                file_h = file[:-2] + ".h"
32                to_file = os.path.abspath(file_h)
33
34                if os.path.exists(from_file):
35                    shutil.copyfile(from_file, to_file)
36                    sys.stdout.write('create swigified header: ' + to_file + '\n')
37                    fd = open(to_file, 'r+')
38                    contents = fd.readlines()
39
40                    ins_line_nr = -1
41                    for line in contents:
42                        ins_line_nr += 1
43                        if line.find("#include ") != -1:
44                            break
45
46                    if ins_line_nr != -1:
47                        contents.insert(ins_line_nr, PIVY_HEADER % (file_i))
48                        fd.seek(0)
49                        fd.writelines(contents)
50                    else:
51                        print("[failed]")
52                        sys.exit(1)
53                    fd.close
54            # fixes for SWIG 1.3.21 and upwards
55            # (mostly workarounding swig's preprocessor "function like macros"
56            # preprocessor bug when no parameters are provided which then results
57            # in no constructors being created in the wrapper)
58            elif file[-4:] == ".fix":
59                sys.stdout.write(' ' + os.path.join(dirname, file)[:-4])
60                shutil.copyfile(os.path.join(dirname, file),
61                                os.path.join(dirname, file)[:-4])
62            # had to introduce this because windows is a piece of crap
63            elif sys.platform == "win32" and file[-6:] == ".win32":
64                sys.stdout.write(' ' + os.path.join(dirname, file)[:-6])
65                shutil.copyfile(os.path.join(dirname, file),
66                                os.path.join(dirname, file)[:-6])
67
68
69def swigify(interface_dir, include_dir):
70    dir_gen = os.walk(os.path.relpath(os.path.join(interface_dir, "Inventor")))
71    for _dir, _, names in dir_gen:
72        copy_and_swigify_headers(include_dir, _dir, names)