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
36 changes: 36 additions & 0 deletions tests/test_world_interior_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,42 @@ def test_pool_size_goes_through_process_workers(self):
self.assertEqual(asked, [70])
self.assertEqual((s["interior_names"], s["placements"]), (2, 3)) # 70 windows on 2 workers, same result

def test_no_report_is_written_without_out(self):
# The report holds game data (every key's hash and distances), so the default run must not leave it in the
# folder it is started from; it used to write reports/world_interior_names.json there.
folder = os.path.join(self.tmp.name, "carved")
os.makedirs(folder)
with open(os.path.join(folder, "a.bin"), "wb") as f:
f.write(interiors())
cwd = os.getcwd()
os.chdir(self.tmp.name)
try:
with redirect_stdout(io.StringIO()) as so:
self.assertEqual(T.main([folder, "--names", self.names]), 0)
finally:
os.chdir(cwd)
self.assertIn("interior_names", so.getvalue())
self.assertFalse(os.path.exists(os.path.join(self.tmp.name, "reports")))

def test_an_out_it_cannot_write_is_one_line(self):
folder = os.path.join(self.tmp.name, "carved")
os.makedirs(folder)
with open(os.path.join(folder, "a.bin"), "wb") as f:
f.write(interiors())
blocker = os.path.join(self.tmp.name, "blocker")
with open(blocker, "w", encoding="utf-8") as f:
f.write("not a folder")
err = io.StringIO()
sys.stderr, saved = err, sys.stderr
try:
with redirect_stdout(io.StringIO()) as so:
self.assertEqual(T.main([folder, "--names", self.names,
"--out", os.path.join(blocker, "r.json")]), 2)
finally:
sys.stderr = saved
self.assertEqual(len(err.getvalue().strip().splitlines()), 1, err.getvalue())
self.assertEqual(so.getvalue(), "")

def test_malformed_names_file_is_one_line(self):
folder = os.path.join(self.tmp.name, "carved")
os.makedirs(folder)
Expand Down
19 changes: 12 additions & 7 deletions tools/world_interior_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def main(argv=None):
ap.add_argument("--start", type=int, help="first byte of an RPF9 data-area window")
ap.add_argument("--limit", type=int, help="window size in bytes (default: to the end of the archive)")
ap.add_argument("--jobs", type=int, default=1, help="parallel windows over an RPF9 archive (default 1)")
ap.add_argument("--out", default=os.path.join("reports", "world_interior_names.json"))
ap.add_argument("--out", help="JSON report of every key (default: no report; it holds game data, so keep it local)")
a = ap.parse_args(argv)
for p in (a.path, a.names):
if not os.path.exists(p):
Expand All @@ -389,11 +389,11 @@ def main(argv=None):
ap.error("--start must not be negative")
if a.limit is not None and a.limit < 1:
ap.error("--limit must be at least 1 byte") # 0 would scan nothing and report every count as 0
out_abs = os.path.abspath(a.out)
if same_report_path(a.out, a.path) or same_report_path(a.out, a.names) or _inside(a.path, out_abs):
out_abs = os.path.abspath(a.out) if a.out else None
if out_abs and (same_report_path(a.out, a.path) or same_report_path(a.out, a.names) or _inside(a.path, out_abs)):
print("world_interior_names.py: --out would overwrite an input: %s" % a.out, file=sys.stderr)
return 2
if os.path.isdir(a.out):
if out_abs and os.path.isdir(a.out):
print("world_interior_names.py: --out is a directory: %s" % a.out, file=sys.stderr)
return 2
try:
Expand All @@ -407,9 +407,14 @@ def main(argv=None):
print("world_interior_names.py: %s" % why, file=sys.stderr)
return 2
summary, rows = resolve(names, scan(a.path, a.start, a.limit, a.jobs), duplicate_keys, malformed)
os.makedirs(os.path.dirname(out_abs), exist_ok=True)
with open(a.out, "w", encoding="utf-8", newline="\n") as f:
json.dump({"summary": summary, "keys": rows}, f, indent=1, sort_keys=True)
if a.out:
try:
os.makedirs(os.path.dirname(out_abs), exist_ok=True)
with open(a.out, "w", encoding="utf-8", newline="\n") as f:
json.dump({"summary": summary, "keys": rows}, f, indent=1, sort_keys=True)
except OSError as exc: # a report path the run cannot write is one line, not a traceback
print("world_interior_names.py: --out %s: %s" % (a.out, exc.strerror or exc), file=sys.stderr)
return 2
print(json.dumps(summary, indent=1, sort_keys=True))
return 0

Expand Down