Salome HOME
Initialisation de la base KERNEL avec la version operationnelle de KERNEL_SRC issue...
[modules/kernel.git] / src / SALOME_SWIG / salome_shared_modules.py
1 from SALOME_utilities import *
2
3 """
4 """
5 MESSAGE( "Module salome_shared_modules" )
6
7 modules={}
8 try:
9   # We try to import PyQt module. If present we import it
10   # as a "shared" module
11   import qt
12   modules["qt"]=qt.__dict__
13 except:
14   pass
15
16 # We keep in modules a copy of dictionnary modules
17 # that need to be imported only once in multi-study context
18
19 # Specific case : omniORB
20 import omniORB
21 modules["omniORB"]=omniORB.__dict__.copy()
22 import omniORB.CORBA
23 modules["omniORB.CORBA"]=omniORB.CORBA.__dict__.copy()
24 modules["CORBA"]=modules["omniORB.CORBA"]
25 import CosNaming
26 modules["CosNaming"]=CosNaming.__dict__.copy()
27 # end omniORB
28
29 #
30 # We search all Python CORBA (omniorb) modules.
31 # A Python CORBA module has 2 associated Python packages 
32 # These packages are named : <module_name> and <module_name>__POA
33 #
34
35
36 # SALOMEDS must be imported first, at least before any CORBA module
37 # that references it.
38 # It seems that import order of related CORBA modules is important
39 # Perhaps, it's not sufficient so you should complete the list ???
40 #
41 import SALOMEDS
42 import Engines
43
44 import glob,os,sys
45
46 repertoire=os.path.dirname(__file__)
47 path=[repertoire,]
48
49 SALOME_ROOT_DIR = os.getenv("SALOME_ROOT_DIR")
50 if SALOME_ROOT_DIR != None:
51         path.append(os.path.join(SALOME_ROOT_DIR,"lib","python"+sys.version[:3],"site-packages","salome"))
52
53 SALOME_SITE_DIR = os.getenv("SALOME_SITE_DIR")
54 if SALOME_SITE_DIR != None:
55         SALOME_SITE_NAME = os.getenv("SALOME_SITE_NAME")
56         if SALOME_SITE_NAME != None:
57                 path.append(os.path.join(SALOME_SITE_DIR,"lib","python"+sys.version[:3],"site-packages",SALOME_SITE_NAME))
58
59 #path=[repertoire,
60 #      os.path.join(repertoire,"..","lib","python"+sys.version[:3],"site-packages","salome"),
61 #      os.path.join(SALOME_ROOT_DIR,"lib","python"+sys.version[:3],"site-packages","salome"),
62 #      os.path.join(SALOME_SITE_DIR,"lib","python"+sys.version[:3],"site-packages","salome"),
63 #     ]
64
65 MESSAGE( str(path) )
66
67 for rep in path:
68    # Add rep directory in the Python path to be able to import modules 
69    sys.path[:0]=[rep]
70    listdir=glob.glob(os.path.join(rep,"*__POA"))
71    for elem in listdir:
72       if os.path.isdir(elem):
73          # Found a directory (Python package) named *__POA 
74          module__POA=os.path.basename(elem)
75          module=module__POA[:-5]
76          MESSAGE( "Import CORBA module: " + module + ".\n Directory: " + os.path.abspath(elem)[:-5] )
77          mod=__import__(module)
78          # force the reload of CORBA module to resolve all the include relations between modules
79          # specific of omniORBpy implementation (1.5)
80          reload(mod)
81          modules[module]=mod.__dict__.copy()
82    # Now we import modules found in shared_modules directory
83    r=os.path.join(rep,"shared_modules")
84    MESSAGE( r )
85    if os.path.isdir(r):
86       sys.path[:0]=[r]
87       listfich=glob.glob(os.path.join(r,"*.py"))
88       MESSAGE( str(listfich) )
89       for m in listfich:
90          module=os.path.basename(m)[:-3]
91          MESSAGE( "Import module: " + module + ".\n Location: " + os.path.abspath(m) )
92          mod=__import__(module)
93          modules[module]=mod.__dict__.copy()
94       # Don't keep r directory in the path to not pollute it
95       del sys.path[0]
96
97    # Don't keep rep directory in the path to not pollute it
98    del sys.path[0]
99
100 # End of CORBA modules import
101
102 def import_shared_modules(sysmodules):
103    """
104       This function "imports" shared modules contained in modules dictionnary
105       in sysmodules.
106       All these modules are only copied and not completely imported (not executed)
107    """
108    # EDF-CCAR: 
109    # Problem with omniORB : omniORB creates a C Python module named  _omnipy
110    # this module has sub-modules : omni_func, ...
111    # _omnipy is quite a package but import with Python sub-interpreters does not seem to work 
112    # To make it work we need to add those sub-modules in sysmodules
113    import _omnipy
114    sysmodules["_omnipy.omni_func"]=_omnipy.omni_func
115    sysmodules["_omnipy.poa_func"]=_omnipy.poa_func
116    sysmodules["_omnipy.poamanager_func"]=_omnipy.poa_func
117    sysmodules["_omnipy.orb_func"]=_omnipy.orb_func
118
119    import imp
120
121    # All modules in the modules dictionnary are only copied, not completely imported 
122    for nom_module,module_dict in modules.items():
123       if sysmodules.has_key(nom_module):continue
124       m=imp.new_module(nom_module)
125       m.__dict__.update(module_dict)
126       sysmodules[nom_module]=m
127