Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions py_env_create
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,41 @@ if [ ! -f $condafile ]; then
exit -1
fi

# Given two version number strings (unsigned integers separated by periods), echo "true" if the
# second one is "greater than or equal to" the first. Otherwise, echo "false".
# Based on https://unix.stackexchange.com/a/285928/208738
function is_version2_ge_version1 {
requiredver=$1
currentver=$2
if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
echo "true"
else
echo "false"
fi
}

#
# Handle an environment that already exists
#
function conda_env_exists {
${condamamba} env list | grep -oE "/$1$" | wc -l
# Not using ${condamamba} because, at least as of mamba 2.3.1, it doesn't always show environment
# names. See https://github.com/mamba-org/mamba/issues/4045
conda env list | grep -oE "/$1$" | wc -l
}
function rename_existing_env {
if [[ "$CONDA_DEFAULT_ENV" == *"$1" ]]; then
echo "Not going to let you rename the currently active env" >&2
exit 1
elif [[ $(conda_env_exists $2) -eq 0 ]]; then
echo "Renaming $1 to $2 (this will take a few minutes)..."
yes_tmp=${yes/yes/force}
conda_version=$(conda --version | cut -d" " -f2)
if [[ "$(is_version2_ge_version1 25.3.0 ${conda_version})" == "true" ]]; then
yes_tmp=$yes
else
# Before conda 25.3.0, conda rename needed --force instead of --yes
# https://github.com/conda/conda/blob/408abc3a3826c80750883accb8896005e3ac97b2/CHANGELOG.md?plain=1#L291
yes_tmp=${yes/yes/force}
fi
# Use conda instead of $condamamba because, at least as of mamba 1.5.9 / conda 24.7.1,
# rename is not supported through mamba.
if [[ "${quiet}" == "--quiet" ]]; then
Expand Down
19 changes: 14 additions & 5 deletions python/ctsm/test/test_sys_py_env_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ def setUp(self):
self.py_env_create = os.path.join(path_to_ctsm_root(), "py_env_create")
assert os.path.exists(self.py_env_create)

# Get path to testing condafile
# Get path to testing conda/mambafile:
# conda needs a completely empty file (# comments okay) as of 25.5.1, but mamba as of 2.3.1
# needs at least some YML structure.
self.empty_condafile = os.path.join(path_to_ctsm_root(), "python", "empty.txt")
assert os.path.exists(self.empty_condafile)
self.empty_mambafile = os.path.join(path_to_ctsm_root(), "python", "empty.yml")

# Set up other variables
self.env_names = []
Expand All @@ -89,7 +91,12 @@ def _create_empty_env(self, check=None, extra_args=None, expect_error=False, new
self.env_names.append(get_unique_env_name(5))

# Form and run command
cmd = [self.py_env_create, "-n", self.env_names[-1], "-f", self.empty_condafile, "--yes"]
if extra_args is not None and ("-m" in extra_args or "--mamba" in extra_args):
empty_file = self.empty_mambafile
else:
empty_file = self.empty_condafile
assert os.path.exists(empty_file)
cmd = [self.py_env_create, "-n", self.env_names[-1], "-f", empty_file, "--yes"]
if extra_args:
cmd += extra_args
out = subprocess.run(cmd, capture_output=True, text=True, check=False)
Expand Down Expand Up @@ -327,7 +334,8 @@ def test_complete_py_env_create(self):
raise e
env_list = get_conda_envs()
for env_name in self.env_names:
assert does_env_exist(env_name, env_list)
if not does_env_exist(env_name, env_list):
raise AssertionError(f"environment not found: {env_name}")

def test_complete_py_env_create_mamba(self):
"""
Expand Down Expand Up @@ -358,7 +366,8 @@ def test_complete_py_env_create_mamba(self):
raise e
env_list = get_conda_envs()
for env_name in self.env_names:
assert does_env_exist(env_name, env_list)
if not does_env_exist(env_name, env_list):
raise AssertionError(f"environment not found: {env_name}")


if __name__ == "__main__":
Expand Down
7 changes: 7 additions & 0 deletions python/empty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# An empty file for use in testing py_env_create with mamba, which needs some YML structure
# (as opposed to conda, which can use a truly empty file)
#
channels:
- conda-forge
- defaults
dependencies:
Loading