Skip to content

Commit

Permalink
Merge branch 'main' into pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
  • Loading branch information
andersy005 authored Jan 16, 2025
2 parents 134d9a3 + 847d577 commit bf66565
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 155 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ jobs:
sudo apt-get update
sudo apt-get install zsh tcsh
cat /etc/shells
- uses: conda-incubator/setup-miniconda@master
- name: Install Conda environment from environment.yml
uses: mamba-org/setup-micromamba@v2
id: conda
with:
channels: conda-forge
channel-priority: strict
activate-environment: jupyter-forward-dev
auto-update-conda: false
python-version: ${{ matrix.python-version }}
mamba-version: "*"
use-mamba: true
miniforge-variant: Mambaforge
# environment-file is not assumed anymore
environment-file: ci/environment.yml
create-args: >-
python=${{ matrix.python-version }}
# now called cache-environment
cache-environment: true

- name: Install jupyter-forward
run: |
Expand Down
17 changes: 8 additions & 9 deletions .github/workflows/upstream-dev-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ jobs:
sudo apt-get update
sudo apt-get install zsh tcsh
cat /etc/shells
- uses: conda-incubator/setup-miniconda@v3
- name: Install Conda environment from environment.yml
uses: mamba-org/setup-micromamba@v2
id: conda
with:
channels: conda-forge,nodefaults
channel-priority: strict
activate-environment: jupyter-forward-dev
auto-update-conda: false
python-version: ${{ matrix.python-version }}
# environment-file is not assumed anymore
environment-file: ci/environment-upstream-dev.yml
mamba-version: "*"
use-mamba: true
miniforge-variant: Mambaforge
create-args: >-
python=${{ matrix.python-version }}
# now called cache-environment
cache-environment: true

- name: Install jupyter-forward
id: install
Expand Down
53 changes: 31 additions & 22 deletions jupyter_forward/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import dataclasses
import datetime
import getpass
Expand Down Expand Up @@ -79,15 +80,12 @@ def _authenticate(self):
f'[bold cyan]Authenticating user ({self.session.user}) from client ({socket.gethostname()}) to remote host ({self.session.host})'
)
# Try passwordless authentication
try:
self.session.open()
except (
with contextlib.suppress(
paramiko.ssh_exception.BadAuthenticationType,
paramiko.ssh_exception.AuthenticationException,
paramiko.ssh_exception.SSHException,
):
pass

self.session.open()
# Prompt for password and token (2FA)
if not self.session.is_connected:
for _ in range(2):
Expand All @@ -110,10 +108,10 @@ def _authenticate(self):
def _check_shell(self):
console.rule('[bold green]Verifying shell location', characters='*')
if self.shell is None:
shell = self.session.run('echo $SHELL || echo $0', hide='out').stdout.strip()
if not shell:
if shell := self.session.run('echo $SHELL || echo $0', hide='out').stdout.strip():
self.shell = shell
else:
raise ValueError('Could not determine shell. Please specify one using --shell.')
self.shell = shell
else:
# Get the full path to the shell in case the user specified a shell name
self.shell = self.run_command(f'which {self.shell}').stdout.strip()
Expand Down Expand Up @@ -225,23 +223,36 @@ def _generate_redirect_command(self, *, log_file: str, command: str) -> str:

def _conda_activate_cmd(self):
console.rule(
'[bold green]Running jupyter sanity checks',
'[bold green]Running Jupyter sanity checks',
characters='*',
)
check_jupyter_status = 'which jupyter'
conda_activate_cmd = 'source activate'
activate_cmds = ['source activate', 'conda activate']

# Check for mamba availability and prioritize it if found
try:
mamba_check = self.run_command('which mamba', warn=False, echo=False, exit=False)
if not mamba_check.failed:
activate_cmds = ['mamba activate']
except Exception as e:
console.print(f'[bold yellow]:warning: Mamba check failed: {e}')

# Attempt activation
if self.conda_env:
try:
self.run_command(f'{conda_activate_cmd} {self.conda_env} && {check_jupyter_status}')
except SystemExit:
console.print(
f'[bold red]:x: `{conda_activate_cmd}` failed. Trying `conda activate`...'
)
self.run_command(f'conda activate {self.conda_env} && {check_jupyter_status}')
conda_activate_cmd = 'conda activate'
for cmd in activate_cmds:
try:
self.run_command(f'{cmd} {self.conda_env} && {check_jupyter_status}')
return cmd # Return the successfully executed command
except SystemExit:
console.print(f'[bold red]:x: `{cmd}` failed. Trying next...')
else:
self.run_command(check_jupyter_status)
return conda_activate_cmd

# Final fallback if all commands fail
console.print(
'[bold red]:x: Could not activate environment. Ensure Conda or Mamba is installed.'
)
sys.exit(1)

def _parse_log_file(self):
# wait for logfile to contain access info, then write it to screen
Expand All @@ -253,13 +264,11 @@ def _parse_log_file(self):
):
# TODO: Ensure this loop doesn't run forever if the log file is not found or empty
while condition:
try:
with contextlib.suppress(invoke.exceptions.UnexpectedExit):
result = self.run_command(f'cat {self.log_file}', echo=False, hide='out')
if 'is running at:' in result.stdout.strip():
condition = False
stdout = result.stdout
except invoke.exceptions.UnexpectedExit:
pass
return parse_stdout(stdout)

def _prepare_batch_job_script(self, command):
Expand Down
3 changes: 2 additions & 1 deletion jupyter_forward/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import getpass
import re
import socket
import typing
import urllib.parse

from .console import console
Expand Down Expand Up @@ -55,7 +56,7 @@ def is_port_available(port) -> bool:
return status != 0


def parse_stdout(stdout: str) -> dict[str, str]:
def parse_stdout(stdout: str) -> dict[str, typing.Any | None]:
"""Parses stdout to determine remote_hostname, port, token, url
Parameters
Expand Down
Loading

0 comments on commit bf66565

Please sign in to comment.