Skip to content

Commit

Permalink
create bashrc file
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianhao-Gu committed Oct 16, 2024
1 parent 5e4b06f commit 0659eda
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/jupyterhub_config/custom_docker_spawner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import venv
from pathlib import Path

Expand All @@ -17,6 +18,7 @@ def start(self):

# Ensure the user directory exists
self._ensure_user_directory(user_dir, username)
self._ensure_bashrc(user_dir)

Check warning on line 21 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L21

Added line #L21 was not covered by tests

# Ensure the user's Jupyter directory exists
self._ensure_user_jupyter_directory(user_dir)
Expand Down Expand Up @@ -49,6 +51,29 @@ def _ensure_user_directory(self, user_dir: Path, username: str):
else:
self.log.info(f'Reusing user directory for {username}')

def _ensure_bashrc(self, user_dir: Path):
"""
Ensure the user's .bashrc and .bash_profile files exist, copying them from .tmpl templates if needed.
"""

config_dir = Path(os.environ['CONFIG_DIR'])
bashrc_ext_src = config_dir / '.bashrc.tmpl'
bash_profile_ext_src = config_dir / '.bash_profile.tmpl'

Check warning on line 61 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L59-L61

Added lines #L59 - L61 were not covered by tests

shutil.copy2(bashrc_ext_src, user_dir/'.bashrc.tmpl')
shutil.copy2(bash_profile_ext_src, user_dir/'.bash_profile.tmpl')

Check warning on line 64 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L63-L64

Added lines #L63 - L64 were not covered by tests

bashrc_dest = user_dir / '.bashrc'
bash_profile_dest = user_dir / '.bash_profile'

Check warning on line 67 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L66-L67

Added lines #L66 - L67 were not covered by tests

if not bashrc_dest.exists():
self.log.info(f'Creating .bashrc file for {user_dir}')
shutil.copy2(bashrc_ext_src, bashrc_dest)

Check warning on line 71 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L69-L71

Added lines #L69 - L71 were not covered by tests

if not bash_profile_dest.exists():
self.log.info(f'Creating .bash_profile file for {user_dir}')
shutil.copy2(bash_profile_ext_src, bash_profile_dest)

Check warning on line 75 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L73-L75

Added lines #L73 - L75 were not covered by tests

def _ensure_user_jupyter_directory(self, user_dir: Path):
"""
Create the user's Jupyter directory and subdirectories if they do not exist. And set the
Expand Down Expand Up @@ -111,6 +136,8 @@ def _configure_environment(self, user_dir: Path, user_env_dir: Path, username: s
self.environment['PYTHONSTARTUP'] = os.path.join(os.environ['JUPYTERHUB_CONFIG_DIR'], 'startup.py')
self.environment['JUPYTERHUB_USER'] = username

self.environment['SHELL'] = '/usr/bin/bash'

Check warning on line 139 in src/jupyterhub_config/custom_docker_spawner.py

View check run for this annotation

Codecov / codecov/patch

src/jupyterhub_config/custom_docker_spawner.py#L139

Added line #L139 was not covered by tests

if self._is_rw_minio_user():
self.log.info(f'MinIO read/write user detected: {self.user.name}. Setting up minio_rw credentials.')
self.environment['MINIO_ACCESS_KEY'] = self.environment['MINIO_RW_ACCESS_KEY']
Expand Down

0 comments on commit 0659eda

Please sign in to comment.