From 4216a06cd84fbcb60a3f99d11eac5951775b05cb Mon Sep 17 00:00:00 2001 From: psakiev Date: Tue, 12 Nov 2024 13:16:59 -0700 Subject: [PATCH] Add build-env --dive --- lib/spack/spack/cmd/common/env_utility.py | 7 +++-- lib/spack/spack/util/shell_detection.py | 32 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 lib/spack/spack/util/shell_detection.py diff --git a/lib/spack/spack/cmd/common/env_utility.py b/lib/spack/spack/cmd/common/env_utility.py index 57988bcc5a6..f2f27172537 100644 --- a/lib/spack/spack/cmd/common/env_utility.py +++ b/lib/spack/spack/cmd/common/env_utility.py @@ -17,6 +17,7 @@ from spack.cmd.common import arguments from spack.context import Context from spack.util.environment import dump_environment, pickle_environment +from spack.util.shell_detection import active_shell_type def setup_parser(subparser): @@ -144,8 +145,10 @@ def emulate_env_utility(cmd_name, context: Context, args): os.execvp(cmd[0], cmd) if args.dive: - mods.extend(spack.prompt.prompt_modifications(f"{spec.name}-build-env", cmd[0])) - os.execvp(cmd[0], [cmd[0]]) + shell = active_shell_type() + mods.extend(spack.prompt.prompt_modifications(f"{spec.name}-{str(context)}-env", shell)) + mods.apply_modifications() + os.execvp(shell, [shell]) elif not bool(args.pickle or args.dump): # If no command or dump/pickle option then act like the "env" command diff --git a/lib/spack/spack/util/shell_detection.py b/lib/spack/spack/util/shell_detection.py new file mode 100644 index 00000000000..96a2b968c97 --- /dev/null +++ b/lib/spack/spack/util/shell_detection.py @@ -0,0 +1,32 @@ +# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other +# Spack Project Developers. See the top-level COPYRIGHT file for details. +# +# SPDX-License-Identifier: (Apache-2.0 OR MIT) + +import os +import platform +import subprocess + +from spack.error import SpackError + + +def active_shell_type(env=os.environ): + if platform.system() == "Windows": + if "POWERSHELL" in env: + return "ps1" + else: + try: + output = subprocess.run("powershell -Command \"echo $PSVersionTable\"", shell=True, check=True, text=True) + if "PSVersion" in output: + return "ps1" + else: + pass + except subprocess.CalledProcessError: + pass + raise SpackError("Unknown shell type being used on Windows") + else: + shell = env.get("SHELL", None) + if shell: + return shell + else: + raise SpackError("No shell type detected for the Unix process")