Skip to content

Commit

Permalink
feat: add mamba environment
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Jun 11, 2021
1 parent 24fc263 commit e0d650a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
5 changes: 3 additions & 2 deletions nox/sessions.py
Expand Up @@ -401,7 +401,7 @@ def conda_install(
extraopts = ("--offline",)

self._run(
"conda",
venv.conda_cmd,
"install",
"--yes",
*extraopts,
Expand Down Expand Up @@ -550,12 +550,13 @@ def _create_venv(self) -> None:
reuse_existing=reuse_existing,
venv_params=self.func.venv_params,
)
elif backend == "conda":
elif backend in {"conda", "mamba"}:
self.venv = CondaEnv(
self.envdir,
interpreter=self.func.python, # type: ignore
reuse_existing=reuse_existing,
venv_params=self.func.venv_params,
conda_cmd=backend,
)
elif backend == "venv":
self.venv = VirtualEnv(
Expand Down
21 changes: 18 additions & 3 deletions nox/virtualenv.py
Expand Up @@ -152,6 +152,10 @@ class PassthroughEnv(ProcessEnv):
hints about the actual env.
"""

@property
def conda_cmd(self) -> str:
return "conda"

@staticmethod
def is_offline() -> bool:
"""As of now this is only used in conda_install"""
Expand All @@ -176,23 +180,27 @@ class CondaEnv(ProcessEnv):
If not specified, this will use the currently running Python.
reuse_existing (Optional[bool]): Flag indicating if the conda environment
should be reused if it already exists at ``location``.
conda_cmd (str): The name of the command, can be "conda" (default) or "mamba".
"""

is_sandboxed = True
allowed_globals = ("conda",)
allowed_globals = ("conda", "mamba")

def __init__(
self,
location: str,
interpreter: Optional[str] = None,
reuse_existing: bool = False,
venv_params: Any = None,
*,
conda_cmd: str = "conda",
):
self.location_name = location
self.location = os.path.abspath(location)
self.interpreter = interpreter
self.reuse_existing = reuse_existing
self.venv_params = venv_params if venv_params else []
self.conda_cmd = conda_cmd
super(CondaEnv, self).__init__()

def _clean_location(self) -> bool:
Expand All @@ -201,7 +209,14 @@ def _clean_location(self) -> bool:
if self.reuse_existing:
return False
else:
cmd = ["conda", "remove", "--yes", "--prefix", self.location, "--all"]
cmd = [
self.conda_cmd,
"remove",
"--yes",
"--prefix",
self.location,
"--all",
]
nox.command.run(cmd, silent=True, log=False)
# Make sure that location is clean
try:
Expand Down Expand Up @@ -231,7 +246,7 @@ def create(self) -> bool:

return False

cmd = ["conda", "create", "--yes", "--prefix", self.location]
cmd = [self.conda_cmd, "create", "--yes", "--prefix", self.location]

cmd.extend(self.venv_params)

Expand Down

0 comments on commit e0d650a

Please sign in to comment.