mirror of
https://github.com/borgbackup/borg.git
synced 2025-01-03 05:35:58 +00:00
helpers: use platformdirs on win32
This commit is contained in:
parent
7ffd87739b
commit
f2452aef2a
2 changed files with 76 additions and 43 deletions
|
@ -8,6 +8,8 @@
|
|||
import sys
|
||||
import textwrap
|
||||
|
||||
import platformdirs
|
||||
|
||||
from .errors import Error
|
||||
|
||||
from .process import prepare_subprocess_env
|
||||
|
@ -58,30 +60,32 @@ 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"""
|
||||
|
||||
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
|
||||
|
@ -89,6 +93,9 @@ def get_cache_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
|
||||
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
|
||||
|
|
|
@ -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,6 +597,9 @@ 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)
|
||||
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")
|
||||
|
@ -608,6 +611,11 @@ 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)
|
||||
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")
|
||||
|
@ -619,6 +627,11 @@ 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)
|
||||
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")
|
||||
|
@ -630,6 +643,14 @@ 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)
|
||||
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"
|
||||
|
|
Loading…
Reference in a new issue