Skip to content

Commit 867fb2b

Browse files
committed
Add --debug option. Tear down build directories but not in debug mode.
1 parent c97b992 commit 867fb2b

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/pdfbaker/__main__.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ def cli():
2727
)
2828
@click.option("-v", "--verbose", is_flag=True, help="Show debug information")
2929
@click.option("-q", "--quiet", is_flag=True, help="Show errors only")
30-
def bake(config_file, verbose=False, quiet=False):
30+
@click.option(
31+
"-d",
32+
"--debug",
33+
is_flag=True,
34+
help="Debug mode (implies --verbose, keeps build files)",
35+
)
36+
def bake(config_file, verbose=False, quiet=False, debug=False):
3137
"""Parse config file and bake PDFs."""
38+
if debug:
39+
verbose = True
3240
if quiet:
3341
logging.getLogger().setLevel(logging.ERROR)
3442
elif verbose:
@@ -44,6 +52,9 @@ def bake(config_file, verbose=False, quiet=False):
4452
for doc_name, doc_path in document_paths.items():
4553
_process_document(doc_name, doc_path, config, build_dir, dist_dir)
4654

55+
if not debug:
56+
_teardown_build_directories(build_dir, document_paths.keys())
57+
4758
logger.info("Done.")
4859
return 0
4960

@@ -158,5 +169,37 @@ def _setup_document_output_directories(build_dir, dist_dir, doc_name):
158169
return doc_build_dir, doc_dist_dir
159170

160171

172+
def _teardown_build_directories(build_dir, doc_names):
173+
"""Clean up build directories after successful processing.
174+
175+
Args:
176+
build_dir: Base build directory
177+
doc_names: Names of processed documents
178+
"""
179+
for doc_name in doc_names:
180+
doc_build_dir = build_dir / doc_name
181+
if doc_build_dir.exists():
182+
# Remove all files in the document's build directory
183+
for file_path in doc_build_dir.iterdir():
184+
if file_path.is_file():
185+
file_path.unlink()
186+
187+
# Try to remove the document's build directory if empty
188+
try:
189+
doc_build_dir.rmdir()
190+
except OSError:
191+
# Directory not empty (might contain subdirectories)
192+
logger.warning(
193+
"Build directory of document not empty, keeping: %s", doc_build_dir
194+
)
195+
196+
# Try to remove the base build directory if empty
197+
try:
198+
build_dir.rmdir()
199+
except OSError:
200+
# Directory not empty
201+
logger.warning("Build directory not empty, keeping: %s", build_dir)
202+
203+
161204
if __name__ == "__main__":
162205
sys.exit(cli())

0 commit comments

Comments
 (0)