[RFC PATCH 08/13] tui: The _suspend_to_shell function assumes a POSIX environment, relying on $SHELL and shell-specific arguments (e.g., --rcfile) that are invalid on Windows.
Adrian Neftali Sanchez <[email protected]>
| Newsgroups | org.kernel.linux.tools |
|---|---|
| Message-ID | <[email protected]> |
Refactor the execution flow to branch on sys.platform. On Windows, launch the native interpreter via COMSPEC (defaulting to cmd.exe). Move the existing POSIX-specific logic for bash/zsh into an explicit else-block to ensure clean separation of platform execution paths. This enables Windows support while preventing POSIX shell configuration side effects on non-POSIX systems. Signed-off-by: Adrian Neftali Sanchez <[email protected]> --- src/b4/tui/_common.py | 58 ++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/src/b4/tui/_common.py b/src/b4/tui/_common.py index cdd3818..6c8042c 100644 --- a/src/b4/tui/_common.py +++ b/src/b4/tui/_common.py @@ -10,6 +10,7 @@ __author__ = 'Konstantin Ryabitsev <[email protected]>' import email.utils import os import subprocess +import sys import tempfile import unicodedata from collections import defaultdict @@ -198,36 +199,41 @@ def _suspend_to_shell(hint: str = 'b4', cwd: Optional[str] = None) -> None: logger.info('When done, Ctrl-d to return to review UI.') logger.info('---') - shell = os.environ.get('SHELL', '/bin/sh') - shellname = os.path.basename(shell) env = os.environ.copy() env['B4_REVIEW'] = hint - if shellname == 'bash': - bashrc = os.path.expanduser('~/.bashrc') - source = f'[ -f {bashrc} ] && . {bashrc}\n' - source += f'PS1="({hint}) $PS1"\n' - with tempfile.NamedTemporaryFile( - mode='w', prefix='b4-shell-', suffix='.sh', delete=False - ) as rcf: - rcf.write(source) - rcfile = rcf.name - try: - subprocess.run([shell, '--rcfile', rcfile], env=env, cwd=cwd) - finally: - os.unlink(rcfile) - elif shellname == 'zsh': - real_zdotdir = os.environ.get('ZDOTDIR', os.path.expanduser('~')) - with tempfile.TemporaryDirectory(prefix='b4-shell-') as tmpdir: - zshrc = os.path.join(tmpdir, '.zshrc') - with open(zshrc, 'w') as f: - f.write(f'ZDOTDIR="{real_zdotdir}"\n') - f.write('[ -f "$ZDOTDIR/.zshrc" ] && . "$ZDOTDIR/.zshrc"\n') - f.write(f'PS1="({hint}) $PS1"\n') - env['ZDOTDIR'] = tmpdir - subprocess.run([shell], env=env, cwd=cwd) - else: + if sys.platform == 'win32': + shell = os.environ.get('COMSPEC', 'cmd.exe') subprocess.run([shell], env=env, cwd=cwd) + else: + shell = os.environ.get('SHELL', '/bin/sh') + shellname = os.path.basename(shell) + + if shellname == 'bash': + bashrc = os.path.expanduser('~/.bashrc') + source = f'[ -f {bashrc} ] && . {bashrc}\n' + source += f'PS1="({hint}) $PS1"\n' + with tempfile.NamedTemporaryFile( + mode='w', prefix='b4-shell-', suffix='.sh', delete=False + ) as rcf: + rcf.write(source) + rcfile = rcf.name + try: + subprocess.run([shell, '--rcfile', rcfile], env=env, cwd=cwd) + finally: + os.unlink(rcfile) + elif shellname == 'zsh': + real_zdotdir = os.environ.get('ZDOTDIR', os.path.expanduser('~')) + with tempfile.TemporaryDirectory(prefix='b4-shell-') as tmpdir: + zshrc = os.path.join(tmpdir, '.zshrc') + with open(zshrc, 'w') as f: + f.write(f'ZDOTDIR="{real_zdotdir}"\n') + f.write('[ -f "$ZDOTDIR/.zshrc" ] && . "$ZDOTDIR/.zshrc"\n') + f.write(f'PS1="({hint}) $PS1"\n') + env['ZDOTDIR'] = tmpdir + subprocess.run([shell], env=env, cwd=cwd) + else: + subprocess.run([shell], env=env, cwd=cwd) def _addrs_to_lines(header_str: str) -> str: -- 2.45.0.windows.1