Skip to content

Commit

Permalink
Perform the checking STDOUT in CLI without changing the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
athakur-reef committed Oct 17, 2023
1 parent 4d2c1e2 commit 69ad361
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 21 additions & 13 deletions b2/console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1407,15 +1413,16 @@ 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)
downloaded_file = self.api.download_file_by_id(
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

Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 69ad361

Please sign in to comment.