Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support '-' as STDOUT alias when download files #211

Conversation

athakur-reef
Copy link

No description provided.

@mjurbanski-reef
Copy link

what about
``

  • support - as /dev/stdout alias in CLI IF no file with such name exists in current directory (same way we do it for - input)

@athakur-reef
Copy link
Author

Support - as /dev/stdout alias.

  • Used CON for stdout at Windows like /dev/stdout at Linux

b2/console_tool.py Outdated Show resolved Hide resolved
@athakur-reef
Copy link
Author

athakur-reef commented Oct 17, 2023

  • Used updated save_to method for download
  • Warn user about deprecation of this like we do on stdin alias

Copy link

@mjurbanski-reef mjurbanski-reef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHANGELOG.md Outdated
Comment on lines 9 to 11
### Fixed

* Fix to support '-' as stdout alias

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is new feature and not a fix

@athakur-reef
Copy link
Author

  • Change the CLI to provide stdout without changing the SDK

Copy link

@ppolewicz ppolewicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both PRs missed an important thing: why and when we use wb+ vs wb. That's messed up and was messed up for years now for no good reason. We have a chance to fix it now but it doesn't seem that we are taking it.

CHANGELOG.md Outdated
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line should not be there

CHANGELOG.md Show resolved Hide resolved
MaxDownloadStreamsMixin,
DownloadCommand,
):
def get_download_info_from_local_filename(self, filename: str):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_download_info_from_local_filename(self, filename: str):
def _get_download_info_from_local_filename(self, filename: str):

the convention in this file is to prefix protected method names with a _

DownloadCommand,
):
def get_download_info_from_local_filename(self, filename: str):
stdout_file_path = 'CON' if platform.system() == 'Windows' else '/dev/stdout'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that should be a (class) const so that it's computed once per program execution and not on every single download call. Here it doesn't matter much because CLI use is one-shot, but generally in terms of how you arrange the code, this should be a constant

return stdout_file_path, 'wb', False

allow_seeking = filename != stdout_file_path
mode = 'wb+' if allow_seeking else 'wb'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be using the same thing for named pipes and it should generally be not CLI but sdk

@athakur-reef athakur-reef force-pushed the atul/b2-70-seeking-stdout branch from 14bd816 to 900957d Compare October 18, 2023 06:06
@athakur-reef athakur-reef force-pushed the atul/b2-70-seeking-stdout branch from 7ffef03 to a4bbc47 Compare October 18, 2023 10:34
@athakur-reef athakur-reef force-pushed the atul/b2-70-seeking-stdout branch from a15c2b3 to 09053e3 Compare October 18, 2023 13:42
@athakur-reef athakur-reef changed the title Use "safe_save_to" method to check the seekability when download files Reinforce the "save_to" method to checkout the seekability when download files Oct 18, 2023
@athakur-reef athakur-reef changed the title Reinforce the "save_to" method to checkout the seekability when download files Support '-' as STDOUT alias when download files Oct 18, 2023
### Fixed
* `--quiet` now will implicitly set `--noProgress` option as well
* Fix failing open non-seekable file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is something wrong grammaticaly with this line

@@ -6,8 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Deprecated
* Support of `-` as a valid filename in `download-file-by-name` or `download-file-by-id` command. In future `-` will be an alias for standard output.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think people typically write "in THE future", right?

Comment on lines +1401 to +1408
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
return self.STDOUT_FILE_PATH
return filename

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
return self.STDOUT_FILE_PATH
return filename
if filename != '-':
return filename
if not os.path.exists(filename):
return self.STDOUT_FILE_PATH
self._print_stderr(
"WARNING: Filename `-` won't be supported in the future and will be treated as stdout alias."
)
return filename

):
STDOUT_FILE_PATH = 'CON' if platform.system() == 'Windows' else '/dev/stdout'

def _correct_local_filename(self, filename: str):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method does not correct a local filename, so its name is confusing. It should be renamed in such a way that the reader can figure out what it does without reading the code to understand what it does.

r_end, w_end = os.pipe()

# Save the current stdout file descriptor for later restoration
stdout_fd = os.dup(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use something like STDOUT_DESCRIPTOR = sys.stdout.fileno() instead of a hardcoded 1 here

@@ -1210,6 +1210,38 @@ def _test_download_threads(self, download_by, num_threads):
self._run_command(command)
self.assertEqual(b'hello world', self._read_file(local_download))

def test_download_to_non_seekable_file(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have added new logic in _correct_local_filename, but you haven't added any unit tests for it, while the integration tests for this new feature are insufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants