def launchJupyter(homeDir): """ Method to set env vars for overriding jup config, adding python path and extensions, and finally launching jupyter """ from xpedite.jupyter import SHELL_PREFIX, DATA_DIR from xpedite.dependencies import binPath LOGGER.info('') pyPath = os.path.dirname( binPath('python')) + os.pathsep + os.environ['PATH'] initPath = os.path.dirname(__file__) jupyterEnv = os.environ jupyterEnv[Context.dataPathKey] = os.path.join(os.path.abspath(homeDir), DATA_DIR) jupyterEnv['JUPYTER_PATH'] = os.path.join(initPath, '../../../jupyter/extensions/') jupyterEnv['JUPYTER_CONFIG_DIR'] = os.path.join( initPath, '../../../jupyter/config/') jupyterEnv['XPEDITE_PATH'] = os.path.abspath( os.path.join(initPath, '../../')) jupyterEnv['PATH'] = pyPath jupyterEnv['HOME'] = tempfile.mkdtemp(prefix=SHELL_PREFIX, dir='/tmp') ip = socket.gethostbyname(socket.gethostname()) jupyterBinary = binPath('jupyter') os.execle(jupyterBinary, 'Xpedite', 'notebook', '--no-browser', '--ip=' + ip, '--notebook-dir=' + homeDir, jupyterEnv)
def __enter__(self): """ Bring up the server and connect to it :return: the Remote itself :rtype: Remote """ from xpedite.dependencies import binPath python = binPath('python2') rpyproc = os.path.join(os.path.dirname(__file__), 'rpyc') if self.host == 'localhost': self.proc = subprocess.Popen([python, rpyproc], stderr=self.std.err, stdout=self.std.out) else: pluginPath = os.environ.get('XPEDITE_PLUGIN_PATH', '') env = 'export XPEDITE_PLUGIN_PATH={};'.format( pluginPath) if pluginPath else '' ssh = binPath('ssh') rpyproc = '/Xpedite/scripts/lib/xpedite/transport/rpyc' self.proc = subprocess.Popen([ ssh, '-T', '-o', 'StrictHostKeyChecking=no', '-l', 'root', self.host, env, python, rpyproc ], stderr=self.std.err, stdout=self.std.out) seconds, done, extracts = 0, False, {} while seconds < self.timeout and not done: done, extracts = matchRegexpsInFile(self.std.errPath, [ re.compile('.*server started on .*:(?P<port>.*)'), re.compile(r'.*pid\<(?P<pid>[0-9]+)\>') ]) time.sleep(0.2) seconds += 0.2 if not done: self.kill() with open(self.std.errPath, 'r') as err: errStr = err.read() raise Exception( 'Timed out trying to launch rpyc on {0} - stderr contents: {1}' .format(self.host, errStr)) self.port = int(extracts['port']) self.pid = int(extracts['pid']) pwd = os.environ['PWD'] self.connection = rpyc.classic.connect(self.host, self.port, keepalive=self.keepalive) self.connection.modules.sys.stdout = sys.stdout if self.chdir: self.connection.modules.os.chdir(pwd) self.connection.modules.os.environ['PWD'] = pwd return self
def kill(self): """ Terminate ssh connection and reap rpyc :returns: None :rtype: NoneType """ self.proc.kill() self.proc.wait() if self.host != 'localhost': from xpedite.dependencies import binPath ssh = binPath('ssh') subprocess.Popen([ ssh, '-T', '-o', 'StrictHostKeyChecking=no', self.host, 'kill', str(self.pid) ]).wait() self.std.close()