List micromamba envs based on the filesystem instead of subprocess

This commit is contained in:
Benoît Seignovert 2024-02-01 18:08:04 +01:00
parent 92059ce826
commit 3cac72ee29
Signed by: Benoît Seignovert
GPG key ID: F5D8895227D18A0B

View file

@ -1,8 +1,8 @@
"""Python environment module."""
import shlex
import subprocess
from dataclasses import dataclass
from operator import itemgetter
from pathlib import Path
@dataclass
@ -34,40 +34,39 @@ class GlobalPyEnv(PyEnv):
scope = 'GLOBAL'
def _micromamba_env_list(root_prefix='', base=False):
"""List micromamba environment list."""
env_list = subprocess.run(
shlex.split('/usr/local/bin/micromamba env list --quiet'), # noqa: S603 (FIXME)
env={'MAMBA_ROOT_PREFIX': root_prefix},
capture_output=True,
text=True,
check=False,
)
def _micromamba_path(path: str) -> Path:
"""Micromamba path environments locations."""
return Path(f'/micromamba/{path}/envs')
return [map(str.strip, env.split()) for env in env_list.stdout.splitlines()[2 if base else 3 :]]
def _micromamba_envs(path, excluded=None) -> list:
"""List micromamba environment list."""
if excluded is None:
excluded = []
elif isinstance(excluded, str):
excluded = [excluded]
envs = _micromamba_path(path)
return sorted(
[(f.name, f.absolute()) for f in envs.iterdir() if f.is_dir() and f.name not in excluded],
key=itemgetter(0),
)
def get_pyenv_user(username: str) -> list:
"""List of python environment available to the user."""
return [
UserPyEnv(*env)
for env in _micromamba_env_list(root_prefix=f'/micromamba/{username}/', base=True)
]
return [UserPyEnv(*env) for env in _micromamba_envs(username)]
def get_pyenv_team(username: str) -> list:
"""List of python environment available to the user's team."""
teams = [] # FIXME: pull user teams list from groups
return [
TeamPyEnv(*env)
for team in teams
for env in _micromamba_env_list(root_prefix=f'/micromamba/teams/{team}')
]
return [TeamPyEnv(*env) for team in teams for env in _micromamba_envs(f'teams/{team}')]
def get_pyenv_global() -> list:
"""List of python environment available globally."""
return [GlobalPyEnv(*env) for env in _micromamba_env_list(root_prefix='/micromamba/operator/')]
return [GlobalPyEnv(*env) for env in _micromamba_envs('operator', excluded='glicid-jupyterhub')]
def get_pyenv(username: str) -> list: