/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/solver/solver_interfaces.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | import threading
import os
import socket
try:
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from SimpleHTTPServer import SimpleHTTPRequestHandler
except ImportError:
# Python 3.x
from xmlrpc.server import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from http.server import SimpleHTTPRequestHandler
from multiprocessing.managers import BaseManager, BaseProxy
class MultiprocessingInterface(BaseManager):
""" A multiprocessing interface to the solver controller
This object exports a controller instance proxy over the multiprocessing
interface. Control actions can be performed by connecting to the interface
and calling methods on the controller proxy instance """
def __init__(self, address=None, authkey=None, try_next_port=False):
BaseManager.__init__(self, address, authkey)
self.authkey = authkey
self.try_next_port = try_next_port
def get_controller(self):
return self.controller
def start(self, controller):
self.controller = controller
self.register('get_controller', self.get_controller)
if not self.try_next_port:
self.get_server().serve_forever()
host, port = self.address
while self.try_next_port:
try:
BaseManager.__init__(self, (host,port), self.authkey)
self.get_server().serve_forever()
self.try_next_port = False
except socket.error as e:
try_next_port = False
import errno
if e.errno == errno.EADDRINUSE:
port += 1
else:
raise
class MultiprocessingClient(BaseManager):
""" A client for the multiprocessing interface
Override the run() method to do appropriate actions on the proxy
instance of the controller object or add an interface using the add_interface
methods similar to the Controller.add_interface method """
def __init__(self, address=None, authkey=None, serializer='pickle', start=True):
BaseManager.__init__(self, address, authkey, serializer)
if start:
self.start()
def start(self, connect=True):
self.interfaces = []
# to work around a python caching bug
# http://stackoverflow.com/questions/3649458/broken-pipe-when-using-python-multiprocessing-managers-basemanager-syncmanager
if self.address in BaseProxy._address_to_local:
del BaseProxy._address_to_local[self.address][0].connection
self.register('get_controller')
if connect:
self.connect()
self.controller = self.get_controller()
self.run(self.controller)
@staticmethod
def is_available(address):
try:
socket.create_connection(address, 1).close()
return True
except socket.error:
return False
def run(self, controller):
pass
def add_interface(self, callable):
""" This makes it act as substitute for the actual command_manager """
thr = threading.Thread(target=callable, args=(self.controller,))
thr.daemon = True
thr.start()
return thr
class CrossDomainXMLRPCRequestHandler(SimpleXMLRPCRequestHandler,
SimpleHTTPRequestHandler):
""" SimpleXMLRPCRequestHandler subclass which attempts to do CORS
CORS is Cross-Origin-Resource-Sharing (http://www.w3.org/TR/cors/)
which enables xml-rpc calls from a different domain than the xml-rpc server
(such requests are otherwise denied)
"""
def do_OPTIONS(self):
""" Implement the CORS pre-flighted access for resources """
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-METHODS", "POST,GET,OPTIONS")
#self.send_header("Access-Control-Max-Age", "60")
self.send_header("Content-length", "0")
self.end_headers()
def do_GET(self):
""" Handle http requests to serve html/image files only """
print(self.path, self.translate_path(self.path))
permitted_extensions = ['.html','.png','.svg','.jpg', '.js']
if not os.path.splitext(self.path)[1] in permitted_extensions:
self.send_error(404, 'File Not Found/Allowed')
else:
SimpleHTTPRequestHandler.do_GET(self)
def end_headers(self):
""" End response header with adding Access-Control-Allow-Origin
This is done to enable CORS request from all clients """
self.send_header("Access-Control-Allow-Origin", "*")
SimpleXMLRPCRequestHandler.end_headers(self)
class XMLRPCInterface(SimpleXMLRPCServer):
""" An XML-RPC interface to the solver controller
Currently cannot work with objects which cannot be marshalled
(which is basically most custom classes, most importantly
ParticleArray and numpy arrays) """
def __init__(self, addr, requestHandler=CrossDomainXMLRPCRequestHandler,
logRequests=True, allow_none=True,
encoding=None, bind_and_activate=True):
SimpleXMLRPCServer.__init__(self, addr, requestHandler, logRequests,
allow_none, encoding, bind_and_activate)
def start(self, controller):
self.register_instance(controller, allow_dotted_names=False)
self.register_introspection_functions()
self.serve_forever()
class CommandlineInterface(object):
""" command-line interface to the solver controller """
def start(self, controller):
while True:
try:
try:
inp = raw_input('pysph[%d]>>> '%controller.get('count'))
except NameError:
inp = input('pysph[%d]>>> '%controller.get('count'))
cmd = inp.strip().split()
try:
cmd, args = cmd[0], cmd[1:]
except Exception as e:
print('Invalid command')
self.help()
continue
args2 = []
for arg in args:
try:
arg = eval(arg)
except:
pass
finally:
args2.append(arg)
if cmd=='p' or cmd=='pause':
controller.pause_on_next()
elif cmd=='c' or cmd=='cont':
controller.cont()
elif cmd=='g' or cmd=='get':
print(controller.get(args[0]))
elif cmd=='s' or cmd=='set':
print(controller.set(args[0], args2[1]))
elif cmd=='q' or cmd=='quit':
break
else:
print(getattr(controller, cmd)(*args2))
except Exception as e:
self.help()
print(e)
def help(self):
print('''Valid commands are:
p | pause
c | cont
g | get <name>
s | set <name> <value>
q | quit -- quit commandline interface (solver keeps running)''')
|