agentremote-cli 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
remotecli/shell.py ADDED
@@ -0,0 +1,71 @@
1
+ """
2
+ remotecli shell — 远程 Shell 执行子命令.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import re
8
+ from typing import Any, Dict
9
+
10
+ from .client import RemoteCliClient
11
+
12
+
13
+ def _repair_powershell_current_object_expansion(script: str) -> str:
14
+ """Repair common outer-shell expansion of PowerShell's $_ in Get-StartApps filters."""
15
+ if "Get-StartApps" not in script or "Where-Object" not in script or "-like" not in script:
16
+ return script
17
+
18
+ matches = list(re.finditer(r"-like\s+(?P<quote>['\"])(?P<pattern>.*?)(?P=quote)", script))
19
+ if not matches:
20
+ return script
21
+
22
+ has_nested_expansion = (
23
+ re.search(r"Where-Object\s*\{.*Where-Object\s*\{.*\.Name\s+-like\b", script) is not None
24
+ )
25
+ if ("$_.Name" in script or "$PSItem.Name" in script) and not has_nested_expansion:
26
+ return script
27
+
28
+ has_expanded_current_object = (
29
+ re.search(r"Where-Object\s*\{\s*\.[A-Za-z_][\w]*\s+-like\b", script) is not None
30
+ or re.search(r"Where-Object\s*\{\s*[^${};|]+\.Name\s+-like\b", script) is not None
31
+ or has_nested_expansion
32
+ )
33
+ if not has_expanded_current_object:
34
+ return script
35
+
36
+ match = matches[-1]
37
+ quote = match.group("quote")
38
+ pattern = match.group("pattern")
39
+ return f"Get-StartApps | Where-Object {{$_.Name -like {quote}{pattern}{quote}}}"
40
+
41
+
42
+ class ShellCommands:
43
+ """远程 Shell 命令构建器。"""
44
+
45
+ def __init__(self, client: RemoteCliClient) -> None:
46
+ self._client = client
47
+ self._capability_id = "native.shell"
48
+
49
+ def exec(self, cmd: str, cwd: str = "", timeout_sec: int = 60) -> Dict[str, Any]:
50
+ """执行单条 shell 命令。"""
51
+ args: Dict[str, Any] = {"cmd": cmd}
52
+ if cwd:
53
+ args["cwd"] = cwd
54
+ return self._client.submit(self._capability_id, "exec", args, timeout_sec=timeout_sec)
55
+
56
+ def exec_script(self, script: str, shell: str = "", cwd: str = "",
57
+ timeout_sec: int = 60) -> Dict[str, Any]:
58
+ """执行多行脚本 (写入临时文件, 执行后清理)。"""
59
+ if shell in ("powershell", "pwsh"):
60
+ script = _repair_powershell_current_object_expansion(script)
61
+
62
+ args: Dict[str, Any] = {"script": script}
63
+ if shell:
64
+ args["shell"] = shell
65
+ if cwd:
66
+ args["cwd"] = cwd
67
+ return self._client.submit(self._capability_id, "exec-script", args, timeout_sec=timeout_sec)
68
+
69
+ def info(self) -> Dict[str, Any]:
70
+ """获取 Shell/OS 环境信息。"""
71
+ return self._client.submit(self._capability_id, "info", {})