diff --git a/tests/test_anim_glb.py b/tests/test_anim_glb.py index 3995867379..dde629c006 100644 --- a/tests/test_anim_glb.py +++ b/tests/test_anim_glb.py @@ -673,6 +673,15 @@ def test_outputs_over_an_input_are_refused(self): "--part", hex(self.offsets[1]), "--report", self.packs) self.assertEqual(r.returncode, 2) + def test_same_nonexistent_glb_and_report_paths_are_refused(self): + out = os.path.join(self.tmp.name, "not-created", "same.glb") + r = self.run_tool(self.packs, out, "--skeleton", hex(self.offsets[0]), "--part", hex(self.offsets[1]), + "--report", out) + self.assertEqual(r.returncode, 2, r.stderr) + self.assertIn("the GLB and the report are the same path", r.stderr) + self.assertFalse(os.path.exists(out)) + self.assertFalse(os.path.exists(os.path.dirname(out))) + def test_report_directory_is_refused_before_writing_a_glb(self): out = os.path.join(self.tmp.name, "out.glb") report = os.path.join(self.tmp.name, "report") diff --git a/tools/anim_clips.py b/tools/anim_clips.py index 14783e0cb1..63531c547f 100644 --- a/tools/anim_clips.py +++ b/tools/anim_clips.py @@ -42,6 +42,7 @@ import anim_index # noqa: E402 from pool_workers import process_workers # noqa: E402 +from report_paths import same_report_path # noqa: E402 SYSTEM = 0x50000000 CHANNEL_NAMES = ("static_quat", "static_vec3", "static_float", "raw_float", "quantize_float", @@ -1519,16 +1520,6 @@ def decode_one(path, offset, frames): return d -def same_output_path(first, second): - """True when writing ``second`` would truncate ``first`` (same file, path or hard link).""" - if os.path.normcase(os.path.realpath(first)) == os.path.normcase(os.path.realpath(second)): - return True - try: - return os.path.samefile(first, second) - except OSError: - return False - - def main(): ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("archive", help="anim.rpf") @@ -1554,7 +1545,7 @@ def main(): if a.out: if os.path.isdir(a.out): ap.error(f"{a.out} is a directory, not a .json file") - if same_output_path(a.archive, a.out): + if same_report_path(a.archive, a.out): ap.error("output must not overwrite the input archive") try: if a.decode is not None: diff --git a/tools/anim_glb.py b/tools/anim_glb.py index d1dc1059bc..618e68db22 100644 --- a/tools/anim_glb.py +++ b/tools/anim_glb.py @@ -61,6 +61,7 @@ from file_mode import published_mode # noqa: E402 import glb_writer # noqa: E402 from pool_workers import process_workers # noqa: E402 +from report_paths import same_report_path # noqa: E402 import skin_streams # noqa: E402 SYSTEM = 0x50000000 @@ -618,10 +619,6 @@ def parse_offset(text): raise argparse.ArgumentTypeError(f"not an offset: {text!r}") from None -def same_path(first, second): - return anim_clips.same_output_path(first, second) - - def export(args): started = time.monotonic() report = {"archive": os.path.basename(args.archive), "parts": [], "clips": [], "counts": collections.Counter()} @@ -772,11 +769,11 @@ def main(argv=None): outputs = [p for p in (args.out, args.report) if p] for o in outputs: for i in inputs: - if os.path.exists(o) and same_path(i, o): + if os.path.exists(o) and same_report_path(i, o): ap.error(f"{o} is an input; refusing to overwrite it") if args.report and os.path.isdir(args.report): ap.error(f"--report is a directory: {args.report}") - if args.out and args.report and same_path(args.out, args.report): + if args.out and args.report and same_report_path(args.out, args.report): ap.error("the GLB and the report are the same path") if args.clip or args.dictionary is not None: if args.coverage: