mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:28:37 +00:00
rpf_probe: refuse directory --out before scanning and preserve symlinked report targets
This commit is contained in:
@ -307,6 +307,35 @@ runpy.run_path(sys.argv.pop(1), run_name="__main__")
|
||||
self.assertIn(blocked, err.getvalue())
|
||||
self.assertEqual(self.rows(), before)
|
||||
|
||||
def test_directory_out_is_refused_before_scanning_archives(self):
|
||||
self.write("a.rpf", archive_bytes())
|
||||
out_dir = os.path.join(self.tmp, "out_is_a_dir")
|
||||
os.mkdir(out_dir)
|
||||
err = io.StringIO()
|
||||
argv = ["rpf_probe.py", self.game, "--out", out_dir]
|
||||
with (mock.patch("sys.argv", argv), mock.patch.object(probe, "probe") as probe_fn,
|
||||
contextlib.redirect_stderr(err)):
|
||||
self.assertEqual(probe.main(), 2)
|
||||
probe_fn.assert_not_called()
|
||||
self.assertIn("directory", err.getvalue().lower())
|
||||
self.assertEqual(os.listdir(out_dir), [])
|
||||
|
||||
def test_symlinked_out_updates_target_in_place(self):
|
||||
self.write("a.rpf", archive_bytes())
|
||||
target = os.path.join(self.tmp, "real_report.json")
|
||||
with open(target, "w", encoding="utf-8") as f:
|
||||
f.write("[]")
|
||||
os.makedirs(os.path.dirname(self.out), exist_ok=True)
|
||||
try:
|
||||
os.symlink(target, self.out)
|
||||
except (OSError, NotImplementedError) as e:
|
||||
self.skipTest(f"symlinks unavailable: {e}")
|
||||
self.run_main()
|
||||
self.assertTrue(os.path.islink(self.out))
|
||||
with open(target, encoding="utf-8") as f:
|
||||
rows = json.load(f)
|
||||
self.assertEqual(len(rows), 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -52,6 +52,9 @@ def main() -> int:
|
||||
ap.add_argument("game_dir")
|
||||
ap.add_argument("--out", default=os.path.join(os.path.dirname(__file__), "..", "reports", "rpf_probe.json"))
|
||||
a = ap.parse_args()
|
||||
if os.path.isdir(a.out):
|
||||
print(f"ERROR: {a.out}: is a directory, not a file", file=sys.stderr)
|
||||
return 2
|
||||
rows = []
|
||||
|
||||
def scan_failed(error: OSError) -> None:
|
||||
@ -76,14 +79,15 @@ def main() -> int:
|
||||
return 2
|
||||
rows.sort(key=lambda r: (-r["size"], r["file"]))
|
||||
try:
|
||||
out_dir = os.path.dirname(os.path.abspath(a.out))
|
||||
dest = os.path.realpath(a.out)
|
||||
out_dir = os.path.dirname(dest) or "."
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
with tempfile.TemporaryDirectory(dir=out_dir) as staging:
|
||||
staged = os.path.join(staging, "rpf_probe.json")
|
||||
with open(staged, "w", encoding="utf-8", newline="\n") as f:
|
||||
json.dump(rows, f, indent=1)
|
||||
os.chmod(staged, published_mode(a.out))
|
||||
os.replace(staged, a.out)
|
||||
os.chmod(staged, published_mode(dest))
|
||||
os.replace(staged, dest)
|
||||
except OSError as error:
|
||||
reason = error.strerror or str(error)
|
||||
print(f"ERROR: {a.out}: {reason}", file=sys.stderr)
|
||||
|
||||
Reference in New Issue
Block a user