List only micromamba environments with jupyter kernel(s)

This commit is contained in:
Benoît Seignovert 2024-03-07 15:46:13 +01:00
parent c4c8fa33aa
commit 1a7f5f102f
Signed by: Benoît Seignovert
GPG key ID: F5D8895227D18A0B
8 changed files with 49 additions and 17 deletions

View file

@ -7,7 +7,6 @@ from pathlib import Path
MICROMAMBA_ROOT = Path('/micromamba') # default micromamba root location
GLOBAL_USER = 'operator'
GLOBAL_EXCLUDED = 'glicid-jupyterhub'
@dataclass
@ -19,15 +18,9 @@ class MicromambaEnv:
path: str
def _envs(folder, excluded=None) -> list:
"""List micromamba environments."""
# Convert excluded as as list
if excluded is None:
excluded = []
elif isinstance(excluded, str):
excluded = [excluded]
# Get micromamba envs as pathlib.Path
def _envs(folder) -> list:
"""List micromamba environments with jupyter kernel(s)."""
# Get micromamba environments as pathlib.Path folders
envs = MICROMAMBA_ROOT / folder / 'envs'
if not envs.exists():
@ -37,7 +30,7 @@ def _envs(folder, excluded=None) -> list:
[
(env.name, str(env))
for env in envs.iterdir()
if env.is_dir() and env.name not in excluded
if env.is_dir() and any(env.glob('share/jupyter/kernels/*/kernel.json'))
],
key=itemgetter(0),
)
@ -64,10 +57,7 @@ def _envs_team(username: str) -> list:
def _envs_global() -> list:
"""Micromamba environment(s) available globally."""
return [
MicromambaEnv('GLOBAL', name, path)
for name, path in _envs(GLOBAL_USER, excluded=GLOBAL_EXCLUDED)
]
return [MicromambaEnv('GLOBAL', name, path) for name, path in _envs(GLOBAL_USER)]
def get_envs(username: str) -> list: