Salome HOME
062f1d4506bad0124c2b337fd99bbd16391f2875
[modules/kernel.git] / src / SALOME_SWIG / salome_shared_modules.py
1 #  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3
4 #  This library is free software; you can redistribute it and/or 
5 #  modify it under the terms of the GNU Lesser General Public 
6 #  License as published by the Free Software Foundation; either 
7 #  version 2.1 of the License. 
8
9 #  This library is distributed in the hope that it will be useful, 
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 #  Lesser General Public License for more details. 
13
14 #  You should have received a copy of the GNU Lesser General Public 
15 #  License along with this library; if not, write to the Free Software 
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17
18 #  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
19 #
20 #
21 #
22 #  File   : salome_shared_modules.py
23 #  Module : SALOME
24
25 from SALOME_utilities import *
26
27 """
28 """
29 MESSAGE( "Module salome_shared_modules" )
30
31 modules={}
32 try:
33   # We try to import PyQt module. If present we import it
34   # as a "shared" module
35   import qt
36   modules["qt"]=qt.__dict__
37 except:
38   pass
39
40 # We keep in modules a copy of dictionnary modules
41 # that need to be imported only once in multi-study context
42
43 # Specific case : omniORB
44 import omniORB
45 modules["omniORB"]=omniORB.__dict__.copy()
46 import omniORB.CORBA
47 modules["omniORB.CORBA"]=omniORB.CORBA.__dict__.copy()
48 modules["CORBA"]=modules["omniORB.CORBA"]
49 import CosNaming
50 modules["CosNaming"]=CosNaming.__dict__.copy()
51 # end omniORB
52
53 #
54 # We search all Python CORBA (omniorb) modules.
55 # A Python CORBA module has 2 associated Python packages 
56 # These packages are named : <module_name> and <module_name>__POA
57 #
58
59
60 # SALOMEDS must be imported first, at least before any CORBA module
61 # that references it.
62 # It seems that import order of related CORBA modules is important
63 # Perhaps, it's not sufficient so you should complete the list ???
64 #
65 import SALOMEDS
66 import Engines
67
68 import glob,os,sys
69
70 repertoire=os.path.dirname(__file__)
71 path=[repertoire,]
72
73 SALOME_ROOT_DIR = os.getenv("SALOME_ROOT_DIR")
74 if SALOME_ROOT_DIR != None:
75         path.append(os.path.join(SALOME_ROOT_DIR,"lib","python"+sys.version[:3],"site-packages","salome"))
76
77 SALOME_SITE_DIR = os.getenv("SALOME_SITE_DIR")
78 if SALOME_SITE_DIR != None:
79         SALOME_SITE_NAME = os.getenv("SALOME_SITE_NAME")
80         if SALOME_SITE_NAME != None:
81                 path.append(os.path.join(SALOME_SITE_DIR,"lib","python"+sys.version[:3],"site-packages",SALOME_SITE_NAME))
82
83 #path=[repertoire,
84 #      os.path.join(repertoire,"..","lib","python"+sys.version[:3],"site-packages","salome"),
85 #      os.path.join(SALOME_ROOT_DIR,"lib","python"+sys.version[:3],"site-packages","salome"),
86 #      os.path.join(SALOME_SITE_DIR,"lib","python"+sys.version[:3],"site-packages","salome"),
87 #     ]
88
89 MESSAGE( str(path) )
90
91 for rep in path:
92    # Add rep directory in the Python path to be able to import modules 
93    sys.path[:0]=[rep]
94    listdir=glob.glob(os.path.join(rep,"*__POA"))
95    for elem in listdir:
96       if os.path.isdir(elem):
97          # Found a directory (Python package) named *__POA 
98          module__POA=os.path.basename(elem)
99          module=module__POA[:-5]
100          MESSAGE( "Import CORBA module: " + module + ".\n Directory: " + os.path.abspath(elem)[:-5] )
101          mod=__import__(module)
102          # force the reload of CORBA module to resolve all the include relations between modules
103          # specific of omniORBpy implementation (1.5)
104          reload(mod)
105          modules[module]=mod.__dict__.copy()
106    # Now we import modules found in shared_modules directory
107    r=os.path.join(rep,"shared_modules")
108    MESSAGE( r )
109    if os.path.isdir(r):
110       sys.path[:0]=[r]
111       listfich=glob.glob(os.path.join(r,"*.py"))
112       MESSAGE( str(listfich) )
113       for m in listfich:
114          module=os.path.basename(m)[:-3]
115          MESSAGE( "Import module: " + module + ".\n Location: " + os.path.abspath(m) )
116          mod=__import__(module)
117          modules[module]=mod.__dict__.copy()
118       # Don't keep r directory in the path to not pollute it
119       del sys.path[0]
120
121    # Don't keep rep directory in the path to not pollute it
122    del sys.path[0]
123
124 # End of CORBA modules import
125
126 def import_shared_modules(sysmodules):
127    """
128       This function "imports" shared modules contained in modules dictionnary
129       in sysmodules.
130       All these modules are only copied and not completely imported (not executed)
131    """
132    # EDF-CCAR: 
133    # Problem with omniORB : omniORB creates a C Python module named  _omnipy
134    # this module has sub-modules : omni_func, ...
135    # _omnipy is quite a package but import with Python sub-interpreters does not seem to work 
136    # To make it work we need to add those sub-modules in sysmodules
137    import _omnipy
138    sysmodules["_omnipy.omni_func"]=_omnipy.omni_func
139    sysmodules["_omnipy.poa_func"]=_omnipy.poa_func
140    sysmodules["_omnipy.poamanager_func"]=_omnipy.poa_func
141    sysmodules["_omnipy.orb_func"]=_omnipy.orb_func
142
143    import imp
144
145    # All modules in the modules dictionnary are only copied, not completely imported 
146    for nom_module,module_dict in modules.items():
147       if sysmodules.has_key(nom_module):continue
148       m=imp.new_module(nom_module)
149       m.__dict__.update(module_dict)
150       sysmodules[nom_module]=m
151