|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Ensure a container-friendly Codex config exists.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import os |
| 7 | +import re |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +def _matches_assignment(line: str, key: str) -> bool: |
| 13 | + return re.match(rf"^\s*{re.escape(key)}\s*=", line) is not None |
| 14 | + |
| 15 | + |
| 16 | +def _matches_table(line: str, table_name: str) -> bool: |
| 17 | + return re.match(rf"^\s*\[{re.escape(table_name)}\]\s*(?:#.*)?$", line) is not None |
| 18 | + |
| 19 | + |
| 20 | +def _is_table_header(line: str) -> bool: |
| 21 | + return re.match(r"^\s*\[", line) is not None |
| 22 | + |
| 23 | + |
| 24 | +def _ensure_trailing_newline(lines: list[str], index: int) -> None: |
| 25 | + if 0 <= index < len(lines) and not lines[index].endswith("\n"): |
| 26 | + lines[index] += "\n" |
| 27 | + |
| 28 | + |
| 29 | +def resolve_config_path(argv: list[str]) -> Path: |
| 30 | + if len(argv) > 2: |
| 31 | + raise SystemExit("usage: bootstrap_codex_config.py [config-path]") |
| 32 | + if len(argv) == 2: |
| 33 | + return Path(argv[1]).expanduser() |
| 34 | + |
| 35 | + codex_home = Path(os.environ.get("CODEX_HOME", "~/.codex")).expanduser() |
| 36 | + return codex_home / "config.toml" |
| 37 | + |
| 38 | + |
| 39 | +def ensure_top_level_setting(lines: list[str], key: str, value: str) -> bool: |
| 40 | + rendered = f'{key} = "{value}"\n' |
| 41 | + for index, line in enumerate(lines): |
| 42 | + if _is_table_header(line): |
| 43 | + break |
| 44 | + if _matches_assignment(line, key): |
| 45 | + if line.strip() == rendered.strip(): |
| 46 | + return False |
| 47 | + lines[index] = rendered |
| 48 | + return True |
| 49 | + |
| 50 | + insert_at = 0 |
| 51 | + for index, line in enumerate(lines): |
| 52 | + if _is_table_header(line): |
| 53 | + insert_at = index |
| 54 | + break |
| 55 | + else: |
| 56 | + insert_at = len(lines) |
| 57 | + |
| 58 | + if insert_at == len(lines): |
| 59 | + _ensure_trailing_newline(lines, insert_at - 1) |
| 60 | + lines.insert(insert_at, rendered) |
| 61 | + return True |
| 62 | + |
| 63 | + |
| 64 | +def find_table(lines: list[str], table_name: str) -> tuple[int | None, int | None]: |
| 65 | + start = None |
| 66 | + end = None |
| 67 | + |
| 68 | + for index, line in enumerate(lines): |
| 69 | + if _matches_table(line, table_name): |
| 70 | + start = index |
| 71 | + continue |
| 72 | + if start is not None and _is_table_header(line): |
| 73 | + end = index |
| 74 | + break |
| 75 | + |
| 76 | + if start is not None and end is None: |
| 77 | + end = len(lines) |
| 78 | + |
| 79 | + return start, end |
| 80 | + |
| 81 | + |
| 82 | +def ensure_feature_setting(lines: list[str], key: str, value: str) -> bool: |
| 83 | + start, end = find_table(lines, "features") |
| 84 | + rendered = f"{key} = {value}\n" |
| 85 | + |
| 86 | + if start is None: |
| 87 | + if lines and lines[-1].strip(): |
| 88 | + lines.append("\n") |
| 89 | + lines.extend(["[features]\n", rendered]) |
| 90 | + return True |
| 91 | + |
| 92 | + assert end is not None |
| 93 | + for index in range(start + 1, end): |
| 94 | + if _matches_assignment(lines[index], key): |
| 95 | + if lines[index].strip() == rendered.strip(): |
| 96 | + return False |
| 97 | + lines[index] = rendered |
| 98 | + return True |
| 99 | + |
| 100 | + _ensure_trailing_newline(lines, end - 1) |
| 101 | + lines.insert(end, rendered) |
| 102 | + return True |
| 103 | + |
| 104 | + |
| 105 | +def main(argv: list[str]) -> int: |
| 106 | + config_path = resolve_config_path(argv) |
| 107 | + config_path.parent.mkdir(parents=True, exist_ok=True) |
| 108 | + |
| 109 | + if config_path.exists(): |
| 110 | + lines = config_path.read_text(encoding="utf-8").splitlines(keepends=True) |
| 111 | + else: |
| 112 | + lines = [] |
| 113 | + |
| 114 | + changed = False |
| 115 | + changed |= ensure_top_level_setting(lines, "cli_auth_credentials_store", "file") |
| 116 | + changed |= ensure_feature_setting(lines, "codex_hooks", "true") |
| 117 | + |
| 118 | + if changed: |
| 119 | + content = "".join(lines) |
| 120 | + if content and not content.endswith("\n"): |
| 121 | + content += "\n" |
| 122 | + config_path.write_text(content, encoding="utf-8") |
| 123 | + |
| 124 | + return 0 |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + raise SystemExit(main(sys.argv)) |
0 commit comments