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