From 7195271ce431f30e23cb61edac9d71561ba9036b Mon Sep 17 00:00:00 2001 From: Gabriel Gazola Milan Date: Sat, 7 Nov 2020 18:28:57 -0300 Subject: [PATCH 1/2] Implement changes from https://github.com/cboone/tdaff-remote_ikernel/commit/341b23262a95830e8b7451b224cf15240b0bb2d6 --- remote_ikernel/kernel.py | 42 +++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/remote_ikernel/kernel.py b/remote_ikernel/kernel.py index 5d34d06..62ecc17 100755 --- a/remote_ikernel/kernel.py +++ b/remote_ikernel/kernel.py @@ -18,6 +18,7 @@ import uuid import pexpect +import warnings try: from pexpect import spawn as pexpect_spawn @@ -184,6 +185,20 @@ class RemoteIKernel(object): """ + @property + def host(self): + return self._host + + @host.setter + def host(self, new): + if isinstance(new, bytes): + warnings.warn('Automatically decoding self.host assignment to bytes. Did you forget a `.decode()`?') + self._host = new.decode() + elif isinstance(new, str): + self._host = new + else: + raise TypeError('self.host can only be assigned `str` or `bytes`, not `{}`'.format(type(new))) + def __init__( self, connection_info=None, @@ -219,7 +234,12 @@ def __init__( self.cpus = cpus self.pe = pe self.kernel_cmd = kernel_cmd - self.host = host # Name of node to be changed once connection is ready. + self.host = '' + if not host: + self.host = '' + else: + self.host = host # Name of node to be changed once connection is ready. + self.tunnel_hosts = tunnel_hosts self.connection = None # will usually be a spawned pexpect self.workdir = workdir @@ -357,10 +377,10 @@ def launch_pbs(self): # hostnames whould be alphanumeric with . and - permitted # This way we also ignore the echoed echo command qsub_i.expect("Running on ([\w.-]+)") - node = qsub_i.match.groups()[0] + node = qsub_i.match.groups()[0] or b'' self.log.info("Established session on node: {0}.".format(node)) - self.host = node + self.host = node.decode() def launch_sge(self, qlogin="qlogin"): """ @@ -387,9 +407,9 @@ def launch_sge(self, qlogin="qlogin"): # Hopefully this text is universal? qlogin.expect("Establishing .* session to host (.*) ...") - node = qlogin.match.groups()[0] + node = qlogin.match.groups()[0] or b'' self.log.info("Established session on node: {0}.".format(node)) - self.host = node + self.host = node.decode() def launch_slurm(self): """ @@ -416,9 +436,9 @@ def launch_slurm(self): # Hopefully this text is universal? srun.expect("srun: Node (.*), .* tasks started") - node = srun.match.groups()[0] + node = srun.match.groups()[0] or b'' self.log.info("Established session on node: {0}.".format(node)) - self.host = node + self.host = node.decode() def launch_lsf(self): """ @@ -444,9 +464,9 @@ def launch_lsf(self): # Hopefully this text is universal? bsub.expect("<>") - node = bsub.match.groups()[0] + node = bsub.match.groups()[0] or b'' self.log.info("Established session on node: {0}.".format(node)) - self.host = node + self.host = node.decode() def start_kernel(self): """ @@ -631,10 +651,6 @@ def tunnel_hosts_cmd(self): @property def tunnel_cmd(self): """Return a tunnelling command that just needs a port.""" - # zmq needs str in Python 3, but pexpect gives bytes - if hasattr(self.host, "decode"): - self.host = self.host.decode("utf-8") - # One connection can tunnel all the ports ports_str = " ".join( [ From 643a0057c31dba6a880017787cb3bb6b460605d9 Mon Sep 17 00:00:00 2001 From: Gabriel Gazola Milan Date: Sat, 7 Nov 2020 19:04:55 -0300 Subject: [PATCH 2/2] Fix PEP8 warning (similar to https://stackoverflow.com/questions/19030952/pep8-warning-on-regex-string-in-python-eclipse) --- remote_ikernel/kernel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/remote_ikernel/kernel.py b/remote_ikernel/kernel.py index 5d34d06..6ad68e1 100755 --- a/remote_ikernel/kernel.py +++ b/remote_ikernel/kernel.py @@ -356,7 +356,7 @@ def launch_pbs(self): # hostnames whould be alphanumeric with . and - permitted # This way we also ignore the echoed echo command - qsub_i.expect("Running on ([\w.-]+)") + qsub_i.expect(r"Running on ([\w.-]+)") node = qsub_i.match.groups()[0] self.log.info("Established session on node: {0}.".format(node))