fix: add tomlkit to Dockerfile for comment-preserving TOML editing (#886)
This commit is contained in:
parent
8943af4484
commit
cf99bdc51e
2 changed files with 10 additions and 11 deletions
|
|
@ -2,7 +2,7 @@ FROM debian:bookworm-slim
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
bash curl git jq tmux python3 python3-pip openssh-client ca-certificates age shellcheck procps gosu \
|
bash curl git jq tmux python3 python3-pip openssh-client ca-certificates age shellcheck procps gosu \
|
||||||
&& pip3 install --break-system-packages networkx \
|
&& pip3 install --break-system-packages networkx tomlkit \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Pre-built binaries (copied from docker/agents/bin/)
|
# Pre-built binaries (copied from docker/agents/bin/)
|
||||||
|
|
|
||||||
|
|
@ -536,8 +536,7 @@ EOF
|
||||||
echo " Writing [agents.${section_name}] to ${toml_file}..."
|
echo " Writing [agents.${section_name}] to ${toml_file}..."
|
||||||
python3 -c '
|
python3 -c '
|
||||||
import sys
|
import sys
|
||||||
import tomllib
|
import tomlkit
|
||||||
import tomli_w
|
|
||||||
import re
|
import re
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
|
|
@ -558,19 +557,19 @@ text = p.read_text()
|
||||||
commented_pattern = rf"(?:^|\n)# \[agents\.{re.escape(section_name)}\](?:\n(?!# \[|\[)[^\n]*)*"
|
commented_pattern = rf"(?:^|\n)# \[agents\.{re.escape(section_name)}\](?:\n(?!# \[|\[)[^\n]*)*"
|
||||||
text = re.sub(commented_pattern, "", text, flags=re.DOTALL)
|
text = re.sub(commented_pattern, "", text, flags=re.DOTALL)
|
||||||
|
|
||||||
# Step 2: Parse TOML with tomllib
|
# Step 2: Parse TOML with tomlkit (preserves comments and formatting)
|
||||||
try:
|
try:
|
||||||
data = tomllib.loads(text)
|
doc = tomlkit.parse(text)
|
||||||
except tomllib.TOMLDecodeError as e:
|
except Exception as e:
|
||||||
print(f"Error: Invalid TOML in {toml_path}: {e}", file=sys.stderr)
|
print(f"Error: Invalid TOML in {toml_path}: {e}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Step 3: Ensure agents table exists
|
# Step 3: Ensure agents table exists
|
||||||
if "agents" not in data:
|
if "agents" not in doc:
|
||||||
data["agents"] = {}
|
doc.add("agents", tomlkit.table())
|
||||||
|
|
||||||
# Step 4: Update the specific agent section
|
# Step 4: Update the specific agent section
|
||||||
data["agents"][section_name] = {
|
doc["agents"][section_name] = {
|
||||||
"base_url": base_url,
|
"base_url": base_url,
|
||||||
"model": model,
|
"model": model,
|
||||||
"api_key": "sk-no-key-required",
|
"api_key": "sk-no-key-required",
|
||||||
|
|
@ -580,8 +579,8 @@ data["agents"][section_name] = {
|
||||||
"poll_interval": int(poll_interval),
|
"poll_interval": int(poll_interval),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Step 5: Serialize back to TOML
|
# Step 5: Serialize back to TOML (preserves comments)
|
||||||
output = tomli_w.dumps(data)
|
output = tomlkit.dumps(doc)
|
||||||
|
|
||||||
# Step 6: Write back
|
# Step 6: Write back
|
||||||
p.write_text(output)
|
p.write_text(output)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue