Add pinned deployment docs sources and text builder

This commit is contained in:
Daniel Samson committed 2026-11-21 13:17:32 +00:00
1 parent f67cbce688
commit bd4211fb61
2 files changed
+166

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
#!/usr/bin/env python3
"""Turn the pinned deployment docs into one text corpus for pretraining.
Reads sources/deployment-docs-sources.json and cleans every .md and .rst file under each
listed path. Markdown loses front matter, Hugo shortcodes and HTML tags. reStructuredText
loses directive lines, label targets and heading underlines, and keeps directive bodies.
Each record carries its project, source path, pinned commit and license.
"""
import argparse
import html
import json
import re
import sys
from pathlib import Path
RESERVED = ("<|pad|>", "<|im_start|>", "<|im_end|>")
SHORTCODE = re.compile(r"\{\{[<%].*?[>%]\}\}", re.S)
TAG = re.compile(r"<[^>]+>")
RST_DIRECTIVE = re.compile(r"^\s*\.\.\s+[\w:-]+::.*$")
RST_LABEL = re.compile(r"^\s*\.\.\s+_[^:]+:\s*$")
RST_COMMENT = re.compile(r"^\s*\.\.\s.*$")
RST_UNDERLINE = re.compile(r"^([=\-~^\"'`#*+_:])\1{2,}\s*$")
RST_ROLE = re.compile(r":[\w:-]+:`([^`]*)`")
def clean_markdown(text):
if text.startswith("---"):
end = text.find("\n---", 3)
if end != -1:
text = text[end + 4:]
text = SHORTCODE.sub("", text)
text = TAG.sub(" ", text)
return html.unescape(text)
def clean_rst(text):
lines = []
for line in text.splitlines():
if RST_LABEL.match(line) or (RST_COMMENT.match(line) and not RST_DIRECTIVE.match(line)):
continue
if RST_DIRECTIVE.match(line) or RST_UNDERLINE.match(line.strip()):
continue
lines.append(RST_ROLE.sub(r"\1", line))
return "\n".join(lines)
def tidy(text):
text = "\n".join(re.sub(r"[ \t]+", " ", line).rstrip() for line in text.splitlines())
return re.sub(r"\n{3,}", "\n\n", text).strip()
def records(root, source, min_words):
for relative in source["paths"]:
base = root / relative
for path in sorted(base.rglob("*")):
if path.suffix not in (".md", ".rst") or not path.is_file():
continue
raw = path.read_text(encoding="utf-8", errors="replace")
text = tidy(clean_markdown(raw) if path.suffix == ".md" else clean_rst(raw))
if len(text.split()) < min_words or any(token in text for token in RESERVED):
continue
yield {"text": text, "project": source["name"],
"source": str(path.relative_to(root)), "commit": source["commit"],
"license": source["license"]}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--raw", type=Path, default=Path("sources/raw"))
parser.add_argument("--manifest", type=Path, default=Path("sources/deployment-docs-sources.json"))
parser.add_argument("--output-file", type=Path, default=Path("corpora/deployment-docs.jsonl"))
parser.add_argument("--min-words", type=int, default=20)
args = parser.parse_args()
if args.output_file.exists():
parser.error(f"refusing to overwrite: {args.output_file}")
manifest = json.loads(args.manifest.read_text(encoding="utf-8"))
counts = {}
args.output_file.parent.mkdir(parents=True, exist_ok=True)
with args.output_file.open("x", encoding="utf-8") as out:
for source in manifest["sources"]:
root = args.raw / source["name"]
if not root.is_dir():
parser.error(f"missing clone: {root}")
written = 0
for record in records(root, source, args.min_words):
out.write(json.dumps(record, ensure_ascii=False) + "\n")
written += 1
counts[source["name"]] = written
print(json.dumps(counts, indent=2))
print(f"total records: {sum(counts.values())}")
if __name__ == "__main__":
sys.exit(main())
+72
View File
@@ -0,0 +1,72 @@
{
"sources": [
{
"name": "prometheus-docs",
"repository": "https://github.com/prometheus/docs",
"commit": "31957d4da93dd215cad8c91be3e5205181dd11f1",
"paths": [
"docs/"
],
"license": "Apache-2.0",
"license_evidence": "repository license (Apache-2.0)",
"markdown_files_fetched": 70
},
{
"name": "minio-docs",
"repository": "https://github.com/minio/docs",
"commit": "0f20cb5c667a6a5c833e6b369addb9a08f0440a7",
"paths": [
"source/"
],
"license": "CC-BY-4.0",
"license_evidence": "LICENSE file is CC-BY-4.0 text",
"markdown_files_fetched": 6,
"restructuredtext_files_fetched": 457,
"notes": "Mostly reStructuredText; needs a different cleaner from Markdown."
},
{
"name": "cert-manager-website",
"repository": "https://github.com/cert-manager/website",
"commit": "cda032e36a7de880aa455ce6bbdb24aa4c3e061f",
"paths": [
"content/docs/"
],
"license": "Apache-2.0",
"license_evidence": "repository license (Apache-2.0)",
"markdown_files_fetched": 201
},
{
"name": "longhorn-website",
"repository": "https://github.com/longhorn/website",
"commit": "34fc6c7bb8e4a161a33af83bbda32d8dc267dbe4",
"paths": [
"content/docs/1.14.0/"
],
"license": "Apache-2.0",
"license_evidence": "repository license (Apache-2.0); latest version only",
"markdown_files_fetched": 172
},
{
"name": "cloudnative-pg",
"repository": "https://github.com/cloudnative-pg/cloudnative-pg",
"commit": "e9eec0d3b49adb088f6fbcfa024d4ea288105c6e",
"paths": [
"docs/"
],
"license": "Apache-2.0",
"license_evidence": "repository license (Apache-2.0)",
"markdown_files_fetched": 83
},
{
"name": "headscale",
"repository": "https://github.com/juanfont/headscale",
"commit": "0c304b0632db524356e940fc74dc6230e790d11d",
"paths": [
"docs/"
],
"license": "BSD-3-Clause",
"license_evidence": "repository license (BSD-3-Clause)",
"markdown_files_fetched": 34
}
]
}