From 69ad361d431883fd768340dfbb6921c3ba8a7eee Mon Sep 17 00:00:00 2001 From: athakur-reef Date: Wed, 18 Oct 2023 00:04:34 +0530 Subject: [PATCH] Perform the checking STDOUT in CLI without changing the SDK --- CHANGELOG.md | 6 +++++- b2/console_tool.py | 34 +++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8935a6763..93a9b9a6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +* Support '-' as stdout alias + ### Fixed -* Fix to support '-' as stdout alias +* Fix unable to use '/dev/stdout' by checking the localFileName if that present the `STDOUT` of OS ### Infrastructure * Fix gathering licenses of typeshed libraries diff --git a/b2/console_tool.py b/b2/console_tool.py index 72faeed5d..256d2f5cd 100644 --- a/b2/console_tool.py +++ b/b2/console_tool.py @@ -1369,15 +1369,21 @@ class DownloadFileMixin( MaxDownloadStreamsMixin, DownloadCommand, ): - def correct_local_file_name(self, filename: str): - if filename != '-': - return filename - if os.path.exists('-'): - self._print_stderr( - "WARNING: Filename `-` won't be supported in the future and will be treated as stdout alias." - ) - return filename - return 'CON' if platform.system() == 'Windows' else '/dev/stdout' + def get_download_info_from_local_filename(self, filename: str): + stdout_file_path = 'CON' if platform.system() == 'Windows' else '/dev/stdout' + + if filename == '-': + if os.path.exists('-'): + self._print_stderr( + "WARNING: Filename `-` won't be supported in the future and will be treated as stdout alias." + ) + return filename, 'wb+', True + return stdout_file_path, 'wb', False + + allow_seeking = filename != stdout_file_path + mode = 'wb+' if allow_seeking else 'wb' + + return filename, mode, allow_seeking @B2.register_subcommand @@ -1407,7 +1413,8 @@ def _setup_parser(cls, parser): super()._setup_parser(parser) def run(self, args): - args.localFileName = self.correct_local_file_name(args.localFileName) + args.localFileName, mode, allow_seeking = self.get_download_info_from_local_filename(args.localFileName) + progress_listener = make_progress_listener(args.localFileName, args.noProgress) encryption_setting = self._get_source_sse_setting(args) self._set_threads_from_args(args) @@ -1415,7 +1422,7 @@ def run(self, args): args.fileId, progress_listener, encryption=encryption_setting ) self._print_download_info(downloaded_file) - downloaded_file.save_to(args.localFileName) + downloaded_file.save_to(args.localFileName, mode, allow_seeking) self._print('Download finished') return 0 @@ -1447,7 +1454,8 @@ def _setup_parser(cls, parser): super()._setup_parser(parser) def run(self, args): - args.localFileName = self.correct_local_file_name(args.localFileName) + args.localFileName, mode, allow_seeking = self.get_download_info_from_local_filename(args.localFileName) + self._set_threads_from_args(args) bucket = self.api.get_bucket_by_name(args.bucketName) progress_listener = make_progress_listener(args.localFileName, args.noProgress) @@ -1456,7 +1464,7 @@ def run(self, args): args.b2FileName, progress_listener, encryption=encryption_setting ) self._print_download_info(downloaded_file) - downloaded_file.save_to(args.localFileName) + downloaded_file.save_to(args.localFileName, mode, allow_seeking) self._print('Download finished') return 0