Metadata-Version: 2.4
Name: funshell
Version: 2.0.0
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Rust
License-File: LICENSE
Summary: Cross-platform shell execution and port/process query & kill library powered by Rust
Keywords: shell,subprocess,process,port,kill
Author-email: 牛哥 <niuliangtao@qq.com>, farfarfun <farfarfun@qq.com>
Maintainer-email: 牛哥 <niuliangtao@qq.com>, farfarfun <farfarfun@qq.com>
License-Expression: MIT
Requires-Python: >=3.12
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Organization, https://github.com/farfarfun
Project-URL: Releases, https://github.com/farfarfun/funshell/releases
Project-URL: Repository, https://github.com/farfarfun/funshell

# funshell

跨平台 shell 命令执行与端口/进程查杀工具。
核心由 Rust 实现，同时提供 **零依赖 Python 库**、**独立原生命令行二进制** 和 **Rust 核心库** 三种形态。

- Linux 下端口查询直接读取 `/proc/net/*` 与 `/proc/<pid>/fd`，**不依赖 `lsof` / `ss` / `ps` / `kill`**，精简容器开箱即用；无结果时自动回退 `lsof` / `ss`。
- 进程列表枚举基于原生系统接口，信号发送直接调用原生系统调用，支持 Linux / macOS / Windows。
- Python 侧零运行时依赖，执行阻塞命令时自动释放 GIL。

## 安装

命令行是独立的原生二进制，**不需要 Python 环境**。

`pip install funshell` 只提供 Python API，**不带 `funshell` 命令**，避免与系统 PATH 冲突并减小 wheel 体积。

### apt（Debian / Ubuntu）

```bash
curl -fsSL https://farfarfun.github.io/packages/farfarfun.gpg \
  | sudo tee /usr/share/keyrings/farfarfun.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/farfarfun.gpg] https://farfarfun.github.io/packages/deb stable main" \
  | sudo tee /etc/apt/sources.list.d/farfarfun.list
sudo apt update && sudo apt install funshell
```

### Homebrew（macOS / Linux）

```bash
brew tap farfarfun/packages https://github.com/farfarfun/packages
brew trust farfarfun/packages
brew install funshell
```

### cargo

```bash
cargo install funshell
```

### 裸二进制

从 [Releases](https://github.com/farfarfun/funshell/releases) 下载对应平台的预编译包（Linux 优先 musl 静态版）：

```bash
curl -fsSL https://github.com/farfarfun/funshell/releases/latest/download/funshell-x86_64-unknown-linux-musl.tar.gz \
  | tar -xz
sudo install -m 0755 funshell /usr/local/bin/
```

### 作为 Python 库

```bash
pip install funshell
# 或
uv add funshell
```

需要 Python 3.12 或更高版本，零运行时依赖，各平台提供预编译 `abi3-py312` wheel。

### 作为 Rust 库

```toml
[dependencies]
funshell-core = "0.1"
```

## 命令行

```bash
# 查询占用 8080 端口的进程
funshell port 8080

# 查询并杀掉
funshell port 8080 --kill

# 按进程名（含任一关键字）查询
funshell name node code-server

# 按进程名查询并用指定信号杀掉
funshell name node --kill --sig TERM

# 执行 shell 命令（支持 --cwd / --timeout / --capture）
funshell run "echo hello world"
funshell run "git status" --cwd /path/to/repo --timeout 10 --capture
```

**注意**：`name` 子命令按完整命令行（含参数）做大小写不敏感子串匹配，终止前建议先不带 `--kill` 查看一遍匹配结果。

## Python API

### `run_shell` / `run_shell_list`

```python
from funshell import run_shell, run_shell_list

# 实时输出到终端，返回退出码字符串
exit_code = run_shell("echo hello world")  # "0"

# 捕获 stdout 输出（去除首尾空白）
output = run_shell("echo hello world", printf=False)  # "hello world"

# 指定工作目录与超时（秒）
run_shell("git status", cwd="/path/to/repo", timeout=5)

# 依次执行多条命令（用 && 连接，前一条失败则终止）
output = run_shell_list(["echo hello", "echo world"], printf=False)
```

| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| `command` / `command_list` | `str` / `list[str]` | 必填 | 要执行的 shell 命令 |
| `printf` | `bool` | `True` | `True` 实时输出并返回退出码字符串；`False` 捕获 stdout |
| `cwd` | `str \| PathLike \| None` | `None` | 工作目录 |
| `timeout` | `float \| None` | `None` | 超时时间（秒），超时自动终止子进程并返回 `"run shell error: command timed out"` |
| `encoding` | `str` | `"utf-8"` | 捕获输出时使用的字符编码 |

### `kill_process` / `ProcessFinder`

```python
from funshell import kill_process
from funshell.kill import ProcInfo, ProcessFinder

# 一步到位：按端口和/或进程名匹配并杀掉
kill_process(port=8080)
kill_process(name=("code-server",))
kill_process(port=3000, name=("node",), sig="TERM")

# 或先查询再决定是否终止
finder = ProcessFinder().find_by_port(8080)
for proc in finder:
    print(proc)  # pid=42 name=python port=8080 | python3 -m myserver
finder.kill(sig="TERM")
```

## License

[MIT](LICENSE)

---

## 关于 farfarfun

[farfarfun](https://github.com/farfarfun) 是一个专注于实用工具库的开源组织，
涵盖云存储、数据处理、AI、多媒体与开发工具链等方向。

- 🏠 组织主页：<https://github.com/farfarfun>
- 📦 PyPI：<https://pypi.org/user/niuliangtao/>
- 📧 联系：farfarfun@qq.com

本项目基于 [MIT](LICENSE) 协议开源。

