]> Creatis software - gdcm.git/blob - gdcmPython/SetupOldies/distutilsWrapping.py
To allow testsuite go on.
[gdcm.git] / gdcmPython / SetupOldies / distutilsWrapping.py
1 from distutils.command.build_ext import build_ext
2 from distutils.core import Extension
3 from distutils.file_util import copy_file
4 from types import ListType
5 import os
6 import string
7
8 class build_extWrap(build_ext):
9         """
10         This distutils command is meant to be used with all wrapper defined for
11         this format.
12         To realize it,we can't have command-line parameters
13         """
14         def build_extension(self,ext):
15                 # command-line arguments prevail over extension arguments
16                 # but if no command-line argument is defined,extension argument is
17                 # taken into account
18                 self.__ext=ext
19                 build_ext.build_extension(self,ext)
20
21                 if(os.name!='posix'):
22                         # Copy the .lib file
23                         fullname = self.get_ext_fullname(ext.name)
24                         modpath = string.split(fullname, '.')
25                         package = string.join(modpath[0:-1], '.')
26                         base = modpath[-1]
27                         if self.inplace:
28                                 # ignore build-lib -- put the compiled extension into
29                                 # the source tree along with pure Python modules
30                                 build_py = self.get_finalized_command('build_py')
31                                 package_dir = build_py.get_package_dir(package)
32                                 dstLib=package_dir
33                         else:
34                                 dstLib=self.build_lib
35         
36                         srcLib=os.path.join(self.build_temp,base+".lib")
37                         dstLib=os.path.join(dstLib,package)
38         
39                         copy_file(srcLib,dstLib)
40
41         def swig_sources(self,sources):
42                 """Walk the list of source files in 'sources',looking for SWIG
43                 interface(.i) files.  Run SWIG on all that are found,and
44                 return a modified 'sources' list with SWIG source files replaced
45                 by the generated C(or C++) files.
46                 """
47                 try:
48                         return(self.__ext.wrapper.WrapSources(self,self.__ext,sources))
49                 except Exception,e:
50                         print Exception,e
51                         self.announce("Warning: wrapping error")
52                         return(sources)
53
54 class Wrapper:
55         def WrapSources(self,distutil,extWrap,sources):
56                 pass
57
58 class ExtensionWrap(Extension):
59         """
60         This class extends basic distutils Extension class,adding two keyword
61         arguments :
62                 * swig_cpp,which triggers -c++ mode when swigging
63                 * swig_include,which specifies -I flag when swigging
64         This class is meant to be build with mybuild_ext distutils command.
65         """
66         def __init__(self,wrapper=None,**args):
67                 Extension.__init__(self,**args)
68
69                 self.wrapper=wrapper
70
71 """
72 Example of use of these classes in distutils setup method :
73
74 from Transfert.tcDistUtils import mybuild_ext,MyExtension
75 setup(name="xxx",
76                 version="X.Y",
77                 description="blah blah blah",
78                 author="John Doe",
79                 author_email="i.hate@spam.com",
80                 url="http://www.fakeurl.com",
81                 packages=["yyy"],
82                 cmdclass={'build_ext':build_extWrap},# redirects default build_ext
83                 ext_modules=[ExtensionWrap(name="src/xxx,# instance of our Extension class
84                                                                                    sources=["src/xxx.cpp",
85                                                                                                          "src/xxx.i"],
86                                                                                    include_dirs=["/usr/include/python2.1",
87                                                                                                                         "/usr/include/vtk"],
88                                                                                    libraries=["vtkGraphics"],
89                                                                                   )
90                                                 ]
91                 )
92                 
93 and then run "python setup.py build"...
94 """