Salome HOME
NRI : Modification.
[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 KERNEL_ROOT_DIR = os.getenv("KERNEL_ROOT_DIR")
50 if KERNEL_ROOT_DIR != None:
51         path.append(os.path.join(KERNEL_ROOT_DIR,"lib","python"+sys.version[:3],"site-packages","salome"))
52
53
54 #
55 import SALOME_ModuleCatalog
56 from SALOME_NamingServicePy import * 
57 orb = CORBA.ORB_init([''], CORBA.ORB_ID)
58 ns = SALOME_NamingServicePy_i(orb)
59 modulecatalog = ns.Resolve('/Kernel/ModulCatalog')
60 compos = []
61 compos = modulecatalog.GetComponentList()
62
63 for name in compos:
64         print name
65         MODULE_ROOT_DIR = os.getenv( name + "_ROOT_DIR" )
66         print MODULE_ROOT_DIR
67
68         if MODULE_ROOT_DIR != None:
69                 path.append(os.path.join(MODULE_ROOT_DIR,"lib","python"+sys.version[:3],"site-packages","salome"))
70
71 #SALOME_ROOT_DIR = os.getenv("SALOME_ROOT_DIR")
72 #if SALOME_ROOT_DIR != None:
73 #       path.append(os.path.join(SALOME_ROOT_DIR,"lib","python"+sys.version[:3],"site-packages","salome"))
74
75 #SALOME_SITE_DIR = os.getenv("SALOME_SITE_DIR")
76 #if SALOME_SITE_DIR != None:
77 #        SALOME_SITE_NAME = os.getenv("SALOME_SITE_NAME")
78 #        if SALOME_SITE_NAME != None:
79 #               path.append(os.path.join(SALOME_SITE_DIR,"lib","python"+sys.version[:3],"site-packages",SALOME_SITE_NAME))
80
81 MESSAGE( str(path) )
82
83 for rep in path:
84    # Add rep directory in the Python path to be able to import modules 
85    sys.path[:0]=[rep]
86    listdir=glob.glob(os.path.join(rep,"*__POA"))
87    for elem in listdir:
88       if os.path.isdir(elem):
89          # Found a directory (Python package) named *__POA 
90          module__POA=os.path.basename(elem)
91          module=module__POA[:-5]
92          MESSAGE( "Import CORBA module: " + module + ".\n Directory: " + os.path.abspath(elem)[:-5] )
93          mod=__import__(module)
94          # force the reload of CORBA module to resolve all the include relations between modules
95          # specific of omniORBpy implementation (1.5)
96          reload(mod)
97          modules[module]=mod.__dict__.copy()
98    # Now we import modules found in shared_modules directory
99    r=os.path.join(rep,"shared_modules")
100    MESSAGE( r )
101    if os.path.isdir(r):
102       sys.path[:0]=[r]
103       listfich=glob.glob(os.path.join(r,"*.py"))
104       MESSAGE( str(listfich) )
105       for m in listfich:
106          module=os.path.basename(m)[:-3]
107          MESSAGE( "Import module: " + module + ".\n Location: " + os.path.abspath(m) )
108          mod=__import__(module)
109          modules[module]=mod.__dict__.copy()
110       # Don't keep r directory in the path to not pollute it
111       del sys.path[0]
112
113    # Don't keep rep directory in the path to not pollute it
114    del sys.path[0]
115
116 # End of CORBA modules import
117
118 def import_shared_modules(sysmodules):
119    """
120       This function "imports" shared modules contained in modules dictionnary
121       in sysmodules.
122       All these modules are only copied and not completely imported (not executed)
123    """
124    # EDF-CCAR: 
125    # Problem with omniORB : omniORB creates a C Python module named  _omnipy
126    # this module has sub-modules : omni_func, ...
127    # _omnipy is quite a package but import with Python sub-interpreters does not seem to work 
128    # To make it work we need to add those sub-modules in sysmodules
129    import _omnipy
130    sysmodules["_omnipy.omni_func"]=_omnipy.omni_func
131    sysmodules["_omnipy.poa_func"]=_omnipy.poa_func
132    sysmodules["_omnipy.poamanager_func"]=_omnipy.poa_func
133    sysmodules["_omnipy.orb_func"]=_omnipy.orb_func
134
135    import imp
136
137    # All modules in the modules dictionnary are only copied, not completely imported 
138    for nom_module,module_dict in modules.items():
139       if sysmodules.has_key(nom_module):continue
140       m=imp.new_module(nom_module)
141       m.__dict__.update(module_dict)
142       sysmodules[nom_module]=m
143