mirror of
https://github.com/tiennm99/serena.git
synced 2026-10-13 20:15:20 +00:00
Apply platform-specific default kwargs to subprocess calls
For now, we only set creationflags=CREATE_NO_WINDOW on Windows
This commit is contained in:
1 parent
f2d81812c7
commit
eaf863df6d
5 files changed
+30
-18
No files matched your search
+6
-4
@@ -31,6 +31,7 @@ from serena.project import Project
|
||||
from serena.tools import ToolRegistry
|
||||
from serena.util.logging import MemoryLogHandler
|
||||
from solidlsp.ls_config import Language
|
||||
from solidlsp.util.subprocess_util import subprocess_kwargs
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -40,18 +41,19 @@ log = logging.getLogger(__name__)
|
||||
def _open_in_editor(path: str) -> None:
|
||||
"""Open the given file in the system's default editor or viewer."""
|
||||
editor = os.environ.get("EDITOR")
|
||||
run_kwargs = subprocess_kwargs()
|
||||
try:
|
||||
if editor:
|
||||
subprocess.run([editor, path], check=False)
|
||||
subprocess.run([editor, path], check=False, **run_kwargs)
|
||||
elif sys.platform.startswith("win"):
|
||||
try:
|
||||
os.startfile(path)
|
||||
except OSError:
|
||||
subprocess.run(["notepad.exe", path], check=False)
|
||||
subprocess.run(["notepad.exe", path], check=False, **run_kwargs)
|
||||
elif sys.platform == "darwin":
|
||||
subprocess.run(["open", path], check=False)
|
||||
subprocess.run(["open", path], check=False, **run_kwargs)
|
||||
else:
|
||||
subprocess.run(["xdg-open", path], check=False)
|
||||
subprocess.run(["xdg-open", path], check=False, **run_kwargs)
|
||||
except Exception as e:
|
||||
print(f"Failed to open {path}: {e}")
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ import subprocess
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from solidlsp.util.subprocess_util import subprocess_kwargs
|
||||
|
||||
|
||||
class ShellCommandResult(BaseModel):
|
||||
stdout: str
|
||||
@@ -36,6 +38,7 @@ def execute_shell_command(command: str, cwd: str | None = None, capture_stderr:
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
cwd=cwd,
|
||||
**subprocess_kwargs(),
|
||||
)
|
||||
|
||||
stdout, stderr = process.communicate()
|
||||
@@ -43,15 +46,7 @@ def execute_shell_command(command: str, cwd: str | None = None, capture_stderr:
|
||||
|
||||
|
||||
def subprocess_check_output(args: list[str], encoding: str = "utf-8", strip: bool = True, timeout: float | None = None) -> str:
|
||||
kwargs = {
|
||||
"stdin": subprocess.DEVNULL,
|
||||
"stderr": subprocess.PIPE,
|
||||
"timeout": timeout,
|
||||
"env": os.environ.copy(),
|
||||
}
|
||||
if platform.system() == "Windows":
|
||||
kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW # type: ignore
|
||||
output = subprocess.check_output(args, **kwargs).decode(encoding) # type: ignore
|
||||
output = subprocess.check_output(args, stdin=subprocess.DEVNULL, stderr=subprocess.PIPE, timeout=timeout, env=os.environ.copy(), **subprocess_kwargs()).decode(encoding) # type: ignore
|
||||
if strip:
|
||||
output = output.strip()
|
||||
return output
|
||||
@@ -9,6 +9,7 @@ from dataclasses import dataclass
|
||||
|
||||
from solidlsp.ls_logger import LanguageServerLogger
|
||||
from solidlsp.ls_utils import FileUtils, PlatformUtils
|
||||
from solidlsp.util.subprocess_util import subprocess_kwargs
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -73,10 +74,8 @@ class RuntimeDependencyCollection:
|
||||
|
||||
@staticmethod
|
||||
def _run_command(command: str | list[str], cwd: str) -> None:
|
||||
kwargs = {}
|
||||
if PlatformUtils.get_platform_id().is_windows():
|
||||
kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW # type: ignore
|
||||
else:
|
||||
kwargs = subprocess_kwargs()
|
||||
if not PlatformUtils.get_platform_id().is_windows():
|
||||
import pwd
|
||||
|
||||
kwargs["user"] = pwd.getpwuid(os.getuid()).pw_name
|
||||
|
||||
@@ -32,6 +32,7 @@ from solidlsp.lsp_protocol_handler.server import (
|
||||
make_request,
|
||||
make_response,
|
||||
)
|
||||
from solidlsp.util.subprocess_util import subprocess_kwargs
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -181,6 +182,8 @@ class SolidLanguageServerHandler:
|
||||
# on Linux/macOS
|
||||
cmd = " ".join(cmd)
|
||||
log.info("Starting language server process via command: %s", self.process_launch_info.cmd)
|
||||
kwargs = subprocess_kwargs()
|
||||
kwargs["start_new_session"] = self.start_independent_lsp_process
|
||||
self.process = subprocess.Popen(
|
||||
cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
@@ -188,8 +191,8 @@ class SolidLanguageServerHandler:
|
||||
stderr=subprocess.PIPE,
|
||||
env=child_proc_env,
|
||||
cwd=self.process_launch_info.cwd,
|
||||
start_new_session=self.start_independent_lsp_process,
|
||||
shell=True,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
# Check if process terminated immediately
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import platform
|
||||
import subprocess
|
||||
|
||||
|
||||
def subprocess_kwargs():
|
||||
"""
|
||||
Returns a dictionary of keyword arguments for subprocess calls, adding platform-specific
|
||||
flags that we want to use consistently.
|
||||
"""
|
||||
kwargs = {}
|
||||
if platform.system() == "Windows":
|
||||
kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW # type: ignore
|
||||
return kwargs
|
||||
Reference in new issue
Block a user