From f2452aef2ae247ef478b4be2703041c14bfeea72 Mon Sep 17 00:00:00 2001 From: Rayyan Ansari Date: Tue, 24 Jan 2023 18:10:49 +0000 Subject: [PATCH] helpers: use platformdirs on win32 --- src/borg/helpers/fs.py | 50 +++++++++++++++---------- src/borg/testsuite/helpers.py | 69 +++++++++++++++++++++++------------ 2 files changed, 76 insertions(+), 43 deletions(-) diff --git a/src/borg/helpers/fs.py b/src/borg/helpers/fs.py index d69a88715..ef135877c 100644 --- a/src/borg/helpers/fs.py +++ b/src/borg/helpers/fs.py @@ -8,6 +8,8 @@ import sys import textwrap +import platformdirs + from .errors import Error from .process import prepare_subprocess_env @@ -58,37 +60,42 @@ def get_base_dir(): return base_dir -def get_keys_dir(): +def get_keys_dir(legacy=True): """Determine where to repository keys and cache""" keys_dir = os.environ.get("BORG_KEYS_DIR") if keys_dir is None: # note: do not just give this as default to the environment.get(), see issue #5979. - keys_dir = os.path.join(get_config_dir(), "keys") + keys_dir = os.path.join(get_config_dir(legacy), "keys") ensure_dir(keys_dir) return keys_dir -def get_security_dir(repository_id=None): +def get_security_dir(repository_id=None, legacy=True): """Determine where to store local security information.""" security_dir = os.environ.get("BORG_SECURITY_DIR") if security_dir is None: # note: do not just give this as default to the environment.get(), see issue #5979. - security_dir = os.path.join(get_config_dir(), "security") + security_dir = os.path.join(get_config_dir(legacy), "security") if repository_id: security_dir = os.path.join(security_dir, repository_id) ensure_dir(security_dir) return security_dir -def get_cache_dir(): +def get_cache_dir(legacy=True): """Determine where to repository keys and cache""" - # Get cache home path - cache_home = os.path.join(get_base_dir(), ".cache") - # Try to use XDG_CACHE_HOME instead if BORG_BASE_DIR isn't explicitly set - if not os.environ.get("BORG_BASE_DIR"): - cache_home = os.environ.get("XDG_CACHE_HOME", cache_home) - # Use BORG_CACHE_DIR if set, otherwise assemble final path from cache home path - cache_dir = os.environ.get("BORG_CACHE_DIR", os.path.join(cache_home, "borg")) + + if legacy: + # Get cache home path + cache_home = os.path.join(get_base_dir(), ".cache") + # Try to use XDG_CACHE_HOME instead if BORG_BASE_DIR isn't explicitly set + if not os.environ.get("BORG_BASE_DIR"): + cache_home = os.environ.get("XDG_CACHE_HOME", cache_home) + # Use BORG_CACHE_DIR if set, otherwise assemble final path from cache home path + cache_dir = os.environ.get("BORG_CACHE_DIR", os.path.join(cache_home, "borg")) + else: + cache_dir = os.environ.get("BORG_CACHE_DIR", platformdirs.user_cache_dir("borg")) + # Create path if it doesn't exist yet ensure_dir(cache_dir) cache_tag_fn = os.path.join(cache_dir, CACHE_TAG_NAME) @@ -110,15 +117,20 @@ def get_cache_dir(): return cache_dir -def get_config_dir(): +def get_config_dir(legacy=True): """Determine where to store whole config""" + # Get config home path - config_home = os.path.join(get_base_dir(), ".config") - # Try to use XDG_CONFIG_HOME instead if BORG_BASE_DIR isn't explicitly set - if not os.environ.get("BORG_BASE_DIR"): - config_home = os.environ.get("XDG_CONFIG_HOME", config_home) - # Use BORG_CONFIG_DIR if set, otherwise assemble final path from config home path - config_dir = os.environ.get("BORG_CONFIG_DIR", os.path.join(config_home, "borg")) + if legacy: + config_home = os.path.join(get_base_dir(), ".config") + # Try to use XDG_CONFIG_HOME instead if BORG_BASE_DIR isn't explicitly set + if not os.environ.get("BORG_BASE_DIR"): + config_home = os.environ.get("XDG_CONFIG_HOME", config_home) + # Use BORG_CONFIG_DIR if set, otherwise assemble final path from config home path + config_dir = os.environ.get("BORG_CONFIG_DIR", os.path.join(config_home, "borg")) + else: + config_dir = os.environ.get("BORG_CONFIG_DIR", platformdirs.user_config_dir("borg")) + # Create path if it doesn't exist yet ensure_dir(config_dir) return config_dir diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index 5cf1900d9..a9c0d6e34 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -45,7 +45,7 @@ from ..helpers import safe_unlink from ..helpers import text_to_json, binary_to_json from ..helpers.passphrase import Passphrase, PasswordRetriesExceeded -from ..platform import is_cygwin +from ..platform import is_cygwin, is_win32 from . import BaseTestCase, FakeInputs, are_hardlinks_supported @@ -597,47 +597,68 @@ def test_get_config_dir(monkeypatch): """test that get_config_dir respects environment""" monkeypatch.delenv("BORG_CONFIG_DIR", raising=False) monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) - assert get_config_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg") - monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") - assert get_config_dir() == os.path.join("/var/tmp/.config", "borg") - monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp") - assert get_config_dir() == "/var/tmp" + if is_win32: + assert get_config_dir(legacy=False) == os.path.join(os.path.expanduser("~"), "AppData", "Local", "borg", "borg") + else: + assert get_config_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg") + monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") + assert get_config_dir() == os.path.join("/var/tmp/.config", "borg") + monkeypatch.setenv("BORG_CONFIG_DIR", "/var/tmp") + assert get_config_dir() == "/var/tmp" def test_get_cache_dir(monkeypatch): """test that get_cache_dir respects environment""" monkeypatch.delenv("BORG_CACHE_DIR", raising=False) monkeypatch.delenv("XDG_CACHE_HOME", raising=False) - assert get_cache_dir() == os.path.join(os.path.expanduser("~"), ".cache", "borg") - monkeypatch.setenv("XDG_CACHE_HOME", "/var/tmp/.cache") - assert get_cache_dir() == os.path.join("/var/tmp/.cache", "borg") - monkeypatch.setenv("BORG_CACHE_DIR", "/var/tmp") - assert get_cache_dir() == "/var/tmp" + if is_win32: + assert get_cache_dir(legacy=False) == os.path.join( + os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "Cache" + ) + else: + assert get_cache_dir() == os.path.join(os.path.expanduser("~"), ".cache", "borg") + monkeypatch.setenv("XDG_CACHE_HOME", "/var/tmp/.cache") + assert get_cache_dir() == os.path.join("/var/tmp/.cache", "borg") + monkeypatch.setenv("BORG_CACHE_DIR", "/var/tmp") + assert get_cache_dir() == "/var/tmp" def test_get_keys_dir(monkeypatch): """test that get_keys_dir respects environment""" monkeypatch.delenv("BORG_KEYS_DIR", raising=False) monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) - assert get_keys_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "keys") - monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") - assert get_keys_dir() == os.path.join("/var/tmp/.config", "borg", "keys") - monkeypatch.setenv("BORG_KEYS_DIR", "/var/tmp") - assert get_keys_dir() == "/var/tmp" + if is_win32: + assert get_keys_dir(legacy=False) == os.path.join( + os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "keys" + ) + else: + assert get_keys_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "keys") + monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") + assert get_keys_dir() == os.path.join("/var/tmp/.config", "borg", "keys") + monkeypatch.setenv("BORG_KEYS_DIR", "/var/tmp") + assert get_keys_dir() == "/var/tmp" def test_get_security_dir(monkeypatch): """test that get_security_dir respects environment""" monkeypatch.delenv("BORG_SECURITY_DIR", raising=False) monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) - assert get_security_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "security") - assert get_security_dir(repository_id="1234") == os.path.join( - os.path.expanduser("~"), ".config", "borg", "security", "1234" - ) - monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") - assert get_security_dir() == os.path.join("/var/tmp/.config", "borg", "security") - monkeypatch.setenv("BORG_SECURITY_DIR", "/var/tmp") - assert get_security_dir() == "/var/tmp" + if is_win32: + assert get_security_dir(legacy=False) == os.path.join( + os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "security" + ) + assert get_security_dir(repository_id="1234", legacy=False) == os.path.join( + os.path.expanduser("~"), "AppData", "Local", "borg", "borg", "security", "1234" + ) + else: + assert get_security_dir() == os.path.join(os.path.expanduser("~"), ".config", "borg", "security") + assert get_security_dir(repository_id="1234") == os.path.join( + os.path.expanduser("~"), ".config", "borg", "security", "1234" + ) + monkeypatch.setenv("XDG_CONFIG_HOME", "/var/tmp/.config") + assert get_security_dir() == os.path.join("/var/tmp/.config", "borg", "security") + monkeypatch.setenv("BORG_SECURITY_DIR", "/var/tmp") + assert get_security_dir() == "/var/tmp" def test_file_size():