| |
| """ |
| collect_manuals.py - Gather all available man pages as a JSON dataset. |
| NixOS compatible; relies on `manpath` and `man`. |
| """ |
| import json |
| import os |
| import subprocess |
| import sys |
| from pathlib import Path |
|
|
| |
| |
| |
| def get_manpaths(): |
| """Return a list of directories that `man` searches for pages.""" |
| |
| |
| try: |
| result = subprocess.run(["manpath", "-q"], capture_output=True, text=True, check=True) |
| paths = result.stdout.strip().split(":") |
| if paths and paths != [""]: |
| return paths |
| except (FileNotFoundError, subprocess.CalledProcessError): |
| pass |
|
|
| |
| manpath_env = os.environ.get("MANPATH") |
| if manpath_env: |
| return manpath_env.split(":") |
|
|
| |
| return ["/usr/share/man", "/usr/local/share/man"] |
|
|
| |
| |
| |
| def find_man_files(manpaths): |
| """ |
| Walk each manpath directory, look inside 'man<section>' subdirs, |
| and collect (topic_name, section, full_path). |
| """ |
| |
| ext_list = {".gz", ".bz2", ".xz", ".Z", ".lzma"} |
| |
| entries = [] |
|
|
| for base_dir in manpaths: |
| base = Path(base_dir) |
| if not base.is_dir(): |
| continue |
| |
| for man_dir in base.glob("man*"): |
| if not man_dir.is_dir(): |
| continue |
| |
| section = man_dir.name[3:] |
| |
| for file_path in man_dir.iterdir(): |
| if not file_path.is_file(): |
| continue |
| |
| real_path = file_path.resolve() |
| |
| name = file_path.name |
| |
| while True: |
| stem, ext = os.path.splitext(name) |
| if ext.lower() in ext_list: |
| name = stem |
| else: |
| break |
| |
| |
| if name.endswith(f".{section}"): |
| name = name[: -(len(section) + 1)] |
| |
| if not name: |
| continue |
| entries.append((name, section, real_path)) |
| return entries |
|
|
| |
| |
| |
| def render_man(topic, section, timeout=5): |
| """ |
| Run `man -P cat <section> <topic>` and return its stdout as a string. |
| Returns None on failure. |
| """ |
| cmd = ["man", "-P", "cat", section, topic] |
| try: |
| result = subprocess.run( |
| cmd, |
| capture_output=True, |
| text=True, |
| timeout=timeout, |
| |
| |
| env={**os.environ, "MANPAGER": "cat", "PAGER": "cat"}, |
| ) |
| if result.returncode != 0: |
| |
| return None |
| return result.stdout |
| except Exception: |
| return None |
|
|
| |
| |
| |
| def main(): |
| |
| paths = get_manpaths() |
| print(f"Searching in {len(paths)} man directories...", file=sys.stderr) |
| entries = find_man_files(paths) |
| print(f"Found {len(entries)} manual page files.", file=sys.stderr) |
|
|
| |
| |
| seen = set() |
| unique_entries = [] |
| for name, section, filepath in entries: |
| key = (name, section) |
| if key not in seen: |
| seen.add(key) |
| unique_entries.append((name, section)) |
| print(f"After deduplication: {len(unique_entries)} unique manuals.", file=sys.stderr) |
|
|
| dataset = [] |
| |
| |
| with open("manuals.jsonl", "w", encoding="utf-8") as fout: |
| for idx, (name, section) in enumerate(unique_entries, 1): |
| text = render_man(name, section) |
| if text is None: |
| |
| continue |
| record = { |
| "topic": name, |
| "section": section, |
| "manual": text, |
| } |
| |
| fout.write(json.dumps(record, ensure_ascii=False) + "\n") |
|
|
| if idx % 100 == 0: |
| print(f"Processed {idx}/{len(unique_entries)}...", file=sys.stderr) |
|
|
| print("Done. Output written to manuals.jsonl", file=sys.stderr) |
|
|
| if __name__ == "__main__": |
| main() |