textures: extract main(argv), atomic output writes, and error guards in bc7_blocks with CLI suite #2380

Merged
Ammmae0x merged 2 commits from upstream-pr/2388 into main 2026-11-25 15:04:28 +00:00
Ammmae0x commented 2026-11-25 14:51:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T14:51:20Z.


Change

Harden tools/bc7_blocks.py for programmatic and CLI execution while keeping its BC7C binary layout and --fields JSON output unchanged:

  • Extract the inline __main__ block into main(argv=None) -> int forwarding argv to parser.parse_args(argv).
  • Validate positive --columns and --rows (> 0) upfront before creating directories or generating blocks.
  • Reject --fields colliding with case via _same_path() (Path.resolve() equality and Path.samefile() for hardlinks/aliases) and reject existing directories for either output path.
  • Create parent directories and stage outputs inside per-parent tempfile.TemporaryDirectory instances via _atomic_write_outputs() before committing with os.replace() before making one atomic replacement per file. A later replacement can fail after the first succeeds. The error tells the caller to retry both destinations, and temporary files are cleaned up.
  • Catch (OSError, ValueError, struct.error) cleanly via parser.exit(1, ...) without raw Python tracebacks.
  • Add tests/bc7_blocks_cli_check.py and register bc7_blocks_cli_check in CMakeLists.txt.

Verification

Verified on commit 6c10634c (rebased on upstream/main ee7a6631):

  • out/pe-env/Scripts/python.exe -B tests/bc7_blocks_cli_check.py -v -> Ran 7 tests in 0.511s - OK (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/bc7_blocks_check.py -v -> Ran 10 tests in 0.196s - OK (skipped=2) (8 passed; 2 optional skips: test_bcpack_mode6_strict_reference optional reference decoder, test_independent_decoder optional texture2ddecoder; exit 0)
  • out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py -> form coverage checks passed (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/docs_links_check.py -> Self-test passed. Checked 19 Markdown files and 463 links. (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py -> Self-test passed. Checked 1709 tracked source, script and build/CI files: no bidirectional controls. (exit 0)

Spot mutation checks against tools/bc7_blocks.py (8/8 killed by tests/bc7_blocks_cli_check.py, restored cleanly):

  • Drop argv forwarding, bypass upfront positive dimension check, bypass existing-directory check, bypass resolve() collision check, bypass samefile() collision check, bypass _atomic_write_outputs() staging + os.replace(), and individually drop struct.error and OSError handlers (all returned exit 1).

Evidence and follow-up

  • All fixtures are deterministically generated in memory by tools/bc7_blocks.fixture() (MIT, repo-authored; no game files, dumps, or binaries).
  • Negative controls cover non-positive --columns/--rows, path collisions via .. alias and hardlink, existing directories as case or --fields, struct.error/ValueError handling, and os.replace() / staging failures leaving zero partial files or temp directories.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6607226188 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2388). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T14:51:20Z. --- ## Change Harden `tools/bc7_blocks.py` for programmatic and CLI execution while keeping its `BC7C` binary layout and `--fields` JSON output unchanged: - Extract the inline `__main__` block into `main(argv=None) -> int` forwarding `argv` to `parser.parse_args(argv)`. - Validate positive `--columns` and `--rows` (`> 0`) upfront before creating directories or generating blocks. - Reject `--fields` colliding with `case` via `_same_path()` (`Path.resolve()` equality and `Path.samefile()` for hardlinks/aliases) and reject existing directories for either output path. - Create parent directories and stage outputs inside per-parent `tempfile.TemporaryDirectory` instances via `_atomic_write_outputs()` before committing with `os.replace()` before making one atomic replacement per file. A later replacement can fail after the first succeeds. The error tells the caller to retry both destinations, and temporary files are cleaned up. - Catch `(OSError, ValueError, struct.error)` cleanly via `parser.exit(1, ...)` without raw Python tracebacks. - Add `tests/bc7_blocks_cli_check.py` and register `bc7_blocks_cli_check` in `CMakeLists.txt`. ## Verification Verified on commit `6c10634c` (rebased on `upstream/main` `ee7a6631`): - `out/pe-env/Scripts/python.exe -B tests/bc7_blocks_cli_check.py -v` -> `Ran 7 tests in 0.511s - OK` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/bc7_blocks_check.py -v` -> `Ran 10 tests in 0.196s - OK (skipped=2)` (8 passed; 2 optional skips: `test_bcpack_mode6_strict_reference` optional reference decoder, `test_independent_decoder` optional `texture2ddecoder`; exit 0) - `out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py` -> `form coverage checks passed` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` -> `Self-test passed. Checked 19 Markdown files and 463 links.` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` -> `Self-test passed. Checked 1709 tracked source, script and build/CI files: no bidirectional controls.` (exit 0) Spot mutation checks against `tools/bc7_blocks.py` (8/8 killed by `tests/bc7_blocks_cli_check.py`, restored cleanly): - Drop `argv` forwarding, bypass upfront positive dimension check, bypass existing-directory check, bypass `resolve()` collision check, bypass `samefile()` collision check, bypass `_atomic_write_outputs()` staging + `os.replace()`, and individually drop `struct.error` and `OSError` handlers (all returned exit 1). ## Evidence and follow-up - All fixtures are deterministically generated in memory by `tools/bc7_blocks.fixture()` (MIT, repo-authored; no game files, dumps, or binaries). - Negative controls cover non-positive `--columns`/`--rows`, path collisions via `..` alias and hardlink, existing directories as `case` or `--fields`, `struct.error`/`ValueError` handling, and `os.replace()` / staging failures leaving zero partial files or temp directories. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
R0Y00 commented 2026-11-25 15:01:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T14:55:37Z.

Review: CHANGES_REQUESTED


I reproduced two failure states on Windows at 6c10634c. With existing outputs and the second os.replace denied, the CLI exits 1 with a new BC7C file and the old JSON. The current injected failure only reaches the first replacement. The two destinations cannot be published together with sequential renames. Please make the per-file replacement and retry behavior explicit and add a second-replacement control with existing files. Also reject an ancestor/descendant pair before staging. With case.bc7c --fields case.bc7c/fields.json and no prior outputs, the current code exits 1 after creating case.bc7c as a directory. All seven existing CLI tests passed here.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6059964075 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2388#pullrequestreview-6059964075). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T14:55:37Z. **Review:** CHANGES_REQUESTED --- I reproduced two failure states on Windows at `6c10634c`. With existing outputs and the second `os.replace` denied, the CLI exits 1 with a new BC7C file and the old JSON. The current injected failure only reaches the first replacement. The two destinations cannot be published together with sequential renames. Please make the per-file replacement and retry behavior explicit and add a second-replacement control with existing files. Also reject an ancestor/descendant pair before staging. With `case.bc7c --fields case.bc7c/fields.json` and no prior outputs, the current code exits 1 after creating `case.bc7c` as a directory. All seven existing CLI tests passed here.
R0Y00 commented 2026-11-25 15:01:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T14:58:40Z.

Review: APPROVED


The follow-up rejects nested output destinations before staging and clearly reports when the first atomic replacement succeeded and the second failed. Retrying with both paths regenerates the pair. Windows: 9/9 CLI and 8/8 decoder checks passed. Two optional decoder comparisons skipped. The independent second-replacement test preserves the old JSON until a successful retry.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6060014053 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2388#pullrequestreview-6060014053). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T14:58:40Z. **Review:** APPROVED --- The follow-up rejects nested output destinations before staging and clearly reports when the first atomic replacement succeeded and the second failed. Retrying with both paths regenerates the pair. Windows: 9/9 CLI and 8/8 decoder checks passed. Two optional decoder comparisons skipped. The independent second-replacement test preserves the old JSON until a successful retry.
upstream-sync closed this pull request 2026-11-25 16:05:11 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2380
No description provided.