-
Notifications
You must be signed in to change notification settings - Fork 227
chore: harden path handling in build-digest.py #1173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e49e629
523c55b
45982ba
4b05491
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,6 @@ | |
| """ | ||
|
|
||
| import sys | ||
| import os | ||
| import re | ||
| from datetime import datetime, timezone | ||
| from pathlib import Path | ||
|
|
@@ -102,14 +101,25 @@ def heading_depth(relpath: Path, is_index: bool) -> int: | |
|
|
||
|
|
||
| def main(): | ||
| """Parse arguments, collect markdown files, and write the digest.""" | ||
| if len(sys.argv) != 4: | ||
| print(f"Usage: {sys.argv[0]} <content_dir> <output_file> <title>") | ||
| sys.exit(1) | ||
|
|
||
| content_dir = Path(sys.argv[1]) | ||
| output_file = Path(sys.argv[2]) | ||
| content_dir_display = sys.argv[1] | ||
| output_file_display = sys.argv[2] | ||
| try: | ||
| content_dir = Path(content_dir_display).resolve() | ||
| output_file = Path(output_file_display).resolve() | ||
| except (OSError, RuntimeError) as e: | ||
| print(f"Error: invalid path: {e}", file=sys.stderr) | ||
| sys.exit(1) | ||
| doc_title = sys.argv[3] | ||
|
|
||
| if not content_dir.is_dir(): | ||
| print(f"Error: content directory '{content_dir_display}' does not exist or is not a directory.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| # Collect all markdown files, excluding releases and helm-chart-values | ||
| md_files = [] | ||
| for f in sorted(content_dir.rglob("*.md")): | ||
|
|
@@ -122,13 +132,16 @@ def main(): | |
| # Exclude helm chart values | ||
| if f.name == 'helm-chart-values.md': | ||
| continue | ||
| # Exclude the output file itself when placed inside content_dir | ||
| if f.resolve() == output_file: | ||
| continue | ||
|
|
||
| md_files.append(f) | ||
|
|
||
| now = datetime.now(timezone.utc).strftime("%Y-%m-%d") | ||
| out_lines = [ | ||
| f"# {doc_title}\n", | ||
| f"> Auto-generated documentation digest. Source: `{content_dir}` ", | ||
| f"> Auto-generated documentation digest. Source: `{content_dir_display}` ", | ||
| f"> Generated: {now}\n", | ||
| "---\n", | ||
| ] | ||
|
|
@@ -155,9 +168,14 @@ def main(): | |
| out_lines.append("\n\n---\n") | ||
| file_count += 1 | ||
|
|
||
| try: | ||
| output_file.parent.mkdir(parents=True, exist_ok=True) | ||
| except OSError as e: | ||
| print(f"Error: cannot create output directory: {e}", file=sys.stderr) | ||
| sys.exit(1) | ||
| output_file.write_text('\n'.join(out_lines), encoding='utf-8') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Handle output-file write failures.
🤖 Prompt for AI Agents |
||
| total_lines = sum(1 for _ in output_file.read_text().split('\n')) | ||
| print(f"Done: {total_lines} lines, {file_count} documents written to {output_file}") | ||
| print(f"Done: {total_lines} lines, {file_count} documents written to {output_file_display}") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will throw a raw
FileNotFoundErrorif the parent directory ofoutput_filedoesn't exist —Path.write_text()doesn't create missing parent dirs. Might be worth adding a guard foroutput_file.parentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed. Added
output_file.parent.mkdir(parents=True, exist_ok=True)before thewrite_text()call so the parent directory is created automatically if it doesn't exist.