|
2 | 2 | import os |
3 | 3 | from pathlib import Path |
4 | 4 | import shutil |
| 5 | +import tempfile |
5 | 6 |
|
6 | 7 | import click |
7 | 8 |
|
|
16 | 17 | upload_doc, |
17 | 18 | ) |
18 | 19 |
|
19 | | -# Default values for command options |
20 | | -DEFAULT_DOWNLOADS_DIR = "./lwr_downloaded_pdfs" |
21 | | - |
22 | 20 |
|
23 | 21 | @sort_click_command_parameters |
24 | 22 | @click.command() |
|
28 | 26 | "-t", |
29 | 27 | "downloads_temp_dir", |
30 | 28 | 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 | + ), |
35 | 35 | ) |
36 | 36 | @click.option( |
37 | 37 | "--input", |
@@ -143,13 +143,20 @@ def weekly_report( |
143 | 143 | # If needed, get the credentials now to enable "failing early". |
144 | 144 | google_credentials = get_google_credentials(config) if output == "gdoc" else None |
145 | 145 |
|
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 |
147 | 155 | 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", |
149 | 159 | ) |
150 | | - if remove_downloads: |
151 | | - os.mkdir(downloads_temp_dir) |
152 | | - logging.info("download directory, %s, created", downloads_temp_dir) |
153 | 160 |
|
154 | 161 | try: |
155 | 162 | if source == "logilica": |
|
0 commit comments