-
Notifications
You must be signed in to change notification settings - Fork 2
style: apply ruff format to source and test files #45
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
Open
Coding-Dev-Tools
wants to merge
8
commits into
main
Choose a base branch
from
cowork/fix-ruff-format-20260810
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0537412
style: apply ruff format to source and test files
Coding-Dev-Tools c879727
fix(ci): SHA-pin checkout in auto-pr workflow + add empty-term guard …
Coding-Dev-Tools 2ed1ff5
feat(cli): add command to apply baseline values to drifted target co…
Coding-Dev-Tools f25e946
cowork-bot: atomic file writes for fix command (prevent config corrup…
Coding-Dev-Tools 74fcdf6
fix: address 6 review issues in cli.py and _atomic.py
Coding-Dev-Tools a4925f2
fix: preserve collection values, literal dotted keys, failure exit, .…
Coding-Dev-Tools 7e86bd6
fix: resolve symlinks before atomic write, validate dry-run format su…
Coding-Dev-Tools 454b25e
fix: recognize .env filename, preserve null values and empty mappings
Coding-Dev-Tools File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """Atomic file-write helpers. | ||
|
|
||
| Write to a temporary file in the same directory, fsync, then os.replace() | ||
| to atomically swap. If the process crashes mid-write the original file | ||
| is preserved intact. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import contextlib | ||
| import os | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def atomic_write_text(path: Path, text: str, encoding: str = "utf-8") -> None: | ||
| """Atomically write *text* to *path*. | ||
|
|
||
| Creates a temporary file beside *path*, writes + fsyncs, then | ||
| ``os.replace()`` for an atomic rename. Preserves the original | ||
| file's permissions when it already exists. When *path* is a | ||
| symlink, resolves it first so the referent is updated rather than | ||
| the link being replaced by a regular file. | ||
| """ | ||
| # Resolve symlinks so we update the target file, not replace the link. | ||
| resolved = path.resolve() if path.is_symlink() else path | ||
| parent = resolved.parent | ||
| parent.mkdir(parents=True, exist_ok=True) | ||
| fd, tmp = tempfile.mkstemp(dir=parent, suffix=".tmp") | ||
| try: | ||
| with os.fdopen(fd, "w", encoding=encoding, newline="") as fh: | ||
| fh.write(text) | ||
| fh.flush() | ||
| os.fsync(fh.fileno()) | ||
| # Preserve target permissions during atomic replacement | ||
| if resolved.exists(): | ||
| try: | ||
| st = resolved.stat() | ||
| os.chmod(tmp, st.st_mode) | ||
| except OSError: | ||
| pass | ||
| os.replace(tmp, resolved) | ||
| except BaseException: | ||
| # Clean up temp file on any failure | ||
| with contextlib.suppress(OSError): | ||
| os.unlink(tmp) | ||
| raise | ||
|
|
||
|
|
||
| def atomic_write_bytes(path: Path, data: bytes) -> None: | ||
| """Atomically write *data* to *path*. | ||
|
|
||
| Preserves the original file's permissions when it already exists. | ||
| When *path* is a symlink, resolves it first so the referent is | ||
| updated rather than the link being replaced by a regular file. | ||
| """ | ||
| resolved = path.resolve() if path.is_symlink() else path | ||
| parent = resolved.parent | ||
| parent.mkdir(parents=True, exist_ok=True) | ||
| fd, tmp = tempfile.mkstemp(dir=parent, suffix=".tmp") | ||
| try: | ||
| with os.fdopen(fd, "wb") as fh: | ||
| fh.write(data) | ||
| fh.flush() | ||
| os.fsync(fh.fileno()) | ||
| # Preserve target permissions during atomic replacement | ||
| if resolved.exists(): | ||
| try: | ||
| st = resolved.stat() | ||
| os.chmod(tmp, st.st_mode) | ||
| except OSError: | ||
| pass | ||
| os.replace(tmp, resolved) | ||
| except BaseException: | ||
| with contextlib.suppress(OSError): | ||
| os.unlink(tmp) | ||
| raise | ||
|
|
||
|
|
||
| def atomic_dump_yaml(path: Path, data: object, **dump_kwargs: object) -> None: | ||
| """Serialize *data* via ``yaml.dump`` into a temp file, then atomically rename.""" | ||
| import io | ||
| import yaml | ||
|
|
||
| buf = io.StringIO() | ||
| yaml.dump(data, buf, **dump_kwargs) # type: ignore[arg-type] | ||
| atomic_write_text(path, buf.getvalue()) | ||
|
|
||
|
|
||
| def atomic_dump_toml(path: Path, data: object) -> None: | ||
| """Serialize *data* via ``tomli_w.dump`` into a temp file, then atomically rename.""" | ||
| import io | ||
| import tomli_w | ||
|
|
||
| buf = io.BytesIO() | ||
| tomli_w.dump(data, buf) # type: ignore[arg-type] | ||
| atomic_write_bytes(path, buf.getvalue()) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.