refactor retrieval of a user's home into get_home_dir()

This commit is contained in:
Daniel Reichelt 2016-03-17 02:55:55 +01:00
parent 220d44b2b8
commit 6dd5f6a179
3 changed files with 19 additions and 14 deletions

View File

@ -211,19 +211,24 @@ class Statistics:
print(msg, file=stream or sys.stderr, end="\r", flush=True)
def get_keys_dir():
"""Determine where to repository keys and cache"""
def get_home_dir():
"""Get user's home directory while preferring a possibly set HOME
environment variable
"""
# os.path.expanduser() behaves differently for '~' and '~someuser' as
# parameters: when called with an explicit username, the possibly set
# 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.
if os.environ.get('HOME', ''):
home_dir = os.environ.get('HOME')
return os.environ.get('HOME')
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'))
if not os.path.exists(keys_dir):
os.makedirs(keys_dir)
@ -233,7 +238,7 @@ def get_keys_dir():
def get_cache_dir():
"""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'))
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)

View File

@ -10,7 +10,7 @@ import tempfile
from . import __version__
from .helpers import Error, IntegrityError, sysinfo
from .helpers import Error, IntegrityError, get_home_dir, sysinfo
from .repository import Repository
import msgpack
@ -108,8 +108,8 @@ class RepositoryServer: # pragma: no cover
def open(self, path, create=False, lock_wait=None, lock=True):
path = os.fsdecode(path)
if path.startswith('/~'):
path = path[1:]
path = os.path.realpath(os.path.expanduser(path))
path = os.path.join(get_home_dir(), path[2:])
path = os.path.realpath(path)
if self.restrict_to_paths:
for restrict_to_path in self.restrict_to_paths:
if path.startswith(os.path.realpath(restrict_to_path)):

View File

@ -6,7 +6,7 @@ import os
import shutil
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 .repository import Repository, MAGIC
from .key import KeyfileKey, KeyfileNotFoundError
@ -187,7 +187,7 @@ class AtticRepositoryUpgrader(Repository):
"""
# copy of attic's get_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'))
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'))
@ -248,7 +248,7 @@ class AtticKeyfileKey(KeyfileKey):
def get_keys_dir():
"""Determine where to repository keys and cache"""
return os.environ.get('ATTIC_KEYS_DIR',
os.path.join(os.path.expanduser('~'), '.attic', 'keys'))
os.path.join(get_home_dir(), '.attic', 'keys'))
@classmethod
def find_key_file(cls, repository):
@ -308,7 +308,7 @@ class Borg0xxKeyfileKey(KeyfileKey):
@staticmethod
def get_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
def find_key_file(cls, repository):