mdevenv: powershell <7.0 is EOL, prefer using pwsh.exe

If neither pwsh.exe nor powershell.exe works, fallback to cmd.exe. This
script could be failing because of virus scanner.
pull/10838/head
Xavier Claessens 2 years ago committed by Xavier Claessens
parent bb4c8c07f8
commit 31a6633e62
  1. 15
      mesonbuild/mdevenv.py

@ -13,6 +13,8 @@ import typing as T
if T.TYPE_CHECKING: if T.TYPE_CHECKING:
from .backends import InstallData from .backends import InstallData
POWERSHELL_EXES = {'pwsh.exe', 'powershell.exe'}
def add_arguments(parser: argparse.ArgumentParser) -> None: def add_arguments(parser: argparse.ArgumentParser) -> None:
parser.add_argument('-C', dest='wd', action=RealPathAction, parser.add_argument('-C', dest='wd', action=RealPathAction,
help='Directory to cd into before running') help='Directory to cd into before running')
@ -21,12 +23,17 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
parser.add_argument('command', nargs=argparse.REMAINDER, parser.add_argument('command', nargs=argparse.REMAINDER,
help='Command to run in developer environment (default: interactive shell)') help='Command to run in developer environment (default: interactive shell)')
def get_windows_shell() -> str: def get_windows_shell() -> T.Optional[str]:
mesonbuild = Path(__file__).parent mesonbuild = Path(__file__).parent
script = mesonbuild / 'scripts' / 'cmd_or_ps.ps1' script = mesonbuild / 'scripts' / 'cmd_or_ps.ps1'
command = ['powershell.exe', '-noprofile', '-executionpolicy', 'bypass', '-file', str(script)] for shell in POWERSHELL_EXES:
try:
command = [shell, '-noprofile', '-executionpolicy', 'bypass', '-file', str(script)]
result = subprocess.check_output(command) result = subprocess.check_output(command)
return result.decode().strip() return result.decode().strip()
except (subprocess.CalledProcessError, OSError):
pass
return None
def reduce_winepath(env: T.Dict[str, str]) -> None: def reduce_winepath(env: T.Dict[str, str]) -> None:
winepath = env.get('WINEPATH') winepath = env.get('WINEPATH')
@ -145,7 +152,9 @@ def run(options: argparse.Namespace) -> int:
args = [shell_env] args = [shell_env]
elif is_windows(): elif is_windows():
shell = get_windows_shell() shell = get_windows_shell()
if shell in ['powershell.exe', 'pwsh.exe']: if not shell:
mlog.warning('Failed to determine Windows shell, fallback to cmd.exe')
if shell in POWERSHELL_EXES:
args = [shell, '-NoLogo', '-NoExit'] args = [shell, '-NoLogo', '-NoExit']
prompt = f'function global:prompt {{ "{prompt_prefix} PS " + $PWD + "> "}}' prompt = f'function global:prompt {{ "{prompt_prefix} PS " + $PWD + "> "}}'
args += ['-Command', prompt] args += ['-Command', prompt]

Loading…
Cancel
Save