divbase-cli 0.1.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,632 @@
1
+ """
2
+ Command line interface for managing files in a DivBase project's store on DivBase.
3
+ """
4
+
5
+ from pathlib import Path
6
+ from zoneinfo import ZoneInfo
7
+
8
+ import typer
9
+ from rich import print
10
+ from rich.table import Table
11
+ from typing_extensions import Annotated
12
+
13
+ from divbase_cli.cli_commands.shared_args_options import FORMAT_AS_TSV_OPTION, PROJECT_NAME_OPTION
14
+ from divbase_cli.cli_exceptions import UnsupportedFileNameError, UnsupportedFileTypeError
15
+ from divbase_cli.config_resolver import ensure_logged_in, resolve_download_dir, resolve_project
16
+ from divbase_cli.services.project_versions import get_version_details_command
17
+ from divbase_cli.services.s3_files import (
18
+ ToDownload,
19
+ download_files_command,
20
+ filter_out_already_downloaded_files,
21
+ get_file_info_command,
22
+ list_files_command,
23
+ list_soft_deleted_files_command,
24
+ restore_objects_command,
25
+ soft_delete_objects_command,
26
+ stream_file_command,
27
+ upload_files_command,
28
+ )
29
+ from divbase_cli.utils import print_rich_table_as_tsv
30
+ from divbase_lib.divbase_constants import SUPPORTED_DIVBASE_FILE_TYPES, UNSUPPORTED_CHARACTERS_IN_FILENAMES
31
+ from divbase_lib.utils import format_file_size
32
+
33
+ file_app = typer.Typer(no_args_is_help=True, help="Download/upload/list files to/from the project's store on DivBase.")
34
+
35
+ NO_FILES_SPECIFIED_MSG = "No files specified for the command, exiting..."
36
+
37
+ DOWNLOAD_DIR_OPTION = typer.Option(
38
+ None,
39
+ "--download-dir",
40
+ "-d",
41
+ help="""Directory to download the files to.
42
+ If not provided, defaults to what you specified in your user config.
43
+ If also not specified in your user config, downloads to the current directory.
44
+ You can also specify "." to download to the current directory.""",
45
+ )
46
+ DISABLE_VERIFY_CHECKSUMS_OPTION = typer.Option(
47
+ False,
48
+ "--disable-verify-checksums",
49
+ "-nc",
50
+ help="Turn off checksum verification which is on by default. "
51
+ "Checksum verification means all downloaded files are verified against their MD5 checksums."
52
+ "It is recommended to leave checksum verification enabled unless you have a specific reason to disable it.",
53
+ )
54
+ PROJECT_VERSION_OPTION = typer.Option(
55
+ None,
56
+ "--project-version",
57
+ "-pv",
58
+ help="User defined version of the project's at which to download the files. If not provided, downloads the latest version of all selected files.",
59
+ )
60
+ DRY_RUN_OPTION = typer.Option(
61
+ False,
62
+ "--dry-run",
63
+ "-n",
64
+ help="If set, will not actually download the files, just print what would be downloaded.",
65
+ )
66
+
67
+
68
+ @file_app.command("ls")
69
+ def list_files(
70
+ format_output_as_tsv: bool = FORMAT_AS_TSV_OPTION,
71
+ prefix_filter: str | None = typer.Option(
72
+ None,
73
+ "--prefix",
74
+ "-pre",
75
+ help="Optional prefix to filter the listed files by name (only list files starting with this prefix).",
76
+ ),
77
+ include_results_files: bool = typer.Option(
78
+ False,
79
+ "--include-results-files",
80
+ "-r",
81
+ help="If set, will also show DivBase query results files which are hidden by default.",
82
+ ),
83
+ show_deleted_files: bool = typer.Option(
84
+ False,
85
+ "--show-deleted-files",
86
+ "-s",
87
+ help="Show the files in the project that are currently soft deleted. These files can be recovered within a certain time frame after deletion",
88
+ ),
89
+ project: str | None = PROJECT_NAME_OPTION,
90
+ ):
91
+ """
92
+ list all currently available files in the project's DivBase store.
93
+
94
+ You can optionally filter the listed files by providing a prefix.
95
+ By default, DivBase query results files are hidden from the listing. Use the --include-results-files option to include them.
96
+ To see information about the versions of each file, use the 'divbase-cli files info [FILE_NAME]' command instead
97
+ """
98
+ if (show_deleted_files and include_results_files) or (show_deleted_files and prefix_filter):
99
+ print(
100
+ "The --show-deleted-files option cannot be used with --include-results-files or --prefix. "
101
+ "Please use these options separately."
102
+ )
103
+ raise typer.Exit(1)
104
+
105
+ project_config = resolve_project(project_name=project)
106
+ logged_in_url = ensure_logged_in(desired_url=project_config.divbase_url)
107
+
108
+ if show_deleted_files:
109
+ files = list_soft_deleted_files_command(
110
+ divbase_base_url=logged_in_url,
111
+ project_name=project_config.name,
112
+ )
113
+
114
+ if not files:
115
+ print(f"No soft deleted files found for the project '{project_config.name}'.")
116
+ return
117
+
118
+ print(f"Soft deleted files for the project '{project_config.name}':")
119
+ for file_details in files:
120
+ cet_timestamp = file_details.last_modified.astimezone(ZoneInfo("CET")).strftime("%Y-%m-%d %H:%M:%S %Z")
121
+
122
+ print(f"- '{file_details.name}' (deleted at: '{cet_timestamp}')")
123
+ print("\nTo restore a soft deleted file, use the 'divbase-cli files restore' command.")
124
+ print("To get more information about one of the soft deleted files, use the 'divbase-cli files info' command.")
125
+ return
126
+
127
+ files = list_files_command(
128
+ divbase_base_url=logged_in_url,
129
+ project_name=project_config.name,
130
+ prefix_filter=prefix_filter,
131
+ include_results_files=include_results_files,
132
+ )
133
+
134
+ if not files:
135
+ print("No files found in the project's store on DivBase.")
136
+ return
137
+
138
+ table = Table(title=f"Files in [bold]{project_config.name}'s [/bold] DivBase Store:")
139
+ table.add_column("Name", justify="left", style="cyan")
140
+ table.add_column("File size", justify="left", style="magenta", no_wrap=True)
141
+ table.add_column("Upload date (CET)", justify="left", style="green", no_wrap=True)
142
+ table.add_column("MD5 checksum", justify="left", style="yellow")
143
+
144
+ for file_details in files:
145
+ cet_timestamp = file_details.last_modified.astimezone(ZoneInfo("CET")).strftime("%Y-%m-%d %H:%M:%S %Z")
146
+ file_size = format_file_size(size_bytes=file_details.size)
147
+ table.add_row(
148
+ file_details.name,
149
+ file_size,
150
+ cet_timestamp,
151
+ file_details.etag,
152
+ )
153
+
154
+ if not format_output_as_tsv:
155
+ print(table)
156
+ else:
157
+ print_rich_table_as_tsv(table=table)
158
+
159
+
160
+ @file_app.command("info")
161
+ def file_info(
162
+ file_name: str = typer.Argument(..., help="Name of the file to get information about."),
163
+ format_output_as_tsv: bool = FORMAT_AS_TSV_OPTION,
164
+ project: str | None = PROJECT_NAME_OPTION,
165
+ ):
166
+ """
167
+ Get detailed information about a specific file in the project's DivBase store.
168
+
169
+ This includes all versions of the file and whether the file is currently marked as soft deleted.
170
+ """
171
+ project_config = resolve_project(project_name=project)
172
+ logged_in_url = ensure_logged_in(desired_url=project_config.divbase_url)
173
+
174
+ file_info = get_file_info_command(
175
+ divbase_base_url=logged_in_url,
176
+ project_name=project_config.name,
177
+ object_name=file_name,
178
+ )
179
+
180
+ if not file_info.versions:
181
+ # API should never return an object with no versions, but just in case
182
+ print("No available versions for this file.")
183
+ return
184
+
185
+ if file_info.is_currently_deleted:
186
+ print(
187
+ "[bold red]Warning: This file is marked as soft deleted.\n[/bold red]"
188
+ + "[italic] To restore a deleted file, use the 'divbase-cli files restore' command.\n"
189
+ + " Or upload the file again using the 'divbase-cli files upload' command.\n[/italic]",
190
+ )
191
+
192
+ table = Table(
193
+ title=f"Available Versions for '[bold]{file_info.object_name}[/bold]'",
194
+ caption="Versions shown are ordered with the latest/current version first/at the top",
195
+ )
196
+ table.add_column("File size", justify="left", style="magenta", no_wrap=True)
197
+ table.add_column("Upload date (CET)", justify="left", style="green", no_wrap=True)
198
+ table.add_column("MD5 checksum", justify="left", style="yellow")
199
+ table.add_column("Version ID", justify="left", style="cyan")
200
+
201
+ for version in file_info.versions:
202
+ cet_timestamp = version.last_modified.astimezone(ZoneInfo("CET")).strftime("%Y-%m-%d %H:%M:%S %Z")
203
+ file_size = format_file_size(size_bytes=version.size)
204
+ table.add_row(
205
+ file_size,
206
+ cet_timestamp,
207
+ version.etag,
208
+ version.version_id,
209
+ )
210
+
211
+ if not format_output_as_tsv:
212
+ print(table)
213
+ else:
214
+ print_rich_table_as_tsv(table=table)
215
+
216
+
217
+ @file_app.command("download")
218
+ def download_files(
219
+ files: list[str] = typer.Argument(
220
+ None, help="Space separated list of files/objects to download from the project's store on DivBase."
221
+ ),
222
+ file_list: Path | None = typer.Option(None, "--file-list", help="Text file with list of files to upload."),
223
+ download_dir: str = DOWNLOAD_DIR_OPTION,
224
+ dry_run: bool = DRY_RUN_OPTION,
225
+ disable_verify_checksums: bool = DISABLE_VERIFY_CHECKSUMS_OPTION,
226
+ project_version: str | None = PROJECT_VERSION_OPTION,
227
+ project: str | None = PROJECT_NAME_OPTION,
228
+ ):
229
+ """
230
+ Download files from the project's store on DivBase.
231
+
232
+ This can be done by either:
233
+ 1. providing a list of files paths directly in the command line
234
+ 2. providing a text file with a list of files to download (new file on each line).
235
+
236
+ To download the latest version of a file, just provide its name. "file1" "file2" etc.
237
+ To download a specific/older version of a file, use the format: "file_name:version_id"
238
+ You can get a file's version id using the 'divbase-cli file info [FILE_NAME]' command.
239
+ You can mix and match latest and specific versions in the same command.
240
+ E.g. to download the latest version of file1 and version "3xcdsdsdiw829x"
241
+ of file2: 'divbase-cli files download file1 file2:3xcdsdsdiw829x'
242
+ """
243
+ project_config = resolve_project(project_name=project)
244
+ logged_in_url = ensure_logged_in(desired_url=project_config.divbase_url)
245
+ download_dir_path = resolve_download_dir(download_dir=download_dir)
246
+
247
+ raw_files_input = _resolve_file_inputs(files=files, file_list=file_list)
248
+
249
+ download_results = download_files_command(
250
+ divbase_base_url=logged_in_url,
251
+ project_name=project_config.name,
252
+ raw_files_input=raw_files_input,
253
+ download_dir=download_dir_path,
254
+ verify_checksums=not disable_verify_checksums,
255
+ dry_run=dry_run,
256
+ project_version=project_version,
257
+ )
258
+
259
+ _pretty_print_download_results(download_results=download_results)
260
+
261
+
262
+ @file_app.command("download-all")
263
+ def download_all_files(
264
+ download_dir: str = DOWNLOAD_DIR_OPTION,
265
+ resume: bool = typer.Option(
266
+ False,
267
+ "--resume",
268
+ "-r",
269
+ help="If set, will attempt to resume an interrupted download. Will check which files have already been fully downloaded (by checking if a file with the same name and checksum already exists in the download directory) and skip downloading those files again.",
270
+ ),
271
+ dry_run: bool = DRY_RUN_OPTION,
272
+ disable_verify_checksums: bool = DISABLE_VERIFY_CHECKSUMS_OPTION,
273
+ project_version: str | None = PROJECT_VERSION_OPTION,
274
+ project: str | None = PROJECT_NAME_OPTION,
275
+ ):
276
+ """
277
+ Download all files in the project's store on DivBase.
278
+ Before the download proceeds you'll be prompted if you want to continue.
279
+ DivBase Query results files will not be included in the download.
280
+
281
+ You can resume ('--resume' / '-r') a 'download-all' command, just make sure you're downloading into the same directory.
282
+ """
283
+ if resume and disable_verify_checksums:
284
+ print(
285
+ "The --resume and --disable-verify-checksums options cannot be used together, "
286
+ "as checksums are used to determine which files don't need to be downloaded. \n"
287
+ "Exiting... "
288
+ )
289
+ return
290
+
291
+ project_config = resolve_project(project_name=project)
292
+ logged_in_url = ensure_logged_in(desired_url=project_config.divbase_url)
293
+ download_dir_path = resolve_download_dir(download_dir=download_dir)
294
+
295
+ all_files: list[ToDownload] = []
296
+ if project_version:
297
+ version_details = get_version_details_command(
298
+ project_name=project_config.name, divbase_base_url=logged_in_url, version_name=project_version
299
+ )
300
+ for version_name, file_details in version_details.files.items():
301
+ all_files.append(
302
+ ToDownload(
303
+ name=version_name,
304
+ etag=file_details["etag"],
305
+ size_bytes=file_details["size"],
306
+ version_id=file_details["version_id"],
307
+ )
308
+ )
309
+
310
+ else:
311
+ list_files_response = list_files_command(
312
+ divbase_base_url=logged_in_url,
313
+ project_name=project_config.name,
314
+ prefix_filter=None,
315
+ include_results_files=False,
316
+ )
317
+ for file_details in list_files_response:
318
+ all_files.append(
319
+ ToDownload(
320
+ name=file_details.name,
321
+ etag=file_details.etag,
322
+ size_bytes=file_details.size,
323
+ version_id=None, # latest version
324
+ )
325
+ )
326
+
327
+ if not all_files:
328
+ print("No files to download as there are no files in the project's store.")
329
+ return
330
+
331
+ # filter files to download based on those which already exist.
332
+ if resume:
333
+ files_to_download, files_to_overwrite = filter_out_already_downloaded_files(
334
+ all_files=all_files, download_dir=download_dir_path
335
+ )
336
+ if files_to_overwrite:
337
+ print(
338
+ "[yellow bold]Warning: The following files already exist in the download directory but have a different checksum."
339
+ "If you choose to proceed, these files will be overwritten by the download:[/yellow bold]"
340
+ )
341
+ for file in files_to_overwrite:
342
+ print(f"- '{file.name}'")
343
+
344
+ files_to_download.extend(files_to_overwrite)
345
+
346
+ if not files_to_download:
347
+ print("No files left to download, your folder matches the project's store on DivBase, exiting...")
348
+ return
349
+ else:
350
+ files_to_download = all_files
351
+
352
+ total_size_bytes = sum(file.size_bytes for file in files_to_download)
353
+ formatted_total_size = format_file_size(size_bytes=total_size_bytes)
354
+
355
+ print(f"There are '{len(files_to_download)}' files to download with a total size of: {formatted_total_size}.")
356
+
357
+ if not dry_run:
358
+ # (dry run will auto exit in the download_files_command)
359
+ do_download = typer.confirm("Do you want to proceed with the download?")
360
+ if not do_download:
361
+ print("Download cancelled...")
362
+ return
363
+
364
+ if project_version:
365
+ raw_files_input = [f"{file.name}:{file.version_id}" for file in files_to_download]
366
+ else:
367
+ raw_files_input = [file.name for file in files_to_download]
368
+ download_results = download_files_command(
369
+ divbase_base_url=logged_in_url,
370
+ project_name=project_config.name,
371
+ raw_files_input=raw_files_input,
372
+ download_dir=download_dir_path,
373
+ verify_checksums=not disable_verify_checksums,
374
+ dry_run=dry_run,
375
+ project_version=None, # We already know the version id of each file, so can skip the processing here.
376
+ )
377
+
378
+ _pretty_print_download_results(download_results=download_results)
379
+
380
+
381
+ @file_app.command("stream")
382
+ def stream_file(
383
+ file_name: str = typer.Argument(..., help="Name of the file you want to stream."),
384
+ version_id: str | None = typer.Option(
385
+ default=None,
386
+ help="Specify this if you want to look at an older/specific version of the file. "
387
+ "If not provided, the latest version of the file is used. "
388
+ "To get a file's version ids, use the 'divbase-cli file info [FILE_NAME]' command.",
389
+ ),
390
+ project: str | None = PROJECT_NAME_OPTION,
391
+ ):
392
+ """
393
+ Stream a file's content to standard output.
394
+
395
+ This allows your to pipe the output to other tools like 'less', 'head', 'zcat' and 'bcftools'.
396
+
397
+ Examples:
398
+ - View a file: divbase-cli files stream my_file.tsv | less
399
+ - View a gzipped file: divbase-cli files stream my_file.vcf.gz | zcat | less
400
+ - Run a bcftools command: divbase-cli files stream my_file.vcf.gz | bcftools view -h - # The "-" tells bcftools to read from standard input
401
+ """
402
+ project_config = resolve_project(project_name=project)
403
+ logged_in_url = ensure_logged_in(desired_url=project_config.divbase_url)
404
+
405
+ stream_file_command(
406
+ divbase_base_url=logged_in_url,
407
+ project_name=project_config.name,
408
+ file_name=file_name,
409
+ version_id=version_id,
410
+ )
411
+
412
+
413
+ @file_app.command("upload")
414
+ def upload_files(
415
+ files: list[Path] | None = typer.Argument(None, help="Space separated list of files to upload."),
416
+ upload_dir: Path | None = typer.Option(None, "--upload-dir", help="Directory to upload all files from."),
417
+ file_list: Path | None = typer.Option(None, "--file-list", help="Text file with list of files to upload."),
418
+ disable_safe_mode: Annotated[
419
+ bool,
420
+ typer.Option(
421
+ "--disable-safe-mode",
422
+ help="Turn off safe mode which is on by default. Safe mode adds 2 extra bits of security by first calculating the MD5 checksum of each file that you're about to upload:"
423
+ "(1) Checks if any of the files you're about to upload already exist (by comparing name and checksum) and if so stops the upload process."
424
+ "(2) Sends the file's checksum when the file is uploaded so the server can verify the upload was successful (by calculating and comparing the checksums)."
425
+ "It is recommended to leave safe mode enabled unless you have a specific reason to disable it.",
426
+ ),
427
+ ] = False,
428
+ project: str | None = PROJECT_NAME_OPTION,
429
+ ):
430
+ """
431
+ Upload files to your project's store on DivBase:
432
+
433
+ To provide files to upload you can either:
434
+ 1. provide a list of files paths directly in the command line
435
+ 2. provide a directory to upload
436
+ 3. provide a text file with or a file list.
437
+ """
438
+ project_config = resolve_project(project_name=project)
439
+ logged_in_url = ensure_logged_in(desired_url=project_config.divbase_url)
440
+
441
+ if bool(files) + bool(upload_dir) + bool(file_list) > 1:
442
+ print("Please specify only one of --files, --upload_dir, or --file-list.")
443
+ raise typer.Exit(1)
444
+
445
+ all_files: set[Path] = set()
446
+ if files:
447
+ all_files.update(files)
448
+ if upload_dir:
449
+ all_files.update([p for p in upload_dir.iterdir() if p.is_file()])
450
+ if file_list:
451
+ with open(file_list) as f:
452
+ for line in f:
453
+ path = Path(line.strip())
454
+ if path.is_file():
455
+ all_files.add(path)
456
+
457
+ if not all_files:
458
+ print(NO_FILES_SPECIFIED_MSG)
459
+ raise typer.Exit(1)
460
+
461
+ _check_for_unsupported_files(all_files)
462
+
463
+ uploaded_results = upload_files_command(
464
+ project_name=project_config.name,
465
+ divbase_base_url=logged_in_url,
466
+ all_files=list(all_files),
467
+ safe_mode=not disable_safe_mode,
468
+ )
469
+
470
+ if uploaded_results.successful:
471
+ print("[green bold]\nThe following files were successfully uploaded: [/green bold]")
472
+ for object in uploaded_results.successful:
473
+ print(f"- '{object.object_name}' created from file at: '{object.file_path.resolve()}'")
474
+
475
+ if uploaded_results.failed:
476
+ print("[red bold]\nERROR: Failed to upload the following files:[/red bold]")
477
+ for failed in uploaded_results.failed:
478
+ print(f"[red]- '{failed.object_name}': Exception: '{failed.exception}'[/red]")
479
+
480
+ raise typer.Exit(1)
481
+
482
+
483
+ @file_app.command("rm")
484
+ def remove_files(
485
+ files: list[str] | None = typer.Argument(
486
+ None, help="Space seperated list of files/objects in the project's store on DivBase to delete."
487
+ ),
488
+ file_list: Path | None = typer.Option(None, "--file-list", help="Text file with list of files to delete."),
489
+ dry_run: bool = typer.Option(
490
+ False, "--dry-run", help="If set, will not actually delete the files, just print what would be deleted."
491
+ ),
492
+ project: str | None = PROJECT_NAME_OPTION,
493
+ ):
494
+ """
495
+ Soft delete files from the project's store on DivBase
496
+
497
+ To provide files to delete you can either:
498
+ 1. provide a list of file names directly in the command line
499
+ 2. provide a text file with a list of files to delete.
500
+
501
+ Note that deleting a non existent file will be treated as a successful deletion.
502
+ """
503
+ project_config = resolve_project(project_name=project)
504
+ logged_in_url = ensure_logged_in(desired_url=project_config.divbase_url)
505
+
506
+ all_files = _resolve_file_inputs(files=files, file_list=file_list)
507
+
508
+ if dry_run:
509
+ print("Dry run mode enabled. The following files would have been deleted:")
510
+ for file in all_files:
511
+ print(f"- '{file}'")
512
+ return
513
+
514
+ deleted_files = soft_delete_objects_command(
515
+ divbase_base_url=logged_in_url,
516
+ project_name=project_config.name,
517
+ all_files=all_files,
518
+ )
519
+
520
+ if deleted_files:
521
+ print("Deleted files:")
522
+ for file in deleted_files:
523
+ print(f"- '{file}'")
524
+ else:
525
+ print("No files were deleted.")
526
+
527
+
528
+ @file_app.command("restore")
529
+ def restore_soft_deleted_files(
530
+ files: list[str] | None = typer.Argument(
531
+ None, help="Space seperated list of files/objects in the project's store on DivBase to restore."
532
+ ),
533
+ file_list: Path | None = typer.Option(None, "--file-list", help="Text file with list of files to restore."),
534
+ project: str | None = PROJECT_NAME_OPTION,
535
+ ):
536
+ """
537
+ Restore soft deleted files from the project's store on DivBase
538
+
539
+ To provide files to restore you can either:
540
+ 1. provide a list of files directly in the command line.
541
+ 2. provide a text file with a list of files to restore (new file on each line).
542
+
543
+ NOTE: Attempts to restore a file that is not soft deleted will be considered successful and the file will remain live. This means you can repeatedly run this command on the same file and get the same response.
544
+ """
545
+ project_config = resolve_project(project_name=project)
546
+ logged_in_url = ensure_logged_in(desired_url=project_config.divbase_url)
547
+
548
+ all_files = _resolve_file_inputs(files=files, file_list=file_list)
549
+
550
+ restored_objects_response = restore_objects_command(
551
+ divbase_base_url=logged_in_url,
552
+ project_name=project_config.name,
553
+ all_files=all_files,
554
+ )
555
+
556
+ if restored_objects_response.restored:
557
+ print("Restored files:")
558
+ for file in restored_objects_response.restored:
559
+ print(f"- '{file}'")
560
+
561
+ if restored_objects_response.not_restored:
562
+ print("[bold red]WARNING: Some files could not be restored:[/bold red]")
563
+ for file in restored_objects_response.not_restored:
564
+ print(f"[red]- '{file}'[/red]")
565
+
566
+ print(
567
+ "Possible reasons for failed restores:\n"
568
+ "1. The object does not exist in the bucket (e.g., a typo in the name).\n"
569
+ "2. The object was hard-deleted and is unrecoverable.\n"
570
+ "3. An unexpected server error occurred during the restore attempt."
571
+ )
572
+
573
+
574
+ def _resolve_file_inputs(files: list[str] | None, file_list: Path | None) -> list[str]:
575
+ """Helper function to resolve file inputs from command line arguments."""
576
+ if bool(files) + bool(file_list) > 1:
577
+ print("Please specify only one of --files or --file-list.")
578
+ raise typer.Exit(1)
579
+
580
+ all_files = set()
581
+ if files:
582
+ all_files.update(files)
583
+ if file_list:
584
+ with open(file_list) as f:
585
+ for line in f:
586
+ all_files.add(line.strip())
587
+
588
+ if not all_files:
589
+ print(NO_FILES_SPECIFIED_MSG)
590
+ raise typer.Exit(1)
591
+ return list(all_files)
592
+
593
+
594
+ def _check_for_unsupported_files(all_files: set[Path]) -> None:
595
+ """
596
+ Helper fn to check if any of the files to be uploaded are not supported by DivBase.
597
+ Raises error if so.
598
+
599
+ This can be to prevent users from:
600
+ 1. Accidentally uploading unsupported files.
601
+ 2. Uploading files that have characters that we reserve for actions like filtering/querying on DivBase.
602
+ (e.g. the syntax "[file_name]:[version_id]" can be used to download specific file versions).
603
+
604
+ This is not a security feature, just for UX purposes.
605
+ """
606
+ unsupported_file_types, unsupported_chars = [], []
607
+ for file_path in all_files:
608
+ if not any(file_path.name.endswith(supported) for supported in SUPPORTED_DIVBASE_FILE_TYPES):
609
+ unsupported_file_types.append(file_path)
610
+
611
+ if any(char in file_path.name for char in UNSUPPORTED_CHARACTERS_IN_FILENAMES):
612
+ unsupported_chars.append(file_path)
613
+
614
+ if unsupported_file_types:
615
+ raise UnsupportedFileTypeError(unsupported_files=unsupported_file_types)
616
+
617
+ if unsupported_chars:
618
+ raise UnsupportedFileNameError(unsupported_files=unsupported_chars)
619
+
620
+
621
+ def _pretty_print_download_results(download_results):
622
+ """Helper fn used by download and download all commands to print the results of the download in a nice format."""
623
+ if download_results.successful:
624
+ print("\n[green bold]Successfully downloaded the following files:[/green bold]")
625
+ for success in download_results.successful:
626
+ print(f"- '{success.object_name}' downloaded to: '{success.file_path.resolve()}'")
627
+
628
+ if download_results.failed:
629
+ print("\n[red bold]ERROR: Failed to download the following files:[/red bold]")
630
+ for failed in download_results.failed:
631
+ print(f"[red]- '{failed.object_name}': [/red] Exception: '{failed.exception}'")
632
+ raise typer.Exit(1)