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

Add support for XDG_CONFIG_HOME #129

Closed
wants to merge 20 commits into from
Closed

Add support for XDG_CONFIG_HOME #129

wants to merge 20 commits into from

Conversation

0az
Copy link
Contributor

@0az 0az commented May 30, 2020

Implement support for XDG_CONFIG_HOME.

To avoid breaking changes, we use locations in this order:

  • $B2_ACCOUNT_INFO_ENV_VAR: If set
  • ~/.b2_account_info: If exists
  • $XDG_CONFIG_HOME/b2/account_info: If set
  • ~/.b2_account_info: If no path takes priority

@ppolewicz
Copy link
Collaborator

We used to be able to pass a file path to the constructor of SqliteAccountInfo, but you removed it. Please restore it.

Also, could you briefly explain why are you proposing this change?

@0az
Copy link
Contributor Author

0az commented May 30, 2020 via email

@ppolewicz
Copy link
Collaborator

I think it is a good change. Could you please add a unit test to make sure that we don't hit a regression on this new functionality in the future?

@0az
Copy link
Contributor Author

0az commented May 30, 2020

I added unit tests for two cases:

  • XDG_CONFIG_HOME
  • B2_ACCOUNT_INFO_ENV_VAR and XDG_CONFIG_HOME are set.

I'm not sure how to test the third interaction, where XDG_CONFIG_HOME is set, but a database already exists. This may require changing the file_name kwarg to override B2_ACCOUNT_INFO_DEFAULT_FILE instead of overriding everything.

A hermetic testing environment and compatibility with existing code is ensured by modifying _make_sqlite_account_info to take an env kwarg. If it is unset or falsy, it will instantiate SqliteAccountInfo with an empty env. If it is truthy, it will override the usual file_name behavior.

Copy link
Collaborator

@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.

very nice job overall, wonderful extension, I support the motion to not pollute ~/ with config files. I've asked for a few minor tweaks after closer review.

@ppolewicz
Copy link
Collaborator

@0az will you do those changes so that we can fit this PR in the release?

Copy link
Collaborator

@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.

good work, a couple of minor issues remian

@0az
Copy link
Contributor Author

0az commented Jun 8, 2020 via email

@ppolewicz
Copy link
Collaborator

@mlech-reef did we accidentally enable parallel testing in nosetests on Windows? I don't think that ever worked, now appveyor can't run the tests and our contributor here also has issues. Travis is absent from this PR for some reason... Do you have any clue?

@mlech-reef
Copy link
Contributor

mlech-reef commented Jun 9, 2020

...
Travis is absent from this PR for some reason... Do you have any clue?

Travis is triggered properly, but for some reason, it's not showing here. I've seen this issue in other PRs. We should investigate it.
https://travis-ci.org/github/Backblaze/b2-sdk-python/builds/696025513

@mlech-reef
Copy link
Contributor

@mlech-reef did we accidentally enable parallel testing in nosetests on Windows? I don't think that ever worked, now appveyor can't run the tests and our contributor here also has issues.

I don't recall anything like that. AppVeyor fails not because of the parallel runs but because 2 new tests are failing with:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process
It's probably because TempDir uses shutils.rmtree on a directory with sqlite db file created inside.
It's similar to the problem described here: https://stackoverflow.com/questions/1213706/what-user-do-python-scripts-run-as-in-windows
Windows and python sometimes don't get along :)

@@ -36,17 +36,39 @@ class SqliteAccountInfo(UrlPoolAccountInfo):

def __init__(self, file_name=None, last_upgrade_to_run=None):
"""
If ``file_name`` argument is empty or ``None``, path from ``B2_ACCOUNT_INFO`` environment variable is used. If that is not available, a default of ``~/.b2_account_info`` is used.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am reading this docstring over and over and I think apiver doesn't let us change the default location to use XDG_CONFIG_HOME in v1. Someone may have written a program that relies on the promise that when the filename was not passed and B2_ACCOUNT_INFO was not set, then the db file will land in ~/.b2_account_info. After running something, the program might clean the environment by removing that file.

If we make this change and XDG_CONFIG_HOME is defined, sdk user would not expect the configuration of the end user to be suddenly leaked (as it is not cleaned up anymore), possibly allowing access to the following user.

I think this is a good change and we should introduce it, but that should happen with the release of from b2sdk.v2 import *, otherwise we risk introducing a breaking change, while the purpose of apiver is to avoid it unless fixing a bug (which we are not doing here) which would change behavior because the old behavior was just wrong.

Furthermore, we should separate the documentation of the interface from the documentation of the current behavior, so that this can be altered in the future, by mentioning that the user should not define of this algorithm staying exactly the same, but should use self.filename to determine the actually determined location of the file in case it needs to be accessed later.

We did not have a compelling reason to release v2, but this is a good one.

@bwbeach @0az do you agree with my viewpoint? Shall we move it to v2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch: I didn't actually think of that case.

Sounds good to me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@0az then would you please adjust the docstring here? We'll hold off merging this until b2sdk 1.1.0 and b2cli 2.0.0 are shipped, then we'll increment apiver to v2 and finally we'll merge your thing (though a final adjustment will be necessary to provide backwards compatibility)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another question for v2: should SqliteAccountInfo support :memory: or anonymous (empty string) databases?

https://www.sqlite.org/c3ref/open.html

If the filename is ":memory:", then a private, temporary in-memory database is created for the connection. This in-memory database will vanish when the database connection is closed. Future versions of SQLite might make use of additional special filenames that begin with the ":" character. It is recommended that when a database filename actually does begin with a ":" character you should prefix the filename with a pathname such as "./" to avoid ambiguity.

If the filename is an empty string, then a private, temporary on-disk database will be created. This private database will be automatically deleted as soon as the database connection is closed.

Copy link
Collaborator

@ppolewicz ppolewicz Jun 21, 2020

Choose a reason for hiding this comment

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

It looks like that could replace InMemoryAccountInfo with one line of code. I like less code.

@ppolewicz ppolewicz added the v2 label Jun 17, 2020
Co-authored-by: Andrew Zhou <0az@afzhou.com>
@mpnowacki-reef
Copy link
Collaborator

@0az I have submitted a PR that merges master into your xdg branch and moves your changes along with tests to v2: 0az#1
We're planning to wrap up v2 by the end of this week, would you be willing to finish this PR?

@0az
Copy link
Contributor Author

0az commented Apr 28, 2021

Thanks! Was there anything else that I needed to do? It's been a while. 🙃

v2.B2_ACCOUNT_INFO_ENV_VAR, v2.B2_ACCOUNT_INFO_DEFAULT_FILE
)
self.filename = file_name or os.path.expanduser(user_account_info_path)
logger.debug('%s file path to use: %s', self.__class__.__name__, self.filename)
Copy link
Collaborator

Choose a reason for hiding this comment

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

where is logger initialized?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's imported from here:

logger = logging.getLogger(__name__)

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok, but here in b2sdk/v1/account_info.py there is no logger, so it will crash. Logger initialization should be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, bad wording. The initialization happens there, and there's an import in this file for that logger.

It probably should get initialized here anyway, though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

from b2sdk.account_info.sqlite_account_info import logger ohhh. I don't think we should do that, that is super confusing. We should initialize it locally, right?

@ppolewicz
Copy link
Collaborator

This PR seems to contain v2 changes. Can we somehow make it clean? @0az would you mind if we took your changes and rebased them to slide it into v2 at the appropriate moment? The v2 change is a little bit chaotic and I don't want to keep pulling you into this.

@0az
Copy link
Contributor Author

0az commented Apr 28, 2021

This PR seems to contain v2 changes. Can we somehow make it clean?

Michał sent in a PR with the v2 changes, so I merged that in. I can force push that off of the branch, if you want.

@0az would you mind if we took your changes and rebased them to slide it into v2 at the appropriate moment? The v2 change is a little bit chaotic and I don't want to keep pulling you into this.

This is also fine.

@mpnowacki-reef
Copy link
Collaborator

@0az I've come to clean up the mess I made. I squashed all your commits on top of the current master and created a new PR (#235). I have made you the author of the first commit, in order not to steal your thunder. Is that okay with you?

@0az
Copy link
Contributor Author

0az commented Apr 30, 2021

Yeah, 235 looks good. I'll close this PR, then.

@0az 0az closed this Apr 30, 2021
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.

5 participants