Skip to content

Commit f78b079

Browse files
committed
Use a temporary directory for downloads
1 parent 74cba3b commit f78b079

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

logilica_cli/weekly_report.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from pathlib import Path
44
import shutil
5+
import tempfile
56

67
import click
78

@@ -16,9 +17,6 @@
1617
upload_doc,
1718
)
1819

19-
# Default values for command options
20-
DEFAULT_DOWNLOADS_DIR = "./lwr_downloaded_pdfs"
21-
2220

2321
@sort_click_command_parameters
2422
@click.command()
@@ -28,10 +26,12 @@
2826
"-t",
2927
"downloads_temp_dir",
3028
type=click.Path(writable=True, file_okay=False, path_type=Path, resolve_path=True),
31-
default=DEFAULT_DOWNLOADS_DIR,
32-
show_default=True,
33-
help="Path to a directory to receive downloaded files"
34-
" (will be created if it doesn't exist; will be deleted if created)",
29+
help=(
30+
"Path to a directory to receive downloaded files"
31+
" (if unspecified, a temporary directory will be used;"
32+
" otherwise, the specified directory will be created if it doesn't exist;"
33+
" if the directory is created by the tool, it will be deleted after the run)"
34+
),
3535
)
3636
@click.option(
3737
"--input",
@@ -143,13 +143,20 @@ def weekly_report(
143143
# If needed, get the credentials now to enable "failing early".
144144
google_credentials = get_google_credentials(config) if output == "gdoc" else None
145145

146-
remove_downloads = not downloads_temp_dir.exists()
146+
remove_downloads = True
147+
if not downloads_temp_dir:
148+
downloads_temp_dir = Path(tempfile.mkdtemp())
149+
elif not downloads_temp_dir.exists():
150+
os.mkdir(downloads_temp_dir)
151+
logging.info("download directory (%s) created", downloads_temp_dir)
152+
else:
153+
# The user supplied an existing directory, don't delete it
154+
remove_downloads = False
147155
logging.debug(
148-
"download directory %s", "does not exist" if remove_downloads else "exists"
156+
"download directory (%s) will%s be removed on termination",
157+
downloads_temp_dir,
158+
"" if remove_downloads else " not",
149159
)
150-
if remove_downloads:
151-
os.mkdir(downloads_temp_dir)
152-
logging.info("download directory, %s, created", downloads_temp_dir)
153160

154161
try:
155162
if source == "logilica":

0 commit comments

Comments
 (0)