mirror of
https://github.com/borgbackup/borg.git
synced 2025-03-15 00:21:56 +00:00
refactor retrieval of a user's home into get_home_dir()
This commit is contained in:
parent
220d44b2b8
commit
6dd5f6a179
3 changed files with 19 additions and 14 deletions
|
@ -211,19 +211,24 @@ class Statistics:
|
||||||
print(msg, file=stream or sys.stderr, end="\r", flush=True)
|
print(msg, file=stream or sys.stderr, end="\r", flush=True)
|
||||||
|
|
||||||
|
|
||||||
def get_keys_dir():
|
def get_home_dir():
|
||||||
"""Determine where to repository keys and cache"""
|
"""Get user's home directory while preferring a possibly set HOME
|
||||||
|
environment variable
|
||||||
|
"""
|
||||||
# os.path.expanduser() behaves differently for '~' and '~someuser' as
|
# os.path.expanduser() behaves differently for '~' and '~someuser' as
|
||||||
# parameters: when called with an explicit username, the possibly set
|
# parameters: when called with an explicit username, the possibly set
|
||||||
# environment variable HOME is no longer respected. So we have to check if
|
# environment variable HOME is no longer respected. So we have to check if
|
||||||
# it is set and only expand the user's home directory if HOME is unset.
|
# it is set and only expand the user's home directory if HOME is unset.
|
||||||
if os.environ.get('HOME', ''):
|
if os.environ.get('HOME', ''):
|
||||||
home_dir = os.environ.get('HOME')
|
return os.environ.get('HOME')
|
||||||
else:
|
else:
|
||||||
home_dir = os.path.expanduser('~%s' % os.environ.get('USER'))
|
return os.path.expanduser('~%s' % os.environ.get('USER', ''))
|
||||||
|
|
||||||
xdg_config = os.environ.get('XDG_CONFIG_HOME', os.path.join(home_dir, '.config'))
|
|
||||||
|
def get_keys_dir():
|
||||||
|
"""Determine where to repository keys and cache"""
|
||||||
|
|
||||||
|
xdg_config = os.environ.get('XDG_CONFIG_HOME', os.path.join(get_home_dir(), '.config'))
|
||||||
keys_dir = os.environ.get('BORG_KEYS_DIR', os.path.join(xdg_config, 'borg', 'keys'))
|
keys_dir = os.environ.get('BORG_KEYS_DIR', os.path.join(xdg_config, 'borg', 'keys'))
|
||||||
if not os.path.exists(keys_dir):
|
if not os.path.exists(keys_dir):
|
||||||
os.makedirs(keys_dir)
|
os.makedirs(keys_dir)
|
||||||
|
@ -233,7 +238,7 @@ def get_keys_dir():
|
||||||
|
|
||||||
def get_cache_dir():
|
def get_cache_dir():
|
||||||
"""Determine where to repository keys and cache"""
|
"""Determine where to repository keys and cache"""
|
||||||
xdg_cache = os.environ.get('XDG_CACHE_HOME', os.path.join(os.path.expanduser('~'), '.cache'))
|
xdg_cache = os.environ.get('XDG_CACHE_HOME', os.path.join(get_home_dir(), '.cache'))
|
||||||
cache_dir = os.environ.get('BORG_CACHE_DIR', os.path.join(xdg_cache, 'borg'))
|
cache_dir = os.environ.get('BORG_CACHE_DIR', os.path.join(xdg_cache, 'borg'))
|
||||||
if not os.path.exists(cache_dir):
|
if not os.path.exists(cache_dir):
|
||||||
os.makedirs(cache_dir)
|
os.makedirs(cache_dir)
|
||||||
|
|
|
@ -10,7 +10,7 @@ import tempfile
|
||||||
|
|
||||||
from . import __version__
|
from . import __version__
|
||||||
|
|
||||||
from .helpers import Error, IntegrityError, sysinfo
|
from .helpers import Error, IntegrityError, get_home_dir, sysinfo
|
||||||
from .repository import Repository
|
from .repository import Repository
|
||||||
|
|
||||||
import msgpack
|
import msgpack
|
||||||
|
@ -108,8 +108,8 @@ class RepositoryServer: # pragma: no cover
|
||||||
def open(self, path, create=False, lock_wait=None, lock=True):
|
def open(self, path, create=False, lock_wait=None, lock=True):
|
||||||
path = os.fsdecode(path)
|
path = os.fsdecode(path)
|
||||||
if path.startswith('/~'):
|
if path.startswith('/~'):
|
||||||
path = path[1:]
|
path = os.path.join(get_home_dir(), path[2:])
|
||||||
path = os.path.realpath(os.path.expanduser(path))
|
path = os.path.realpath(path)
|
||||||
if self.restrict_to_paths:
|
if self.restrict_to_paths:
|
||||||
for restrict_to_path in self.restrict_to_paths:
|
for restrict_to_path in self.restrict_to_paths:
|
||||||
if path.startswith(os.path.realpath(restrict_to_path)):
|
if path.startswith(os.path.realpath(restrict_to_path)):
|
||||||
|
|
|
@ -6,7 +6,7 @@ import os
|
||||||
import shutil
|
import shutil
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from .helpers import get_keys_dir, get_cache_dir, ProgressIndicatorPercent
|
from .helpers import get_home_dir, get_keys_dir, get_cache_dir, ProgressIndicatorPercent
|
||||||
from .locking import UpgradableLock
|
from .locking import UpgradableLock
|
||||||
from .repository import Repository, MAGIC
|
from .repository import Repository, MAGIC
|
||||||
from .key import KeyfileKey, KeyfileNotFoundError
|
from .key import KeyfileKey, KeyfileNotFoundError
|
||||||
|
@ -187,7 +187,7 @@ class AtticRepositoryUpgrader(Repository):
|
||||||
"""
|
"""
|
||||||
# copy of attic's get_cache_dir()
|
# copy of attic's get_cache_dir()
|
||||||
attic_cache_dir = os.environ.get('ATTIC_CACHE_DIR',
|
attic_cache_dir = os.environ.get('ATTIC_CACHE_DIR',
|
||||||
os.path.join(os.path.expanduser('~'),
|
os.path.join(get_home_dir(),
|
||||||
'.cache', 'attic'))
|
'.cache', 'attic'))
|
||||||
attic_cache_dir = os.path.join(attic_cache_dir, hexlify(self.id).decode('ascii'))
|
attic_cache_dir = os.path.join(attic_cache_dir, hexlify(self.id).decode('ascii'))
|
||||||
borg_cache_dir = os.path.join(get_cache_dir(), hexlify(self.id).decode('ascii'))
|
borg_cache_dir = os.path.join(get_cache_dir(), hexlify(self.id).decode('ascii'))
|
||||||
|
@ -248,7 +248,7 @@ class AtticKeyfileKey(KeyfileKey):
|
||||||
def get_keys_dir():
|
def get_keys_dir():
|
||||||
"""Determine where to repository keys and cache"""
|
"""Determine where to repository keys and cache"""
|
||||||
return os.environ.get('ATTIC_KEYS_DIR',
|
return os.environ.get('ATTIC_KEYS_DIR',
|
||||||
os.path.join(os.path.expanduser('~'), '.attic', 'keys'))
|
os.path.join(get_home_dir(), '.attic', 'keys'))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def find_key_file(cls, repository):
|
def find_key_file(cls, repository):
|
||||||
|
@ -308,7 +308,7 @@ class Borg0xxKeyfileKey(KeyfileKey):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_keys_dir():
|
def get_keys_dir():
|
||||||
return os.environ.get('BORG_KEYS_DIR',
|
return os.environ.get('BORG_KEYS_DIR',
|
||||||
os.path.join(os.path.expanduser('~'), '.borg', 'keys'))
|
os.path.join(get_home_dir(), '.borg', 'keys'))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def find_key_file(cls, repository):
|
def find_key_file(cls, repository):
|
||||||
|
|
Loading…
Add table
Reference in a new issue