List micromamba envs based on the filesystem instead of subprocess
This commit is contained in:
parent
92059ce826
commit
3cac72ee29
1 changed files with 21 additions and 22 deletions
|
@ -1,8 +1,8 @@
|
||||||
"""Python environment module."""
|
"""Python environment module."""
|
||||||
|
|
||||||
import shlex
|
|
||||||
import subprocess
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from operator import itemgetter
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
@ -34,40 +34,39 @@ class GlobalPyEnv(PyEnv):
|
||||||
scope = 'GLOBAL'
|
scope = 'GLOBAL'
|
||||||
|
|
||||||
|
|
||||||
def _micromamba_env_list(root_prefix='', base=False):
|
def _micromamba_path(path: str) -> Path:
|
||||||
"""List micromamba environment list."""
|
"""Micromamba path environments locations."""
|
||||||
env_list = subprocess.run(
|
return Path(f'/micromamba/{path}/envs')
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
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:
|
def get_pyenv_user(username: str) -> list:
|
||||||
"""List of python environment available to the user."""
|
"""List of python environment available to the user."""
|
||||||
return [
|
return [UserPyEnv(*env) for env in _micromamba_envs(username)]
|
||||||
UserPyEnv(*env)
|
|
||||||
for env in _micromamba_env_list(root_prefix=f'/micromamba/{username}/', base=True)
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def get_pyenv_team(username: str) -> list:
|
def get_pyenv_team(username: str) -> list:
|
||||||
"""List of python environment available to the user's team."""
|
"""List of python environment available to the user's team."""
|
||||||
teams = [] # FIXME: pull user teams list from groups
|
teams = [] # FIXME: pull user teams list from groups
|
||||||
return [
|
return [TeamPyEnv(*env) for team in teams for env in _micromamba_envs(f'teams/{team}')]
|
||||||
TeamPyEnv(*env)
|
|
||||||
for team in teams
|
|
||||||
for env in _micromamba_env_list(root_prefix=f'/micromamba/teams/{team}')
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def get_pyenv_global() -> list:
|
def get_pyenv_global() -> list:
|
||||||
"""List of python environment available globally."""
|
"""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:
|
def get_pyenv(username: str) -> list:
|
||||||
|
|
Loading…
Add table
Reference in a new issue