Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tcsh support #67

Merged
merged 4 commits into from
Mar 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions jupyter_forward/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class RemoteRunner:
shell: str = '/usr/bin/env bash'

def __post_init__(self):
self.run_kwargs = dict(pty=True, shell=self.shell)
self.run_kwargs = dict(pty=True)
console.rule('[bold green]Authentication', characters='*')
if self.port_forwarding and not is_port_available(self.port):
console.log(
Expand Down Expand Up @@ -90,20 +90,26 @@ def __post_init__(self):

console.log('[bold cyan]:white_check_mark: The client is authenticated successfully')

def _jupyter_info(self, command='command -v jupyter'):
def _jupyter_info(self, command='sh -c "command -v jupyter"'):
console.rule('[bold green]Running jupyter sanity checks', characters='*')
out = self.session.run(command, warn=True, hide='out', **self.run_kwargs)
if out.failed:
console.log(f"[bold red]:x: Couldn't find jupyter executable with: '{command}'")
sys.exit(1)
console.log('[bold cyan]:white_check_mark: Found jupyter executable')

def envvar_exists(self, envvar):
message = 'variable is not defined'
cmd = f'''printenv {envvar} || echo "{message}"'''
out = self.session.run(cmd, hide='out', **self.run_kwargs).stdout.strip()
return message not in out

def dir_exists(self, directory):
"""
Checks if a given directory exists on remote host.
"""
message = "couldn't find the directory"
cmd = f'''if [[ ! -d "{directory}" ]] ; then echo "{message}"; fi'''
cmd = f'''cd {directory} || echo "{message}"'''
out = self.session.run(cmd, hide='out', **self.run_kwargs).stdout.strip()
return message not in out

Expand Down Expand Up @@ -136,14 +142,31 @@ def start(self):
# Logfile will be in $TMPDIR if defined on the remote machine, otherwise in $HOME

try:
check_jupyter_status = 'command -v jupyter'
check_jupyter_status = 'sh -c "command -v jupyter"'
if self.conda_env:
check_jupyter_status = f'conda activate {self.conda_env} && command -v jupyter'
check_jupyter_status = (
f'conda activate {self.conda_env} && sh -c "command -v jupyter"'
)
self._jupyter_info(check_jupyter_status)
if self.dir_exists('$TMPDIR'):
if self.envvar_exists('TMPDIR') and self.dir_exists('$TMPDIR'):
self.log_dir = '$TMPDIR'
else:
elif self.envvar_exists('HOME') and self.dir_exists('$HOME'):
self.log_dir = '$HOME'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should double check to make sure $HOME exists and is pointing to a directory too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $HOME does not exist, should we abort? I could do something like

            if self.envvar_exists('TMPDIR') and self.dir_exists('$TMPDIR'):
                self.log_dir = '$TMPDIR'
            elif self.envvar_exists('HOME') and self.dir_exists('$HOME'):
                self.log_dir = '$HOME'
            else:
                console.log(
                    '[bold red]Can not determine directory for log file'
                )
                sys.exit(1)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if both $TMPDIR and $HOME are invalid, we should probably raise an error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a more informative error, we can be more verbose in Can not determine directory for log file' by specifying that both $TMPDIR and $HOME were found to be undefined or pointing to non-existing directories....

else:
message = (
'$TMPDIR/ is not a directory'
if self.envvar_exists('TMPDIR')
else '$TMPDIR is not defined'
)
console.log(f'[bold red]{message}')
message = (
'$HOME/ is not a directory'
if self.envvar_exists('HOME')
else '$HOME is not defined'
)
console.log(f'[bold red]{message}')
console.log('[bold red]Can not determine directory for log file')
sys.exit(1)

self.log_dir = f'{self.log_dir}/.jupyter_forward'
self.session.run(f'mkdir -p {self.log_dir}', **self.run_kwargs)
Expand All @@ -158,7 +181,7 @@ def start(self):
command = f'{command} --ip=`hostname`'
if self.notebook_dir:
command = f'{command} --notebook-dir={self.notebook_dir}'
command = f'{command} > {self.log_file} 2>&1'
command = f'{command} >& {self.log_file}'
if self.conda_env:
command = f'conda activate {self.conda_env} && {command}'

Expand Down