codegen-sh/graph-sitter

GitHub: codegen-sh/graph-sitter

Graph-sitter 是一个基于图结构的代码库操作 Python 库,通过构建代码元素关系图实现大规模、多语言的可脚本化代码转换与静态分析。

Stars: 40 | Forks: 12


强大的多语言语言服务器的可脚本化接口。

[![PyPI](https://img.shields.io/badge/PyPi-graph--sitter-gray?style=flat-square&color=blue)](https://pypi.org/project/graph-sitter/) [![文档](https://img.shields.io/badge/Docs-graph-sitter.com-purple?style=flat-square)](https://graph-sitter.com) [![Slack 社区](https://img.shields.io/badge/Slack-Join-4A154B?logo=slack&style=flat-square)](https://community.codegen.com) [![许可证](https://img.shields.io/badge/Code%20License-Apache%202.0-gray?&color=gray)](https://github.com/codegen-sh/graph-sitter/tree/develop?tab=Apache-2.0-1-ov-file) [![在 X 上关注](https://img.shields.io/twitter/follow/codegen?style=social)](https://x.com/codegen)

[Graph-sitter](https://graph-sitter.com) 是一个用于操作代码库的 Python 库。 ``` from graph_sitter import Codebase # Graph-sitter 构建一个完整的图,连接 # 函数、类、导入及其关系 codebase = Codebase("./") # 处理代码而无需处理 syntax trees 或 parsing for function in codebase.functions: # Comprehensive static analysis for references, dependencies, etc. if not function.usages: # Auto-handles references and imports to maintain correctness function.move_to_file("deprecated.py") ``` 编写能够转换代码的代码。Graph-sitter 结合了 [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) 的解析能力与 [rustworkx](https://github.com/Qiskit/rustworkx) 的图算法,实现可大规模脚本化的多语言代码操作。 ## 安装与使用 我们支持 - 在 Python 3.12 - 3.13 中运行 Graph-sitter(推荐:Python 3.13+) - macOS 和 Linux - 支持 macOS - Linux 支持 x86_64 和带有 glibc 2.34+ 的 aarch64 架构 - Windows 通过 WSL 提供支持。详情请参阅[此处](https://graph-sitter.com/building-with-graph-sitter/codegen-with-wsl)。 - Python、Typescript、Javascript 和 React 代码库 ``` # 在现有项目中安装 uv pip install graph-sitter # 安装全局 CLI uv tool install graph-sitter --python 3.13 # 为给定的 repo 创建一个 codemod cd path/to/repo gs init gs create test-function # 运行 codemod gs run test-function # 创建一个独立的 venv 并使用 graph-sitter => 打开 jupyter gs notebook ``` ## 使用方法 完整教程请参阅[入门指南](https://graph-sitter.com/introduction/getting-started)。 ``` from graph_sitter import Codebase ``` ## 故障排除 遇到问题了吗?以下是一些常见问题及其解决方案: - **我遇到了与 `[[ packages ]]` 相关的 UV 错误**:这意味着您可能在使用过时版本的 UV。尝试使用以下命令更新到最新版本:`uv self update`。 - **我遇到了关于 `No module named 'graph_sitter.sdk.extensions.utils'` 的错误**:编译的 cython 扩展不同步。使用 `uv sync --reinstall-package graph-sitter` 更新它们。 - **在解析我的代码库时遇到了 `RecursionError: maximum recursion depth exceeded` 错误**:如果您使用的是 python 3.12,请尝试升级到 3.13。如果您已经在使用 3.13,请尝试使用 `sys.setrecursionlimit(10000)` 提高递归限制。 如果您遇到此处未列出的其他问题,请[加入我们的 slack 社区](https://community.codegen.com),我们将为您提供帮助! ## 资源 - [文档](https://graph-sitter.com) - [入门指南](https://graph-sitter.com/introduction/getting-started) - [贡献指南](CONTRIBUTING.md) - [联系我们](https://codegen.com/contact) ## 为什么选择 Graph-sitter? 软件开发从根本上是程序化的。重构代码库、强制执行模式或分析控制流——这些都是可以(也应该)作为程序本身来表达的操作。 我们根据在企业代码库上执行的真实重构,以逆向工程的方式构建了 Graph-sitter。我们没有从理论抽象开始,而是专注于创建与开发人员实际思考代码更改方式相匹配的 API: - **自然的心智模型**:编写读起来像您的思考过程的转换——“移动此函数”、“重命名此变量”、“添加此参数”。不再需要与 AST 搏斗或进行手动导入管理。 - **在复杂的代码库中久经考验**:能够处理拥有数百万行代码的 Python、TypeScript 和 React 代码库。 - **为高级智能体构建**:随着 AI 开发人员变得越来越复杂,他们需要富有表现力且精确的工具来操作代码。Graph-sitter 提供了一个人类和 AI 都可以使用的编程接口,以通过代码本身表达复杂的转换。 ## 贡献 有关如何设置开发环境并提交贡献的说明,请参阅我们的[贡献指南](CONTRIBUTING.md)。 ## 企业版 有关企业级合作的更多信息,请[联系我们](https://codegen.com/contact)或[请求演示](https://codegen.com/request-demo)。
标签:AST解析, IP 地址批量处理, LSP, odt, SOC Prime, Tree-sitter, WebSocket, 代码图谱, 代码库操作, 代码生成, 代码自动化, 代码重构, 依赖分析, 图算法, 多语言支持, 威胁情报, 安全测试框架, 开发工具, 开发者工具, 抽象语法树, 数据管道, 渗透测试工具, 自动重构, 软件工程, 逆向工具, 错误基检测, 静态代码分析