21 from socket
import gethostname
22 from string
import split, join
23 from threading
import Lock, Thread
24 from time
import time, sleep
28 from Pyro.errors
import PyroError, NamingError, ConnectionDeniedError
29 from Pyro.naming
import NameServerProxy
33 default_group =
':%s' % os.environ[
'USER']
34 Pyro.config.PYRO_NS_DEFAULTGROUP = default_group
36 if 'PYROGRID_TIMEOUT' in os.environ:
37 default_timeout = eval(os.environ[
'PYROGRID_TIMEOUT'])
39 default_timeout = 600.0
41 if 'PYROGRID_WAIT' in os.environ:
42 default_wait = eval(os.environ[
'PYROGRID_WAIT'])
46 def absolute_uri(uri, ns = None):
48 if not isinstance(ns, NameServerProxy):
49 if ns
is None or ns ==
'':
50 ns = Pyro.naming.NameServerLocator().getNS()
52 ns = Pyro.naming.NameServerLocator().getNS(host=ns)
54 return ns.fullName(uri)
56 def group_uri_list(group_uri, ns = None):
58 Returns the list of all URIs given the group_uri
61 if not isinstance(ns, NameServerProxy):
62 if ns
is None or ns ==
'':
63 ns = Pyro.naming.NameServerLocator().getNS()
65 ns = Pyro.naming.NameServerLocator().getNS(host=ns)
70 lst = ns.list(level_0)
72 level_1 = [ join( (level_0, lst[i][0]),
'.' )
for i
in range(0, len(lst)) ]
75 for level_1_i
in level_1:
76 level_1_expanded += group_uri_list(level_1_i, ns)
82 level_1_expanded = [ns.fullName(level_0)]
87 return level_1_expanded
91 returns a number which is not in xlst
96 if len(xlst) == 0: x = 0
100 for i
in range(len(xlst)):
106 def create_unique_uri(obj_name, n = 1, ns = None):
108 obj_name = e.g. URI will be 'obj_name.6'
109 uri_list = group_uri_list(obj_name)
111 Create unique name 'obj_name.x' (the name which is not in uri_list)
115 print 'PyroUtils.create_unique_uri: %s URIs for %s...' % (n, obj_name)
117 if not isinstance(ns, NameServerProxy):
118 if ns
is None or ns ==
'':
119 ns = Pyro.naming.NameServerLocator().getNS()
121 ns = Pyro.naming.NameServerLocator().getNS(host=ns)
123 old_uri_list = group_uri_list(obj_name, ns)
126 for uri
in old_uri_list: xlst.append( eval(uri.split(
'.')[-1]) )
133 new_uri_list.append( ns.fullName(join( (obj_name, str(x)),
'.' )) )
138 def create_groups(obj_uri, ns = None):
140 Creates groups for the obj_uri
142 Example: for 'factory.horse--bio.1'
143 creates groups factory and factory.horse--bio
147 if not isinstance(ns, NameServerProxy):
148 if ns
is None or ns ==
'':
149 ns = Pyro.naming.NameServerLocator().getNS()
151 ns = Pyro.naming.NameServerLocator().getNS(host=ns)
153 try: ns.createGroup(default_group)
154 except NamingError:
pass
156 groups = obj_uri.split(
'.')[:-1]
158 for i
in range(1, len(groups)):
159 groups[i] = join( groups[i-1:i+1],
'.')
161 for i
in range(0, len(groups)):
163 ns.createGroup(groups[i])
167 def is_stopped(obj_uri, timeout = default_timeout, ns = None):
169 Checks if the Pyro remote object with URI obj_uri
170 has been stopped (gracefully).
174 print 'PyroUtils.is_stopped: for %s...' % obj_uri
176 if not isinstance(ns, NameServerProxy):
177 if ns
is None or ns ==
'':
178 ns = Pyro.naming.NameServerLocator().getNS()
180 ns = Pyro.naming.NameServerLocator().getNS(host = ns)
186 while (
not stopped)
and time()-t0 < timeout:
189 except Pyro.errors.NamingError:
196 print 'PyroUtils.is_stopped: Object with URI %s could not ' +\
197 'be shut down during timeout' % obj_uri
200 def launch_instance(obj_instance, obj_uri, delegate, \
201 nshost =
None, debug =
True, shutdown =
False):
203 Launches obj_instance as Pyro daemon on the current host.
205 obj_instance = the class of the Pyro implementation object
206 obj_uri = URI for the object to register in the Name Server
207 <- create_uri(obj_name, nshost)
208 delegate = whether to use the delegate approach or the regular
209 objBase subclassing approach
211 Function also assigns:
213 obj_instance._pyro_stop = False
214 obj_instance._pyro_suppl['uri'] = obj_uri
220 print 'PyroUtils.launch_instance: %s, URI %s, PID %s on %s' \
221 % (obj_instance, obj_uri, os.getpid(), gethostname())
225 Pyro.config.PYRO_MOBILE_CODE = 1
228 Pyro.core.initServer()
232 if nshost
is None or nshost ==
'':
233 ns = Pyro.naming.NameServerLocator().getNS()
235 ns = Pyro.naming.NameServerLocator().getNS(host=nshost)
239 except NamingError:
pass
241 print 'Object with URI %s already exists, unregister first!' % obj_uri
244 create_groups(obj_uri, ns)
249 print 'launching pyro daemon and nameserver'
250 daemon = Pyro.core.Daemon()
251 daemon.useNameServer(ns)
255 obj_instance._pyro_stop =
False
256 obj_instance._pyro_suppl = {}
257 obj_instance._pyro_suppl[
'uri'] = obj_uri
258 obj_instance._pyro_suppl[
'nshost'] = nshost
259 obj_instance._pyro_suppl[
'host'] = gethostname()
261 obj_instance._pyro_suppl[
'callable'] = {}
262 for attr
in dir(obj_instance):
263 obj_instance._pyro_suppl[
'callable'][attr] = callable(getattr(obj_instance,attr))
265 if isinstance(obj_instance, type(Thread())):
272 obj = Pyro.core.ObjBase()
273 obj.delegateTo(obj_instance)
274 daemon.connectPersistent(obj, obj_uri)
277 daemon.connectPersistent(obj_instance, obj_uri)
279 print 'PyroUtils.launch_instance: Remote object "%s" is ready' % obj_uri
284 daemon.requestLoop(condition =
lambda:
285 not obj_instance._pyro_stop)
286 except KeyboardInterrupt:
289 print 'PyroUtils.launch_instance: Shutting down %s gracefully' % obj_uri
293 daemon.disconnect(obj)
295 daemon.disconnect(obj_instance)
303 print 'Finished, keeping interperter alive and Pyro daemon running'
305 def get_proxy(obj_uri, ns = None, attrproxy = True, timeout = default_timeout):
308 print 'PyroUtils.get_proxy: "%s" using nameserver "%s"...' \
311 Pyro.config.PYRO_MOBILE_CODE = 1
313 Pyro.core.initClient()
315 if not isinstance(ns, NameServerProxy):
316 if ns
is None or ns ==
'':
317 ns = Pyro.naming.NameServerLocator().getNS()
319 ns = Pyro.naming.NameServerLocator().getNS(host=ns)
326 pyro_uri = ns.resolve(obj_uri)
329 except Pyro.errors.NamingError, x:
331 if time()-t0 < timeout:
335 print 'PyroUtils.get_proxy: could not locate object "%s" using "%s"' \
340 print 'PyroUtils.get_proxy: returning the proxy "%s"' % pyro_uri
343 return pyro_uri.getAttrProxy()
345 return pyro_uri.getProxy()
347 def get_nameserver(nshost):
349 if nshost
is None or nshost ==
'':
350 return Pyro.naming.NameServerLocator().getNS()
352 return Pyro.naming.NameServerLocator().getNS(host = nshost)
354 def unregister(obj_uri, ns = None):
357 print 'PyroUtils.unregister: Unregistering %s...' % obj_uri
359 if not isinstance(ns, NameServerProxy):
360 if ns
is None or ns ==
'':
361 ns = Pyro.naming.NameServerLocator().getNS()
363 ns = Pyro.naming.NameServerLocator().getNS(host = ns)
365 ns.unregister(obj_uri)