def launch_hub(port=9999): hub_addr = "localhost:%s" % (port, ) try: pythonshare.connect(hub_addr).kill_server() except: pass soe(["pythonshare-server", "-p", hub_addr.split(":")[-1]]) g_hubs_kill_at_exit.append(hub_addr) g_hubs.append(hub_addr) return hub_addr
def _clean_up(): for _ in xrange(len(g_workers)): g_workers_free.put(None) eval_jobs.put(None) time.sleep(0.01) for conn in g_worker_conns: try: conn.close() except: pass for hub_addr in g_hubs_kill_at_exit: try: pythonshare.connect(hub_addr).kill_server() except: pass
def pspycosh(psconn): """pspycosh HOSTSPEC open pycosh shell on a pythonshare server""" global _g_pspycosh_conn if isinstance(psconn, pythonshare.client.Connection): _g_pspycosh_conn = psconn else: _g_pspycosh_conn = pythonshare.connect(psconn) _g_pspycosh_conn.exec_(_g_pycosh_source) return ""
def psget(psconn, pattern): """psget CONNSPEC FILE... download files from pythonshare server""" # Get *.txt from host to current working directory: # psget passwd@host:port *.txt # Get * from host via hub to current working directory: # psget passwd@hub/host * # Get * from HOSTDIR on host, via hub, to current working directory: # psget passwd@hub/host//HOSTDIR * if isinstance(psconn, pythonshare.client.Connection): conn = psconn remotedir = "" close_connection = False elif "//" in psconn: hostspec, remotedir = psconn.split("//", 1) conn = pythonshare.connect(hostspec) close_connection = True else: remotedir = "" conn = pythonshare.connect(psconn) close_connection = True conn.exec_("".join(inspect.getsourcelines(expand)[0])) conn.exec_("import glob") if remotedir: remotedir = remotedir.replace("\\", "/") if not remotedir.endswith("/"): remotedir = remotedir + "/" rv = [] for filename in conn.eval_('expand(%s, accept_pipe=False)' % repr(remotedir + pattern)).splitlines(): try: data = conn.eval_("file(%r, 'rb').read()" % (filename,)) except: rv.append("! error reading %r" % (filename,)) continue file(os.path.basename(filename), "wb").write(data) rv.append(filename) return "\n".join(rv)
def psget(psconn, pattern): """psget CONNSPEC FILE... download files from pythonshare server""" if isinstance(psconn, pythonshare.client.Connection): conn = psconn close_connection = False else: conn = pythonshare.connect(psconn) close_connection = True conn.exec_("".join(inspect.getsourcelines(expand)[0])) conn.exec_("import glob") rv = [] for filename in conn.eval_('expand(%s, accept_pipe=False)' % repr(pattern)).splitlines(): file(os.path.basename(filename), "w").write( conn.eval_("file(%s, 'rb').read()" % (repr(filename), ))) rv.append(filename) return "\n".join(rv)
def psput(psconn, pattern): """psput CONNSPEC FILE... upload files to pythonshare server""" if isinstance(psconn, pythonshare.client.Connection): conn = psconn close_connection = False else: conn = pythonshare.connect(psconn) close_connection = True conn.exec_("import base64") rv = [] for filename in expand(pattern, accept_pipe=False).splitlines(): data = file(filename).read() conn.eval_( 'file(%s, "wb").write(base64.b64decode(%s))' % (repr(os.path.basename(filename)), repr(base64.b64encode(data)))) rv.append(filename) if close_connection: conn.close() return "\n".join(rv)
def psput(psconn, pattern): """psput CONNSPEC[//DEST] FILE... upload files to pythonshare server""" # Examples: # Put files to current working directory on host: # psput passwd@host:port files # Put localdir under cwd/relative/path on host: # psput passwd@host:port//relative/path localdir # Put localdir under /abs/path on host on Linux host: # psput passwd@host:port///abs/path localdir # Put localdir under c:/abs/winpath on Windows host: # psput passwd@host:port//c:/abs/winpath localdir # Put localdir to /abs/path on Linux host via hub/namespace: # psput passwd@hub:port/namespace///abs/path localdir # Check cwd on host: # pspycosh passwd@host:port pwd if isinstance(psconn, pythonshare.client.Connection): dest_dir = "." conn = psconn close_connection = False else: if "//" in psconn: psconn, dest_dir = psconn.split("//", 1) else: dest_dir = "." conn = pythonshare.connect(psconn) close_connection = True conn.exec_("import base64, os") rv = [] for filename in expand(pattern, accept_pipe=False).splitlines(): if os.path.isdir(filename): rv.extend(_psput_dir(conn, filename, dest_dir)) else: _psput_file(conn, filename, dest_dir + "/" + os.path.basename(filename)) rv.append(filename) if close_connection: conn.close() return "\n".join(rv)
def pspycosh(psconn, *cmdlines): """pspycosh CONNSPEC [CMD...] open remote pycosh shell or run CMDs on it""" global _g_pspycosh_conn if isinstance(psconn, pythonshare.client.Connection): _g_pspycosh_conn = psconn close_connection = False else: _g_pspycosh_conn = pythonshare.connect(psconn) close_connection = True _g_pspycosh_conn.exec_(_g_pycosh_source) if cmdlines: rv = [] try: for cmdline in cmdlines: rv.append(pycosh_eval(cmdline)) finally: if close_connection: _g_pspycosh_conn.close() _g_pspycosh_conn = None return "".join(rv) return ""
def pspycosh(psconn, *cmdlines): """pspycosh CONNSPEC [CMD...] open remote pycosh shell or run CMDs on it""" global _g_pspycosh_conn if isinstance(psconn, pythonshare.client.Connection): _g_pspycosh_conn = psconn close_connection = False else: _g_pspycosh_conn = pythonshare.connect(psconn) close_connection = True _g_pspycosh_conn.exec_(_g_pycosh_source) if cmdlines: rv = [] try: for cmdline in cmdlines: rv.append(pycosh_eval(cmdline)) finally: if close_connection: _g_pspycosh_conn.close() _g_pspycosh_conn = None return "".join(rv) return ""
def psput(psconn, pattern): """psput CONNSPEC[//DEST] FILE... upload files to pythonshare server""" # Examples: # Put files to current working directory on host: # psput passwd@host:port files # Put localdir under cwd/relative/path on host: # psput passwd@host:port//relative/path localdir # Put localdir under /abs/path on host on Linux host: # psput passwd@host:port///abs/path localdir # Put localdir under c:/abs/winpath on Windows host: # psput passwd@host:port//c:/abs/winpath localdir # Put localdir to /abs/path on Linux host via hub/namespace: # psput passwd@hub:port/namespace///abs/path localdir # Check cwd on host: # pspycosh passwd@host:port pwd if isinstance(psconn, pythonshare.client.Connection): dest_dir = "." conn = psconn close_connection = False else: if "//" in psconn: psconn, dest_dir = psconn.split("//", 1) else: dest_dir = "." conn = pythonshare.connect(psconn) close_connection = True conn.exec_("import base64, os") rv = [] for filename in expand(pattern, accept_pipe=False).splitlines(): if os.path.isdir(filename): rv.extend(_psput_dir(conn, filename, dest_dir)) else: _psput_file(conn, filename, dest_dir + "/" + os.path.basename(filename)) rv.append(filename) if close_connection: conn.close() return "\n".join(rv)
def use_workers(hub_hostspec): g_hubs.append(hub_hostspec) conn = pythonshare.connect(hub_hostspec) for ns in conn.ls_remote(): use_worker(hub_hostspec + "/" + ns)
def use_worker(worker_hostspec): conn = pythonshare.connect(worker_hostspec) g_workers.append(worker_hostspec) g_worker_conns.append(conn) g_workers_free.put(conn) thread.start_new_thread(eval_thread, ())
### Online tests for pythonshare ### ### Copyright Jukka Laukkanen / Dashing Test Automation ### [email protected] ### import os import sys import pythonshare HUB_CONNSPEC = os.getenv("HUB_CONNSPEC", "xyz@localhost") HUB_NS = os.getenv("HUB_NS", "logger") TEST_ENV = os.getenv("TEST_ENV", "na") password = HUB_CONNSPEC.split("@")[0] # don't catch the exception, all failures needs to fail the test print "Trying to connect to %s/%s" % (HUB_CONNSPEC, HUB_NS) hub = pythonshare.connect(HUB_CONNSPEC, password, HUB_NS) data_in = "data = '%s'" % (TEST_ENV, ) print "writing to hub: %s" % (data_in, ) hub.exec_in(HUB_NS, data_in) print "reading value of 'data' from hub" ret = hub.eval_in(HUB_NS, "data") print "value read from hub: %s" % (ret, ) assert ret == TEST_ENV print "Great success"