Context
split_book.py, write_chunks.
Problem
Before writing, write_chunks unconditionally does:
if out_path.exists():
shutil.rmtree(out_path)
out_path.mkdir(parents=True, exist_ok=True)
shutil.rmtree only works on plain directories. If <out>/<input-stem> already exists as something else, the call raises an uncaught exception and the tool dies with a raw Python traceback instead of the clear stderr messages used everywhere else in the tool (e.g. load_reader).
Reproduction (both confirmed)
Case A — plain file at the output path:
mkdir -p out_dir && touch out_dir/mybook (a file, not a directory, named after the input's stem)
python split_book.py mybook.pdf --out out_dir
- Crashes with
NotADirectoryError: [Errno 20] Not a directory: '.../out_dir/mybook'
Case B — symlink at the output path:
mkdir -p out_dir && ln -s /some/other/dir out_dir/mybook
python split_book.py mybook.pdf --out out_dir
- Crashes with
OSError: Cannot call rmtree on a symbolic link
Expected
A clear, actionable stderr message and a clean exit (code 1) — e.g. "Error: output path '...' exists and is not a plain directory; remove it or choose a different --out."
Actual
Raw, unhandled traceback in both cases (see above).
Acceptance criteria
Note: the same shutil.rmtree call is also the source of a silent-data-loss bug (unrelated files in the output directory get deleted with no warning) — see the follow-up issue for that; a fix here should probably be designed together with it.
Context
split_book.py,write_chunks.Problem
Before writing,
write_chunksunconditionally does:shutil.rmtreeonly works on plain directories. If<out>/<input-stem>already exists as something else, the call raises an uncaught exception and the tool dies with a raw Python traceback instead of the clear stderr messages used everywhere else in the tool (e.g.load_reader).Reproduction (both confirmed)
Case A — plain file at the output path:
mkdir -p out_dir && touch out_dir/mybook(a file, not a directory, named after the input's stem)python split_book.py mybook.pdf --out out_dirNotADirectoryError: [Errno 20] Not a directory: '.../out_dir/mybook'Case B — symlink at the output path:
mkdir -p out_dir && ln -s /some/other/dir out_dir/mybookpython split_book.py mybook.pdf --out out_dirOSError: Cannot call rmtree on a symbolic linkExpected
A clear, actionable stderr message and a clean exit (code 1) — e.g. "Error: output path '...' exists and is not a plain directory; remove it or choose a different --out."
Actual
Raw, unhandled traceback in both cases (see above).
Acceptance criteria
Note: the same
shutil.rmtreecall is also the source of a silent-data-loss bug (unrelated files in the output directory get deleted with no warning) — see the follow-up issue for that; a fix here should probably be designed together with it.