Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tests/test_feature_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import subprocess
import sys
import tempfile
from unittest import mock

TOOLS = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tools")
sys.path.insert(0, TOOLS)
Expand Down Expand Up @@ -510,6 +511,24 @@ def passes():
assert failures == 0, f"{failures} script-runner check(s) failed"


def test_out_write_failure_preserves_previous_report():
with tempfile.TemporaryDirectory() as tmp:
previous = '{"previous": true}'
path = os.path.join(tmp, "feature_scan.json")
with open(path, "w", encoding="utf-8", newline="\n") as f:
f.write(previous)
with mock.patch.object(F, "replace_retry", side_effect=OSError(13, "Permission denied")):
raised = False
try:
F.write_json_report(path, {"shaders": 0})
except OSError:
raised = True
check(raised, "write_json_report raises when replace fails")
with open(path, encoding="utf-8") as f:
check(f.read() == previous, "failed --out write leaves the previous report intact")
assert failures == 0, f"{failures} preserve check(s) failed"


def run_tests(tests, report=print):
"""Run each test on its own, for the plain-script run (CI's standard-library job): one that raises
AssertionError is reported and the rest still run. Each test starts from a zero check count, so its closing
Expand Down
49 changes: 44 additions & 5 deletions tools/feature_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
import json
import os
import re
import stat
import struct
import sys
import tempfile
from collections import Counter, defaultdict

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from file_mode import published_mode # noqa: E402
from replace_retry import replace as replace_retry # noqa: E402
from shader_carve import parts as dxbc_part_ranges # noqa: E402

# d3d12shader.h (Windows SDK 10.0.26100.0), D3D_SHADER_REQUIRES_*: the SFI0 part is this 64-bit mask.
Expand Down Expand Up @@ -551,6 +555,45 @@ def inside_dir(path: str, folder: str) -> bool:
return False


def write_json_report(path: str, report: dict) -> None:
"""Publish a complete regular file, or write directly to a non-regular destination.

Parent folders are created when missing (same as the previous direct writer).
"""
folder = os.path.dirname(path)
if folder:
os.makedirs(folder, exist_ok=True)
destination = os.path.realpath(path)
parent = os.path.dirname(destination) or "."
try:
mode = os.stat(destination).st_mode
except FileNotFoundError:
mode = None
if mode is not None and not stat.S_ISREG(mode):
with open(path, "w", encoding="utf-8", newline="\n") as f:
json.dump(report, f, indent=1)
return
if mode is not None:
# Retain the direct writer's permission check without truncating the report.
os.close(os.open(destination, os.O_WRONLY))

staged = tempfile.NamedTemporaryFile(
mode="w", encoding="utf-8", newline="\n", delete=False,
dir=parent, prefix=".feature_scan-", suffix=".tmp",
)
try:
with staged:
json.dump(report, staged, indent=1)
if os.name != "nt":
os.chmod(staged.name, published_mode(destination))
replace_retry(staged.name, destination)
finally:
try:
os.unlink(staged.name)
except FileNotFoundError:
pass


def main() -> int:
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
ap.add_argument("shaders", help="<work>/shaders from make_game.py")
Expand Down Expand Up @@ -646,12 +689,8 @@ def main() -> int:
if d["unreadable"]:
print(f"{d['unreadable']} .dxbc files could not be read")
if a.out:
folder = os.path.dirname(a.out) # --out into a folder that does not exist yet, as awc_manifest.py does
try:
if folder:
os.makedirs(folder, exist_ok=True)
with open(a.out, "w", encoding="utf-8", newline="\n") as f:
json.dump(result, f, indent=1)
write_json_report(a.out, result)
except OSError as e:
print(f"ERROR: cannot write {a.out}: {e.strerror or e}", file=sys.stderr)
return 2
Expand Down