From 3c641c85097cef24cbea238f2254ced90f57bd2c Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Mon, 6 Nov 2023 07:53:26 +0100 Subject: spack env activate: create & activate default environment without args (#40756) This PR implements the concept of "default environment", which doesn't have to be created explicitly. The aim is to lower the barrier for adopting environments. To (create and) activate the default environment, run ``` $ spack env activate ``` This mimics the behavior of ``` $ cd ``` which brings you to your home directory. This is not a breaking change, since `spack env activate` without arguments currently errors. It is similar to the already existing `spack env activate --temp` command which always creates an env in a temporary directory, the difference is that the default environment is a managed / named environment named `default`. The name `default` is not a reserved name, it's just that `spack env activate` creates it for you if you don't have it already. With this change, you can get started with environments faster: ``` $ spack env activate [--prompt] $ spack install --add x y z ``` instead of ``` $ spack env create default ==> Created environment 'default in /Users/harmenstoppels/spack/var/spack/environments/default ==> You can activate this environment with: ==> spack env activate default $ spack env activate [--prompt] default $ spack install --add x y z ``` Notice that Spack supports switching (but not stacking) environments, so the parallel with `cd` is pretty clear: ``` $ spack env activate named_env $ spack env status ==> In environment named_env $ spack env activate $ spack env status ==> In environment default ``` --- lib/spack/spack/cmd/env.py | 25 +++++++++++++++++++++---- lib/spack/spack/test/cmd/env.py | 19 +++++++++++++++++++ share/spack/qa/setup-env-test.fish | 6 +++++- share/spack/qa/setup-env-test.sh | 6 +++++- share/spack/setup-env.sh | 3 +-- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/lib/spack/spack/cmd/env.py b/lib/spack/spack/cmd/env.py index d75cf6b262..bf1f29d558 100644 --- a/lib/spack/spack/cmd/env.py +++ b/lib/spack/spack/cmd/env.py @@ -5,6 +5,7 @@ import argparse import os +import shlex import shutil import sys import tempfile @@ -144,10 +145,13 @@ def create_temp_env_directory(): return tempfile.mkdtemp(prefix="spack-") -def env_activate(args): - if not args.activate_env and not args.dir and not args.temp: - tty.die("spack env activate requires an environment name, directory, or --temp") +def _tty_info(msg): + """tty.info like function that prints the equivalent printf statement for eval.""" + decorated = f'{colorize("@*b{==>}")} {msg}\n' + print(f"printf {shlex.quote(decorated)};") + +def env_activate(args): if not args.shell: spack.cmd.common.shell_init_instructions( "spack env activate", " eval `spack env activate {sh_arg} [...]`" @@ -160,12 +164,25 @@ def env_activate(args): env_name_or_dir = args.activate_env or args.dir + # When executing `spack env activate` without further arguments, activate + # the default environment. It's created when it doesn't exist yet. + if not env_name_or_dir and not args.temp: + short_name = "default" + if not ev.exists(short_name): + ev.create(short_name) + action = "Created and activated" + else: + action = "Activated" + env_path = ev.root(short_name) + _tty_info(f"{action} default environment in {env_path}") + # Temporary environment - if args.temp: + elif args.temp: env = create_temp_env_directory() env_path = os.path.abspath(env) short_name = os.path.basename(env_path) ev.create_in_dir(env).write(regenerate=False) + _tty_info(f"Created and activated temporary environment in {env_path}") # Managed environment elif ev.exists(env_name_or_dir) and not args.dir: diff --git a/lib/spack/spack/test/cmd/env.py b/lib/spack/spack/test/cmd/env.py index 308e0b0e90..e291432a0f 100644 --- a/lib/spack/spack/test/cmd/env.py +++ b/lib/spack/spack/test/cmd/env.py @@ -2924,6 +2924,25 @@ def test_activate_temp(monkeypatch, tmpdir): assert ev.is_env_dir(str(tmpdir)) +def test_activate_default(monkeypatch): + """Tests whether `spack env activate` creates / activates the default + environment""" + assert not ev.exists("default") + + # Activating it the first time should create it + env("activate", "--sh") + env("deactivate", "--sh") + assert ev.exists("default") + + # Activating it while it already exists should work + env("activate", "--sh") + env("deactivate", "--sh") + assert ev.exists("default") + + env("remove", "-y", "default") + assert not ev.exists("default") + + def test_env_view_fail_if_symlink_points_elsewhere(tmpdir, install_mockery, mock_fetch): view = str(tmpdir.join("view")) # Put a symlink to an actual directory in view diff --git a/share/spack/qa/setup-env-test.fish b/share/spack/qa/setup-env-test.fish index 6474917b70..589f4cbfa8 100755 --- a/share/spack/qa/setup-env-test.fish +++ b/share/spack/qa/setup-env-test.fish @@ -371,7 +371,6 @@ spt_contains " spack env list " spack env list --help title 'Testing `spack env activate`' spt_contains "No such environment:" spack env activate no_such_environment -spt_contains "env activate requires an environment " spack env activate spt_contains "usage: spack env activate " spack env activate -h spt_contains "usage: spack env activate " spack env activate --help @@ -415,6 +414,11 @@ spt_contains 'spack_test_2_env' 'fish' '-c' 'echo $PATH' spt_does_not_contain 'spack_test_env' 'fish' '-c' 'echo $PATH' despacktivate +echo "Testing default environment" +spack env activate +contains "In environment default" spack env status +despacktivate + echo "Correct error exit codes for activate and deactivate" spt_fails spack env activate nonexisiting_environment spt_fails spack env deactivate diff --git a/share/spack/qa/setup-env-test.sh b/share/spack/qa/setup-env-test.sh index 4172a40155..b26619b9cd 100755 --- a/share/spack/qa/setup-env-test.sh +++ b/share/spack/qa/setup-env-test.sh @@ -140,7 +140,6 @@ contains " spack env list " spack env list --help title 'Testing `spack env activate`' contains "No such environment:" spack env activate no_such_environment -contains "env activate requires an environment " spack env activate contains "usage: spack env activate " spack env activate -h contains "usage: spack env activate " spack env activate --help @@ -197,6 +196,11 @@ contains "spack_test_2_env" sh -c 'echo $PATH' does_not_contain "spack_test_env" sh -c 'echo $PATH' despacktivate +echo "Testing default environment" +spack env activate +contains "In environment default" spack env status +despacktivate + echo "Correct error exit codes for activate and deactivate" fails spack env activate nonexisiting_environment fails spack env deactivate diff --git a/share/spack/setup-env.sh b/share/spack/setup-env.sh index 91a601e652..a42882266c 100755 --- a/share/spack/setup-env.sh +++ b/share/spack/setup-env.sh @@ -126,8 +126,7 @@ _spack_shell_wrapper() { # Space needed here to differentiate between `-h` # argument and environments with "-h" in the name. # Also see: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion - if [ -z ${1+x} ] || \ - [ "${_a#* --sh}" != "$_a" ] || \ + if [ "${_a#* --sh}" != "$_a" ] || \ [ "${_a#* --csh}" != "$_a" ] || \ [ "${_a#* -h}" != "$_a" ] || \ [ "${_a#* --help}" != "$_a" ]; -- cgit v1.2.3-70-g09d2