MCPcopy Create free account
hub / github.com/Louisym/MiniCC / execute_bash

Function execute_bash

tutorials/14_bash_engine_deep_dive.py:454–501  ·  view source on GitHub ↗

执行 Bash 命令 — 源码 bash.rs:67-100 这是入口函数。它处理三种情况: 1. 后台执行 → spawn 后立即返回 PID 2. 有超时 → 用 asyncio.wait_for 包装 3. 普通执行 → 同步等待完成 在 Rust 源码中,它创建一个 tokio runtime 来支持异步超时。 Python 中我们用 asyncio 模拟。

(inp: BashCommandInput)

Source from the content-addressed store, hash-verified

452
453
454def execute_bash(inp: BashCommandInput) -> BashCommandOutput:
455 """执行 Bash 命令 — 源码 bash.rs:67-100
456
457 这是入口函数。它处理三种情况:
458 1. 后台执行 → spawn 后立即返回 PID
459 2. 有超时 → 用 asyncio.wait_for 包装
460 3. 普通执行 → 同步等待完成
461
462 在 Rust 源码中,它创建一个 tokio runtime 来支持异步超时。
463 Python 中我们用 asyncio 模拟。
464 """
465 cwd = Path.cwd()
466
467 # 第一步: 解析沙箱配置
468 config = SandboxConfig() # 实际从 ConfigLoader 加载
469 request = resolve_request(
470 config,
471 enabled_override=(not inp.dangerously_disable_sandbox
472 if inp.dangerously_disable_sandbox is not None else None),
473 namespace_override=inp.namespace_restrictions,
474 network_override=inp.isolate_network,
475 filesystem_mode_override=inp.filesystem_mode,
476 allowed_mounts_override=inp.allowed_mounts,
477 )
478 sandbox_status = resolve_sandbox_status(request, cwd)
479
480 # ========== 路径 1: 后台执行 ==========
481 # 源码 bash.rs:71-96
482 if inp.run_in_background:
483 cmd_args, env = prepare_command(inp.command, cwd, sandbox_status, create_dirs=False)
484 # 关键: stdin/stdout/stderr 全部设为 DEVNULL
485 # 这让进程完全脱离——不占用终端、不阻塞管道
486 process = subprocess.Popen(
487 cmd_args,
488 stdin=subprocess.DEVNULL, # 不接受输入
489 stdout=subprocess.DEVNULL, # 不捕获输出
490 stderr=subprocess.DEVNULL, # 不捕获错误
491 cwd=str(cwd),
492 env=env,
493 )
494 return BashCommandOutput(
495 background_task_id=str(process.pid),
496 sandbox_status=sandbox_status,
497 no_output_expected=True,
498 )
499
500 # ========== 路径 2 & 3: 前台执行(有/无超时)==========
501 return _execute_bash_sync(inp, sandbox_status, cwd)
502
503
504def _execute_bash_sync(

Callers 1

demo_full_workflowFunction · 0.70

Calls 6

resolve_requestFunction · 0.85
_execute_bash_syncFunction · 0.85
SandboxConfigClass · 0.70
resolve_sandbox_statusFunction · 0.70
prepare_commandFunction · 0.70
BashCommandOutputClass · 0.70

Tested by

no test coverage detected