From 927cb8e15db32e3d71b1bd40f7ba781de67185e4 Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Thu, 26 Oct 2023 08:24:08 -0600 Subject: [PATCH 1/3] Fix location for juju config directory pointed by XDG_DATA_HOME XDG_DATA_HOME is supposed to point to the parent directory for applications. We were incorrectly setting that as Juju config dir. --- juju/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/juju/utils.py b/juju/utils.py index bc72be94..d81bf1c4 100644 --- a/juju/utils.py +++ b/juju/utils.py @@ -52,7 +52,9 @@ def juju_config_dir(): # Second option: $XDG_DATA_HOME for ~/.local/share if not config_dir: - config_dir = os.environ.get('XDG_DATA_HOME', None) + base_dir = os.environ.get('XDG_DATA_HOME', None) + if base_dir is not None: + config_dir = base_dir + '/juju' # Third option: just set it to ~/.local/share/juju if not config_dir: From c1ede8d423165b85fbf5145144803f880635a7cb Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Thu, 26 Oct 2023 08:47:28 -0600 Subject: [PATCH 2/3] Handle juju config dirs with pathlib --- juju/utils.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/juju/utils.py b/juju/utils.py index d81bf1c4..7ec89e48 100644 --- a/juju/utils.py +++ b/juju/utils.py @@ -48,19 +48,17 @@ def juju_config_dir(): """ # Check $JUJU_DATA first - config_dir = os.environ.get('JUJU_DATA', None) + config_dir = Path(os.environ.get('JUJU_DATA', None)) # Second option: $XDG_DATA_HOME for ~/.local/share - if not config_dir: - base_dir = os.environ.get('XDG_DATA_HOME', None) - if base_dir is not None: - config_dir = base_dir + '/juju' + if not config_dir.exists(): + config_dir = Path(os.environ.get('XDG_DATA_HOME', '')) / 'juju' # Third option: just set it to ~/.local/share/juju - if not config_dir: - config_dir = '~/.local/share/juju' + if not config_dir.exists(): + config_dir = Path('~/.local/share/juju') - return os.path.abspath(os.path.expanduser(config_dir)) + return str(config_dir.expanduser().resolve()) def juju_ssh_key_paths(): From 11207386d32237a32856cea8850f1f5b4830e12e Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Thu, 26 Oct 2023 09:37:38 -0600 Subject: [PATCH 3/3] Avoid setting None in Path objects -- fix unit tests --- juju/utils.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/juju/utils.py b/juju/utils.py index 7ec89e48..d52e49e3 100644 --- a/juju/utils.py +++ b/juju/utils.py @@ -47,16 +47,15 @@ def juju_config_dir(): * ~/.local/share/juju """ - # Check $JUJU_DATA first - config_dir = Path(os.environ.get('JUJU_DATA', None)) - - # Second option: $XDG_DATA_HOME for ~/.local/share - if not config_dir.exists(): - config_dir = Path(os.environ.get('XDG_DATA_HOME', '')) / 'juju' - - # Third option: just set it to ~/.local/share/juju - if not config_dir.exists(): - config_dir = Path('~/.local/share/juju') + # Set it to ~/.local/share/juju as default + config_dir = Path('~/.local/share/juju') + + # Check $JUJU_DATA + if juju_data := os.environ.get('JUJU_DATA'): + config_dir = Path(juju_data) + # Secondly check: $XDG_DATA_HOME for ~/.local/share + elif xdg_data_home := os.environ.get('XDG_DATA_HOME'): + config_dir = Path(xdg_data_home) / 'juju' return str(config_dir.expanduser().resolve())