Skip to content

Commit e38bfbe

Browse files
adapt to differences in mamba versions
1 parent 3ae0c47 commit e38bfbe

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

snakemake/deployment/conda.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import shutil
2626
from abc import ABC, abstractmethod
2727

28+
from packaging.version import Version
2829

2930
from snakemake.exceptions import CreateCondaEnvironmentException, WorkflowError
3031
from snakemake.logging import logger
@@ -374,6 +375,21 @@ def execute_deployment_script(self, env_file, deploy_file):
374375
text=True,
375376
)
376377

378+
def _create_env_args(self, mode: str):
379+
args = [
380+
"--quiet",
381+
"--no-shortcuts" if ON_WINDOWS else "",
382+
]
383+
384+
if mode != "yaml":
385+
args.append("--yes")
386+
if self.conda.frontend == "conda" or (
387+
self.conda.frontend == "mamba"
388+
and self.conda.frontend_version < Version("2.0.0")
389+
):
390+
args.append("--no-default-packages")
391+
return args
392+
377393
def create(self, dryrun=False):
378394
"""Create the conda environment."""
379395
from snakemake.shell import shell
@@ -499,12 +515,9 @@ def create(self, dryrun=False):
499515
[
500516
self.frontend,
501517
"create",
502-
"--quiet",
503-
"--no-shortcuts" if ON_WINDOWS else "",
504-
"--yes",
505-
"--no-default-packages",
506518
f"--prefix '{env_path}'",
507519
]
520+
+ self._create_env_args(mode="archive")
508521
+ packages
509522
)
510523
if self._container_img:
@@ -534,22 +547,18 @@ def create_env(env_file, filetype="yaml"):
534547
)
535548

536549
subcommand = [self.frontend]
537-
yes_flag = ["--yes"]
538550
if filetype == "yaml":
539551
subcommand.append("env")
540-
yes_flag = []
541552

542553
cmd = (
543554
strict_priority
544555
+ subcommand
545556
+ [
546557
"create",
547-
"--quiet",
548-
"--no-default-packages",
549558
f'--file "{target_env_file}"',
550559
f'--prefix "{env_path}"',
551560
]
552-
+ yes_flag
561+
+ self._create_env_args(mode=filetype)
553562
)
554563
cmd = " ".join(cmd)
555564
if self._container_img:
@@ -692,6 +701,8 @@ def __init__(
692701
raise ValueError("Frontend must be specified if check is True.")
693702
self._check()
694703

704+
self.frontend_version = self._get_version(self.frontend)
705+
695706
@property
696707
def is_initialized(self):
697708
return hasattr(self, "prefix_path")
@@ -766,21 +777,31 @@ def _check(self):
766777
"Unable to check conda installation:\n" + e.stderr.decode()
767778
)
768779

769-
def _check_version(self):
780+
def _get_version(self, frontend: str) -> Version:
770781
from snakemake.shell import shell
771-
from packaging.version import Version
772782

773783
version = shell.check_output(
774-
self._get_cmd("conda --version"), stderr=subprocess.PIPE, text=True
784+
self._get_cmd(f"{frontend} --version"), stderr=subprocess.PIPE, text=True
775785
)
786+
787+
def parse_version(version_matches):
788+
return Version(version_matches[0])
789+
776790
version_matches = re.findall(r"\d+.\d+.\d+", version)
777791
if len(version_matches) != 1:
792+
if frontend == "mamba":
793+
version_matches = re.findall(r"mamba (\d+.\d+.\d+)", version)
794+
if len(version_matches) == 1:
795+
return parse_version(version_matches)
778796
raise WorkflowError(
779-
f"Unable to determine conda version. 'conda --version' returned {version}"
797+
f"Unable to determine conda version. '{frontend} --version' returned:\n{version}"
780798
)
781799
else:
782-
version = version_matches[0]
783-
if Version(version) < Version("4.2"):
800+
return parse_version(version_matches)
801+
802+
def _check_version(self):
803+
version = self._get_version("conda")
804+
if version < Version("4.2"):
784805
raise CreateCondaEnvironmentException(
785806
f"Conda must be version 4.2 or later, found version {version}."
786807
)

0 commit comments

Comments
 (0)