尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

别把整个仓库塞给 AI:用 Python 生成安全的代码上下文清单

别把整个仓库塞给 AI:用 Python 生成安全的代码上下文清单
📅 发布时间:2026/8/4 3:48:21

让 AI 帮忙分析老项目,最省事的做法似乎是把整个目录直接丢进去。

但项目里往往混着.env、密钥、依赖目录、构建产物和大体积文件。全部提交不仅浪费上下文,还可能把不该出现的信息一起带出去。

我更建议先生成一份“仓库上下文清单”:只列出适合分析的文件路径和大小,人工看一遍,再决定下一步让 AI 读取哪些文件。

这个脚本会做什么

脚本默认执行以下处理:

  • 忽略.git、node_modules、dist、.venv等目录;

  • 排除.env、私钥和常见凭据文件;

  • 跳过软链接,避免扫描到项目外部;

  • 只保留常见代码、配置和文档文件;

  • 跳过超过指定大小的文件;

  • 只生成文件清单,不读取文件内容;

  • 自动排除生成的报告本身。

脚本使用 Python 标准库,不需要安装第三方依赖。

完整代码

将下面代码保存为repo_context.py:

from __future__ import annotations import argparse import os from collections import Counter from pathlib import Path IGNORE_DIRS = { ".git", ".idea", ".vscode", "node_modules", "dist", "build", "coverage", "__pycache__", ".venv", "venv", } SENSITIVE_NAMES = { ".env", ".env.local", ".env.production", "id_rsa", "id_ed25519", "credentials.json", "secrets.json", } ALLOWED_SUFFIXES = { ".py", ".js", ".jsx", ".ts", ".tsx", ".java", ".go", ".rs", ".php", ".vue", ".sql", ".md", ".json", ".yaml", ".yml", ".toml", } def collect_files( root: Path, max_bytes: int, excluded: set[Path] | None = None, ) -> tuple[list[tuple[Path, int]], Counter[str]]: files: list[tuple[Path, int]] = [] skipped: Counter[str] = Counter() excluded = excluded or set() for current_dir, dir_names, file_names in os.walk( root, followlinks=False, ): dir_names[:] = sorted( name for name in dir_names if name not in IGNORE_DIRS and not name.startswith(".") ) current = Path(current_dir) for name in sorted(file_names): path = current / name if path.resolve() in excluded: skipped["output"] += 1 continue if name in SENSITIVE_NAMES or name.startswith(".env."): skipped["sensitive"] += 1 continue if path.is_symlink(): skipped["symlink"] += 1 continue if path.suffix.lower() not in ALLOWED_SUFFIXES: skipped["unsupported"] += 1 continue try: size = path.stat().st_size except OSError: skipped["unreadable"] += 1 continue if size > max_bytes: skipped["too_large"] += 1 continue files.append((path.relative_to(root), size)) return files, skipped def build_report( root: Path, files: list[tuple[Path, int]], skipped: Counter[str], ) -> str: suffix_counts = Counter( path.suffix.lower() or "[no suffix]" for path, _ in files ) lines = [ "# Repository Context", "", f"- Root: `{root.name}`", f"- Included files: {len(files)}", f"- Skipped files: {sum(skipped.values())}", "", "## File types", "", ] if suffix_counts: lines.extend( f"- `{suffix}`: {count}" for suffix, count in sorted(suffix_counts.items()) ) else: lines.append("- No matching files") lines.extend(["", "## Files", ""]) if files: lines.extend( f"- `{path.as_posix()}` ({size} bytes)" for path, size in files ) else: lines.append("- No matching files") lines.extend(["", "## Skip summary", ""]) if skipped: lines.extend( f"- `{reason}`: {count}" for reason, count in sorted(skipped.items()) ) else: lines.append("- Nothing skipped") return "\n".join(lines) + "\n" def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser( description="Generate a safe repository context manifest." ) parser.add_argument( "root", type=Path, help="Project root directory", ) parser.add_argument( "-o", "--output", type=Path, default=Path("REPO_CONTEXT.md"), ) parser.add_argument( "--max-kb", type=int, default=200, help="Maximum size per file", ) return parser.parse_args() def main() -> int: args = parse_args() root = args.root.expanduser().resolve() if not root.is_dir(): raise SystemExit( f"Project directory does not exist: {root}" ) if args.max_kb <= 0: raise SystemExit( "--max-kb must be greater than 0" ) output = args.output.expanduser().resolve() files, skipped = collect_files( root, args.max_kb * 1024, excluded={output}, ) report = build_report(root, files, skipped) output.write_text(report, encoding="utf-8") print(f"Wrote {len(files)} files to {output}") return 0 if __name__ == "__main__": raise SystemExit(main())

运行方法

macOS 或 Linux:

python repo_context.py /path/to/project \ -o REPO_CONTEXT.md \ --max-kb 200

Windows PowerShell:

python repo_context.py "D:\work\demo" ` -o REPO_CONTEXT.md ` --max-kb 200

执行完成后会得到类似下面的文件:

# Repository Context - Root: `demo` - Included files: 18 - Skipped files: 326 ## File types - `.json`: 2 - `.md`: 3 - `.py`: 13 ## Files - `README.md` (1820 bytes) - `src/main.py` (963 bytes) - `src/config.json` (218 bytes) ## Skip summary - `sensitive`: 2 - `too_large`: 3 - `unsupported`: 321

拿到这份清单后,先人工检查一次,再让 AI 按模块分析:

这是项目文件清单。请先判断项目类型、主要入口和核心模块, 暂时不要生成代码,也不要假设你已经看到文件内容。 请告诉我: 1. 第一批需要读取哪些文件; 2. 每个文件的分析目的; 3. 哪些配置文件可能包含敏感信息,不应该直接提供。

这样做比一次上传整个项目更可控。AI 不需要先看到几百个依赖文件,也不会因为目录太杂而忽略真正的入口。

还需要注意两个边界

第一,这个脚本只按文件名、扩展名和大小过滤,不是专业的密钥扫描工具。即使文件通过过滤,也要在提交前人工检查内容。

第二,脚本默认忽略所有以点开头的目录。如果项目需要分析.github/workflows,可以删除not name.startswith("."),然后单独检查工作流里是否存在密钥、令牌或部署信息。

如果你长期使用 ChatGPT、Claude、Cursor 或 Kiro,会员充值问题也可以了解 gpt68.com。它是第三方 AI 会员充值平台,使用前应看清套餐说明、账号要求和售后规则。工具是否好用是一方面,能不能把项目上下文整理清楚,往往更影响最终结果。

本文脚本基于 Python 标准库pathlib和os.walk实现。pathlib用于跨平台路径处理,可参考 Python 官方文档。

相关新闻

  • 2026年8月怀化市移动1000M单宽带我的真实踩坑经历 - 找卡家园
  • ESC/POS命令集详解:从乱码到专业小票的嵌入式打印实战
  • 从Text-to-SQL到Data Agent:企业数据智能为什么只能这样演进?

最新新闻

  • 2026免费工具保姆级教程:视频转MP4保留多音轨+字幕(TOP3小程序全攻略) - 今日咨询
  • 怎么选?2026年新型TX-XZK直线振动筛制造商综合评估与选型指南 - 优质品牌商家
  • SpringBoot全域旅游系统架构设计与高并发实践
  • 三分钟为AI助手安装“女娲.skill”,解锁结构化思维与问题解决框架
  • 乐山小语种机构培训哪家好?2026年本地市场深度分析与选择建议 - 优质品牌商家
  • 2026 年 7 月新发布:武侯正规的下水道疏通服务团队怎么联系,你家堵了3次的管道,原来这玩意儿疏通才是对的! - 企业官方推荐【认证】

日新闻

  • 5分钟快速搭建智能数字人:Live2D虚拟形象终极部署指南
  • 告别繁简字幕转换烦恼:这款开源工具让你一键搞定影视字幕处理 [特殊字符]
  • GPT-5.4传闻背后:大模型永久记忆与极限推理的技术演进与挑战

周新闻

  • 怀化母婴除甲醛公司测甲醛中心怎么选:康之居母婴除甲醛标准、流程、避坑指南 - 信誉隆金银铂奢回收
  • 三步打造你的终极音乐中心:foobox-cn网络电台功能完整指南
  • Lance湖仓格式:为多模态AI工作流设计的终极数据存储方案

月新闻

  • ClickHouse版本管理深度实战:4步构建零风险升级与回滚体系
  • Java 23 种设计模式:从踩坑到精通 | 番外:责任链模式 —— 物流审批流程实战
  • 华硕笔记本性能解放指南:G-Helper轻量级控制工具全面解析

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号