Hide transient command windows

This commit is contained in:
王鹏
2026-10-09 16:11:08 +08:00
parent 11cec27774
commit 6c79ce0b26
4 changed files with 24 additions and 1 deletions

View File

@@ -487,6 +487,7 @@ def _find_listening_pids(port: int) -> set[int]:
text=True,
errors="replace",
shell=False,
creationflags=_hidden_process_creation_flags(),
)
if result.returncode != 0:
message = result.stderr.strip() or f"netstat 退出码 {result.returncode}"
@@ -502,6 +503,7 @@ def _terminate_process_tree(pid: int) -> None:
text=True,
errors="replace",
shell=False,
creationflags=_hidden_process_creation_flags(),
)
if result.returncode != 0:
message = result.stdout.strip() or f"taskkill 退出码 {result.returncode}"
@@ -744,6 +746,12 @@ def _background_creation_flags() -> int:
return subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP
def _hidden_process_creation_flags() -> int:
if os.name != "nt":
return 0
return getattr(subprocess, "CREATE_NO_WINDOW", 0)
def _backend_console_creation_flags() -> int:
if os.name != "nt":
return 0
@@ -904,6 +912,7 @@ def _run_process(
stderr=subprocess.STDOUT,
env=process_environment,
shell=False,
creationflags=_hidden_process_creation_flags(),
)
assert process.stdout is not None
for raw_line in process.stdout:

View File

@@ -214,7 +214,17 @@ def _run_command(
executable = Path(command[0])
if executable.name.lower() in {"catalina.bat", "catalina.sh"} and executable.exists():
environment["CATALINA_HOME"] = str(executable.parent.parent)
return subprocess.run(command, capture_output=True, text=True, timeout=6, shell=False, env=environment)
creation_flags = getattr(subprocess, "CREATE_NO_WINDOW", 0) if os.name == "nt" else 0
return subprocess.run(
command,
capture_output=True,
text=True,
errors="replace",
timeout=6,
shell=False,
env=environment,
creationflags=creation_flags,
)
def _combine_output(completed: subprocess.CompletedProcess[str]) -> str:

View File

@@ -43,6 +43,8 @@ class DeployerTests(unittest.TestCase):
self.assertEqual(output, ["npm ERR! ⚠ dependency failed", "安装失败"])
self.assertNotIn("text", popen_options)
self.assertEqual(popen_options["env"]["JAVA_HOME"], str(Path.cwd() / "fake-jdk"))
expected_flags = getattr(subprocess, "CREATE_NO_WINDOW", 0) if os.name == "nt" else 0
self.assertEqual(popen_options["creationflags"], expected_flags)
self.assertEqual(
Path(popen_options["env"]["PATH"].split(os.pathsep)[0]),
npm.parent,

View File

@@ -100,6 +100,8 @@ class EnvironmentTests(unittest.TestCase):
self.assertTrue(results[0].installed)
self.assertEqual(run.call_args.kwargs["env"]["JAVA_HOME"], str(java.parent.parent))
self.assertTrue(run.call_args.kwargs["env"]["PATH"].startswith(str(java.parent)))
expected_flags = getattr(subprocess, "CREATE_NO_WINDOW", 0) if environment_module.os.name == "nt" else 0
self.assertEqual(run.call_args.kwargs["creationflags"], expected_flags)
def test_manual_maven_failure_is_not_hidden_by_path_failure(self):
with tempfile.TemporaryDirectory() as tmp: