Skip to content

Commit a5d6c0b

Browse files
committed
⚡️ Improve handling of powershell commands
1 parent f95c295 commit a5d6c0b

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

simvue/executor.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_current_shell() -> str | None:
3939
"""Return the users current shell executable."""
4040
try:
4141
_process = psutil.Process(os.getppid())
42-
return _process.exe()
42+
return os.environ.get("SHELL", _process.exe())
4343
except psutil.Error:
4444
return None
4545

@@ -142,6 +142,30 @@ def std_err(self, process_id: str) -> str | None:
142142
with open(err_file) as err:
143143
return err.read() or None
144144

145+
@staticmethod
146+
def _kwarg_assembly(kwargs) -> list[str]:
147+
_arguments: list[str] = []
148+
_shell_is_pwsh: bool = any(
149+
shell in get_current_shell() for shell in ("pwsh", "powershell")
150+
)
151+
for arg, value in kwargs.items():
152+
if arg.startswith("__"):
153+
continue
154+
155+
arg = arg.replace("_", "-")
156+
157+
if len(arg) == 1 or _shell_is_pwsh:
158+
_arguments += (
159+
[f"-{arg}"]
160+
if isinstance(value, bool) and value
161+
else [f"-{arg}", f"{value}"]
162+
)
163+
elif isinstance(value, bool) and value:
164+
_arguments += [f"--{arg}"]
165+
else:
166+
_arguments += [f"--{arg}", f"{value}"]
167+
return _arguments
168+
145169
def add_process(
146170
self,
147171
identifier: str,
@@ -252,22 +276,7 @@ def callback_function(status_code: int, std_out: str, std_err: str) -> None:
252276
if input_file:
253277
command += [f"{input_file}"]
254278

255-
for arg, value in kwargs.items():
256-
if arg.startswith("__"):
257-
continue
258-
259-
arg = arg.replace("_", "-")
260-
261-
if len(arg) == 1:
262-
command += (
263-
[f"-{arg}"]
264-
if isinstance(value, bool) and value
265-
else [f"-{arg}", f"{value}"]
266-
)
267-
elif isinstance(value, bool) and value:
268-
command += [f"--{arg}"]
269-
else:
270-
command += [f"--{arg}", f"{value}"]
279+
command += self._kwarg_assembly(kwargs)
271280

272281
command += pos_args
273282

0 commit comments

Comments
 (0)