From 516dd2d7cedfe1309aa033ce335d4c7130f3c693 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Tue, 4 Feb 2025 15:07:05 +0100 Subject: [PATCH 1/2] Escape & on Windows in shell using ^& in `run_subprocess` --- client/ayon_core/lib/execute.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/client/ayon_core/lib/execute.py b/client/ayon_core/lib/execute.py index 95696fd272..35e931a5fc 100644 --- a/client/ayon_core/lib/execute.py +++ b/client/ayon_core/lib/execute.py @@ -122,6 +122,16 @@ def run_subprocess(*args, **kwargs): ) args = (new_arg, ) + # Escape & on Windows in shell using ^& + if ( + kwargs.get("shell") is True + and len(args) == 1 + and isinstance(args[0], str) + and platform.system().lower() == "windows" + ): + new_arg = args[0].replace("&", "^&") + args = (new_arg, ) + # Get environents from kwarg or use current process environments if were # not passed. env = kwargs.get("env") or os.environ From ed0f5c8d7f300a984c139ad7849779c4e781b456 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Tue, 4 Feb 2025 15:20:28 +0100 Subject: [PATCH 2/2] Merge escape if checks + include `COMSPEC` check on Windows --- client/ayon_core/lib/execute.py | 36 ++++++++++++++++----------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/client/ayon_core/lib/execute.py b/client/ayon_core/lib/execute.py index 35e931a5fc..516ea958f5 100644 --- a/client/ayon_core/lib/execute.py +++ b/client/ayon_core/lib/execute.py @@ -108,31 +108,29 @@ def run_subprocess(*args, **kwargs): | getattr(subprocess, "CREATE_NO_WINDOW", 0) ) - # Escape parentheses for bash + # Escape special characters in certain shells if ( kwargs.get("shell") is True and len(args) == 1 and isinstance(args[0], str) - and os.getenv("SHELL") in ("/bin/bash", "/bin/sh") ): - new_arg = ( - args[0] - .replace("(", "\\(") - .replace(")", "\\)") - ) - args = (new_arg, ) + # Escape parentheses for bash + if os.getenv("SHELL") in ("/bin/bash", "/bin/sh"): + new_arg = ( + args[0] + .replace("(", "\\(") + .replace(")", "\\)") + ) + args = (new_arg,) + # Escape & on Windows in shell with `cmd.exe` using ^& + elif ( + platform.system().lower() == "windows" + and os.getenv("COMSPEC").endswith("cmd.exe") + ): + new_arg = args[0].replace("&", "^&") + args = (new_arg, ) - # Escape & on Windows in shell using ^& - if ( - kwargs.get("shell") is True - and len(args) == 1 - and isinstance(args[0], str) - and platform.system().lower() == "windows" - ): - new_arg = args[0].replace("&", "^&") - args = (new_arg, ) - - # Get environents from kwarg or use current process environments if were + # Get environments from kwarg or use current process environments if were # not passed. env = kwargs.get("env") or os.environ # Make sure environment contains only strings