From c9d3f574f3e115079c33fe512078a2dc1f612919 Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Tue, 6 Jul 2021 08:35:42 -0600 Subject: [PATCH 1/3] Use Try except block for conda/source activate --- jupyter_forward/core.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/jupyter_forward/core.py b/jupyter_forward/core.py index f475c78..c24e0b4 100644 --- a/jupyter_forward/core.py +++ b/jupyter_forward/core.py @@ -153,11 +153,19 @@ def start(self): ) def _launch_jupyter(self): + + + + console.rule('[bold green]Running jupyter sanity checks (ensuring `jupyter` is in `$PATH`)', characters='*') check_jupyter_status = 'sh -c "command -v jupyter"' if self.conda_env: - check_jupyter_status = f'source activate {self.conda_env} && sh -c "command -v jupyter"' - console.rule('[bold green]Running jupyter sanity checks', characters='*') - self.run_command(command=check_jupyter_status) + try: + self.run_command(f'conda activate {self.conda_env} && {check_jupyter_status}') + except: + console.print('`conda activate` failed. Trying `source activate`...') + self.run_command(f'source activate {self.conda_env} && {check_jupyter_status}') + else: + self.run_command(check_jupyter_status) console.rule( f'[bold green] Checking $TMPDIR and $HOME on {self.session.host}', characters='*' ) From 61062ce6afb440efd079e150e296a853d19e890e Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Tue, 6 Jul 2021 08:39:20 -0600 Subject: [PATCH 2/3] Formatting only --- jupyter_forward/core.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/jupyter_forward/core.py b/jupyter_forward/core.py index c24e0b4..f442d8f 100644 --- a/jupyter_forward/core.py +++ b/jupyter_forward/core.py @@ -153,15 +153,16 @@ def start(self): ) def _launch_jupyter(self): - - - - console.rule('[bold green]Running jupyter sanity checks (ensuring `jupyter` is in `$PATH`)', characters='*') + + console.rule( + '[bold green]Running jupyter sanity checks (ensuring `jupyter` is in `$PATH`)', + characters='*', + ) check_jupyter_status = 'sh -c "command -v jupyter"' if self.conda_env: try: self.run_command(f'conda activate {self.conda_env} && {check_jupyter_status}') - except: + except Exception: console.print('`conda activate` failed. Trying `source activate`...') self.run_command(f'source activate {self.conda_env} && {check_jupyter_status}') else: From 6433d8dbba1b71891b0426df0ce26cf3ba3903dd Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Tue, 6 Jul 2021 10:55:10 -0600 Subject: [PATCH 3/3] Ensure we are using the right command --- jupyter_forward/core.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/jupyter_forward/core.py b/jupyter_forward/core.py index f442d8f..9c0ff01 100644 --- a/jupyter_forward/core.py +++ b/jupyter_forward/core.py @@ -159,12 +159,15 @@ def _launch_jupyter(self): characters='*', ) check_jupyter_status = 'sh -c "command -v jupyter"' + conda_activate_cmd = 'source activate' if self.conda_env: try: + self.run_command(f'{conda_activate_cmd} {self.conda_env} && {check_jupyter_status}') + except SystemExit: + console.print(f'`{conda_activate_cmd}` failed. Trying `conda activate`...') self.run_command(f'conda activate {self.conda_env} && {check_jupyter_status}') - except Exception: - console.print('`conda activate` failed. Trying `source activate`...') - self.run_command(f'source activate {self.conda_env} && {check_jupyter_status}') + conda_activate_cmd = 'conda activate' + else: self.run_command(check_jupyter_status) console.rule( @@ -195,7 +198,7 @@ def _launch_jupyter(self): command = f'{command} --notebook-dir={self.notebook_dir}' command = f'{command} >& {self.log_file}' if self.conda_env: - command = f'source activate {self.conda_env} && {command}' + command = f'{conda_activate_cmd} {self.conda_env} && {command}' if self.launch_command: console.rule('[bold green]Preparing Batch Job script', characters='*') @@ -277,9 +280,7 @@ def open_browser(port: int = None, token: str = None, url: str = None): def is_port_available(port): socket_for_port_check = socket.socket(socket.AF_INET, socket.SOCK_STREAM) status = socket_for_port_check.connect_ex(('localhost', int(port))) - if status == 0: # Port is in use - return False - return True + return status != 0 def parse_stdout(stdout: str):