-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
58 lines (48 loc) · 2.21 KB
/
Copy pathutils.py
File metadata and controls
58 lines (48 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys
import subprocess
def get_cmdline(pid: int) -> list[str]:
"""Возвращает список аргументов командной строки процесса по PID."""
platform = sys.platform
# Linux / Android
if platform.startswith("linux") or platform.startswith('android'):
with open(f"/proc/{pid}/cmdline", "rb") as f:
data = f.read()
# Аргументы разделены нулевыми байтами
return data.rstrip(b"\x00").split(b"\x00")
# macOS / BSD
elif platform == "darwin" or platform.startswith("freebsd"):
result = subprocess.run(
["ps", "-p", str(pid), "-o", "command="],
capture_output=True, text=True, check=True
)
return result.stdout.strip().split()
# Windows
elif platform == "win32":
# WMIC или PowerShell — оба встроены
try:
result = subprocess.run(
["wmic", "process", "where", f"ProcessId={pid}",
"get", "CommandLine", "/format:value"],
capture_output=True, text=True, check=True
)
for line in result.stdout.splitlines():
if line.startswith("CommandLine="):
cmdline = line[len("CommandLine="):].strip()
return cmdline.split() if cmdline else []
except FileNotFoundError:
# wmic убран в Windows 11 24H2+, фолбэк на PowerShell
result = subprocess.run(
["powershell", "-NoProfile", "-Command",
f"(Get-Process -Id {pid}).CommandLine"],
capture_output=True, text=True, check=True
)
return result.stdout.strip().split()
else:
raise NotImplementedError(f"Unsupported platform: {platform}")
def run_detached_process(cmd: list, **kwargs):
platform = sys.platform
if (platform.startswith("linux") or platform.startswith('android') or
platform == "darwin" or platform.startswith("freebsd")):
subprocess.Popen(cmd, start_new_session=True, **kwargs)
elif platform == "win32":
subprocess.Popen(cmd, creationflags=subprocess.DETACHED_PROCESS, **kwargs)