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

lib/init: add GRASS_CONFIG_DIR environment variable #3899

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

imincik
Copy link
Contributor

@imincik imincik commented Jun 19, 2024

GRASS_CONFIG_DIR variable allows to set alternative root path for
.grass<VERSION> configuration directory. If not set, $HOME directory
is used.

The main use case of this PR is to be able to run multiple isolated instances of grass. Using $HOME or $XDG_CONFIG_HOME variables to force grass to store configuration in alternative directory is not a good idea, because this will also have an impact on other tools relying on those variables. That's why I introduced a new grass specific environment variable.

/cc @landam

GRASS_CONFIG_DIR variable allows to set alternative root path for
`.grass<VERSION>` configuration directory. If not set, $HOME directory
is used.
@nilason
Copy link
Contributor

nilason commented Jun 19, 2024

Related #3153

@landam landam self-assigned this Jun 19, 2024
@landam landam added this to the 8.5.0 milestone Jun 19, 2024
@nilason
Copy link
Contributor

nilason commented Jun 19, 2024

Would it be possible to, instead of introducing another GRASS env var, adopt our code to respect (if set) $XDG_CONFIG_HOME (see https://specifications.freedesktop.org/basedir-spec/basedir-spec-0.6.html)?

@imincik
Copy link
Contributor Author

imincik commented Jun 19, 2024

Would it be possible to, instead of introducing another GRASS env var, adopt our code to respect (if set) $XDG_CONFIG_HOME (see https://specifications.freedesktop.org/basedir-spec/basedir-spec-0.6.html)?

The main use case of this PR is to be able to run multiple isolated instances of grass. Using $HOME or $XDG_CONFIG_HOME variables to force grass to store configuration in alternative directory is not a good idea, because this will also have an impact on other tools relying on those variables. That's why I introduced a new grass specific environment variable.

@imincik
Copy link
Contributor Author

imincik commented Jun 19, 2024

Would it be possible to, instead of introducing another GRASS env var, adopt our code to respect (if set) $XDG_CONFIG_HOME (see https://specifications.freedesktop.org/basedir-spec/basedir-spec-0.6.html)?

But I agree, that usage of XDG_CONFIG_HOME instead of HOME is a good fix for #3153 .

@nilason
Copy link
Contributor

nilason commented Jun 19, 2024

The main use case of this PR is to be able to run multiple isolated instances of grass. Using $HOME or $XDG_CONFIG_HOME variables to force grass to store configuration in alternative directory is not a good idea, because this will also have an impact on other tools relying on those variables. That's why I introduced a new grass specific environment variable.

I see.

Copy link
Contributor

@nilason nilason left a comment

Choose a reason for hiding this comment

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

some typos

lib/init/grass.py Outdated Show resolved Hide resolved
lib/init/variables.html Outdated Show resolved Hide resolved
landam and others added 2 commits June 19, 2024 17:35
Co-authored-by: Nicklas Larsson <n_larsson@yahoo.com>
Co-authored-by: Nicklas Larsson <n_larsson@yahoo.com>
@github-actions github-actions bot added Python Related code is in Python HTML Related code is in HTML libraries docs labels Jun 19, 2024
lib/init/grass.py Outdated Show resolved Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
nilason
nilason previously approved these changes Jun 20, 2024
@landam landam marked this pull request as draft June 20, 2024 08:48
- create grass.utils.config to avoid code duplication in grass.py and core.utils
- change grass.py in order to use grass.utils.config.get_grass_config_path()
@github-actions github-actions bot added the GUI wxGUI related label Jun 20, 2024
@landam landam marked this pull request as ready for review June 26, 2024 12:39
@nilason
Copy link
Contributor

nilason commented Jun 26, 2024

I will not be able to look at and test this this week, but please hold on, I see there are Mac specific changes now that need hands-on testing/review.

@landam
Copy link
Member

landam commented Jun 27, 2024

I will not be able to look at and test this this week, but please hold on, I see there are Mac specific changes now that need hands-on testing/review.

Sure. This PR shouldn't change behaviour compared to #3694.

Copy link
Member

@wenzeslaus wenzeslaus left a comment

Choose a reason for hiding this comment

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

This seems like a reasonable feature, although I'm wondering whether other (all?) software allows the same, i.e., having its own variable to set config dir just for that software.

As for the implementation, there is couple of other places which may need fixing. g.extension needs to do something, but I didn't examine what. lib/gis/home.c worries me a little bit as it seems to figure out the path as well, at this point duplicating what is in Python which is more complex code with this PR and the anticipated XDG change.

Comment on lines +54 to +57
if env.get("GRASS_CONFIG_DIR"):
# override config_path if GRASS_CONFIG_DIR environmental
# variable is defined
config_path = os.path.join(env.get("GRASS_CONFIG_DIR"), config_dirname)
Copy link
Member

Choose a reason for hiding this comment

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

This overrides, so it can be first and no tests of base env vars or existence of paths from them is needed. (Current code would raise if HOME does not exist even when GRASS_CONFIG_DIR is set.)

" system support"
)
if not os.path.exists(env.get(env_dirname)):
raise IsADirectoryError(
Copy link
Member

Choose a reason for hiding this comment

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

Raising IsADirectoryError when it is confusing:

exception IsADirectoryError

Raised when a file operation (such as os.remove()) is requested on a directory. Corresponds to errno EISDIR.

NotADirectoryError seems to be appropriate, but in that case, the test should be isdir not exists.

@@ -29,15 +29,34 @@ def get_grass_config_dir(major_version, minor_version, env):
Determines path of GRASS GIS user configuration directory.
"""
# The code is in sync with grass.app.runtime (but not the same).
env_dirname = "APPDATA" if WINDOWS else "HOME"
if env.get(env_dirname) is None:
raise KeyError(
Copy link
Member

Choose a reason for hiding this comment

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

I can't quite see what exception would make sense here. KeyError is more related to the underlying dictionary rather than the state the error message is trying to describe. Consider RuntimeError.

Comment on lines -382 to +385
def get_grass_config_dir():
"""Get configuration directory
def create_grass_config_dir():
"""Create configuration directory
Copy link
Member

Choose a reason for hiding this comment

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

Without examining the code more closely, I don't see where the creation happens, but it is not here. I think "creation" is expected to refer to creation of the actual directory rather than construction of the path which is referred to by the simple "get".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs GUI wxGUI related HTML Related code is in HTML libraries Python Related code is in Python
4 participants