Salome HOME
append some logs for GetCommandFromTemplate
[modules/kernel.git] / bin / orbmodule.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 ## @package orbmodule
25 # \brief Module that provides a client for %SALOME
26 #
27
28 import sys,os,time
29 import string
30 from nameserver import NamingServer
31 from omniORB import CORBA
32 from launchConfigureParser import verbose
33
34 # Import the stubs for the Naming service
35 import CosNaming
36
37 # -----------------------------------------------------------------------------
38
39 class client:
40     """Client for SALOME"""
41
42     def __init__(self,args=None):
43       # Initialise the ORB
44
45       if args is not None and 'launcher' in args:
46         pos = args['launcher'].find(":")
47         if pos != -1:
48           machine = args['launcher'][0:pos]
49           port = args['launcher'][pos+1:]
50           sys.argv.append('-ORBInitRef')
51           sys.argv.append("NameService=corbaname::" + machine + ":" + port)
52           print("Connect to naming service on machine: "+machine+" port: "+port)
53       self.orb=CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
54
55       # Initialise the Naming Service
56       self.initNS(args or {})
57
58     # --------------------------------------------------------------------------
59
60     def initNS(self,args):
61       # Obtain a reference to the root naming context
62       obj = self.orb.resolve_initial_references("NameService")
63       try:
64           self.rootContext = obj._narrow(CosNaming.NamingContext)
65           return
66       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
67           self.rootContext = None
68           if verbose(): print("Launch Naming Service++", end=' ')
69
70       # On lance le Naming Server (doit etre dans le PATH)
71       test = True
72       if args != None and 'wake_up_session' in args and args['wake_up_session']:
73         test = False
74         pass
75       if test:
76         NamingServer(args).run()
77         pass
78       print("Searching Naming Service ", end=' ')
79       ncount=0
80       delta=0.1
81       while(ncount < 100):
82           ncount += 1
83           try:
84               obj = self.orb.resolve_initial_references("NameService")
85               self.rootContext = obj._narrow(CosNaming.NamingContext)
86               break
87           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
88               self.rootContext = None
89               sys.stdout.write('+')
90               sys.stdout.flush()
91               time.sleep(delta)
92
93       if self.rootContext is None:
94           print("Failed to narrow the root naming context")
95           sys.exit(1)
96       print(" found in %s seconds " % ((ncount-1)*delta))
97
98     # --------------------------------------------------------------------------
99
100     def showNScontext(self,context,dec=''):
101       if not context:
102         print("[NS] No context")
103         return
104       else:
105         print(context)
106
107       _,bi = context.list(0)
108       if bi is not None:
109         ok,b = bi.next_one()
110         while(ok):
111             for s in b.binding_name :
112               print("%s%s.%s" %(dec,s.id,s.kind))
113               if s.kind == "dir":
114                   obj = context.resolve([s])
115                   scontext = obj._narrow(CosNaming.NamingContext)
116                   self.showNScontext(scontext,dec=dec+'  ')
117             ok,b = bi.next_one()
118
119     # --------------------------------------------------------------------------
120
121     def showNS(self):
122       """ Show the content of SALOME naming service """
123       self.showNScontext(self.rootContext)
124
125     # --------------------------------------------------------------------------
126
127     def Resolve(self, Path):
128       resolve_path = Path.split('/')
129       if resolve_path[0] == '': del resolve_path[0]
130       dir_path = resolve_path[:-1]
131       context_name = []
132       for e in dir_path:
133         context_name.append(CosNaming.NameComponent(e,"dir"))
134       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
135
136       try:
137           obj = self.rootContext.resolve(context_name)
138       except CosNaming.NamingContext.NotFound as ex:
139           obj = None
140       except CosNaming.NamingContext.InvalidName as ex:
141           obj = None
142       except CosNaming.NamingContext.CannotProceed as ex:
143           obj = None
144       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
145           obj = None
146       return obj
147
148     # --------------------------------------------------------------------------
149
150     def waitNS(self,name,typobj=None,maxcount=240):
151       count = 0
152       delta = 0.5
153       print("Searching %s in Naming Service " % name, end=' ')
154       while(1):
155           count += 1
156           if count > maxcount : raise RuntimeError("Impossible de trouver %s" % name)
157           obj = self.Resolve(name)
158           if obj :
159               print(" found in %s seconds " % ((count-1)*delta))
160               break
161           else:
162               sys.stdout.write('+')
163               sys.stdout.flush()
164               time.sleep(delta)
165
166       if typobj is None:return obj
167
168       nobj = obj._narrow(typobj)
169       if nobj is None:
170             print("%s exists but is not a %s" % (name,typobj))
171       return nobj
172
173     if sys.platform != "win32":
174       def waitNSPID(self, theName, thePID, theTypObj = None):
175         aCount = 0
176         aDelta = 0.5
177         anObj = None
178         print("Searching %s in Naming Service " % theName, end=' ')
179         while(1):
180           try:
181             os.kill(thePID,0)
182           except Exception:
183             raise RuntimeError("Process %d for %s not found" % (thePID,theName))
184           aCount += 1
185           anObj = self.Resolve(theName)
186           if anObj:
187             print(" found in %s seconds " % ((aCount-1)*aDelta))
188             break
189           else:
190             sys.stdout.write('+')
191             sys.stdout.flush()
192             time.sleep(aDelta)
193             pass
194           pass
195   
196         if theTypObj is None:
197           return anObj
198   
199         anObject = anObj._narrow(theTypObj)
200         if anObject is None:
201           print("%s exists but is not a %s" % (theName,theTypObj))
202         return anObject
203
204
205     # --------------------------------------------------------------------------
206
207     def ResolveLogger(self, name):
208       context_name = []
209       context_name.append(CosNaming.NameComponent(name,""))
210
211       try:
212           obj = self.rootContext.resolve(context_name)
213       except CosNaming.NamingContext.NotFound as ex:
214           obj = None
215       except CosNaming.NamingContext.InvalidName as ex:
216           obj = None
217       except CosNaming.NamingContext.CannotProceed as ex:
218           obj = None
219       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
220           obj = None
221       return obj
222
223     # --------------------------------------------------------------------------
224
225     def waitLogger(self,name,typobj=None,maxcount=40):
226       count = 0
227       delta = 0.5
228       print("Searching %s in Naming Service " % name, end=' ')
229       while(1):
230           count += 1
231           if count > maxcount : raise RuntimeError("Impossible de trouver %s" % name)
232           obj = self.ResolveLogger(name)
233           if obj :
234               print(" found in %s seconds " % ((count-1)*delta))
235               break
236           else:
237               sys.stdout.write('+')
238               sys.stdout.flush()
239               time.sleep(delta)
240
241       if typobj is None:return obj
242
243       nobj = obj._narrow(typobj)
244       if nobj is None:
245             print("%s exists but is not a %s" % (name,typobj))
246       return nobj