Skip to content

xdg support for .mackup.cfg and .mackup #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions mackup/appsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@


from .constants import APPS_DIR
from .constants import CUSTOM_APPS_DIR
from .constants import (CUSTOM_APPS_DIR, XDG_CUSTOM_APPS_DIR)
from . import utils


class ApplicationsDatabase(object):
Expand Down Expand Up @@ -54,25 +55,15 @@ def __init__(self):
self.apps[app_name]['configuration_files'].add(path)

# Add the XDG configuration files to sync
xdg_config_home = os.environ.get('XDG_CONFIG_HOME')
xdg_config_home = utils.get_xdg_config_home()
if xdg_config_home:
if not os.path.exists(xdg_config_home):
raise ValueError('$XDG_CONFIG_HOME: {} does not exist'
.format(xdg_config_home))
home = os.path.expanduser('~/')
if not xdg_config_home.startswith(home):
raise ValueError('$XDG_CONFIG_HOME: {} must be '
'somewhere within your home '
'directory: {}'
.format(xdg_config_home, home))
if config.has_section('xdg_configuration_files'):
for path in config.options('xdg_configuration_files'):
if path.startswith('/'):
raise ValueError('Unsupported absolute path: '
'{}'
.format(path))
'{}'.format(path))
path = os.path.join(xdg_config_home, path)
path = path.replace(home, '')
path = path.replace(os.path.expanduser('~/'), '')
(self.apps[app_name]['configuration_files']
.add(path))

Expand All @@ -94,7 +85,12 @@ def get_config_files():
# Configure the config parser
apps_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
APPS_DIR)
custom_apps_dir = os.path.join(os.environ['HOME'], CUSTOM_APPS_DIR)
if os.environ.get('XDG_CONFIG_HOME'):
custom_apps_dir = os.path.join(utils.get_mackup_config_home(),
XDG_CUSTOM_APPS_DIR)
else:
custom_apps_dir = os.path.join(utils.get_mackup_config_home(),
CUSTOM_APPS_DIR)

# List of stock application config files
config_files = set()
Expand Down
9 changes: 7 additions & 2 deletions mackup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import os
import os.path
import sys
from . import utils

from .constants import (MACKUP_BACKUP_PATH,
MACKUP_CONFIG_FILE,
XDG_MACKUP_CONFIG_FILE,
ENGINE_DROPBOX,
ENGINE_GDRIVE,
ENGINE_COPY,
Expand Down Expand Up @@ -143,10 +145,13 @@ def _setup_parser(self, filename=None):

# If we are not overriding the config filename
if not filename:
filename = MACKUP_CONFIG_FILE
if os.environ.get('XDG_CONFIG_HOME'):
filename = XDG_MACKUP_CONFIG_FILE
else:
filename = MACKUP_CONFIG_FILE

parser = configparser.SafeConfigParser(allow_no_value=True)
parser.read(os.path.join(os.path.join(os.environ['HOME'], filename)))
parser.read(os.path.join(utils.get_mackup_config_home(), filename))

return parser

Expand Down
10 changes: 7 additions & 3 deletions mackup/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
# Default Mackup backup path where it stores its files in Dropbox
MACKUP_BACKUP_PATH = 'Mackup'

# Mackup config file
# Mackup config file for non XDG users
MACKUP_CONFIG_FILE = '.mackup.cfg'

# Directory that can contains user defined app configs
# Directory that can contains user defined app configs; for non XDG users
CUSTOM_APPS_DIR = '.mackup'

# Mackup config file; for XDG users
XDG_MACKUP_CONFIG_FILE = 'mackup.cfg'
# Directory that can contains user defined app configs; for XDG users
XDG_CUSTOM_APPS_DIR = 'mackup'

# Supported engines
ENGINE_DROPBOX = 'dropbox'
ENGINE_GDRIVE = 'google_drive'
Expand Down
41 changes: 40 additions & 1 deletion mackup/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ def can_file_be_synced_on_current_platform(path):
(bool): True if given file can be synced
"""
can_be_synced = True

# If the given path is relative, prepend home
fullpath = os.path.join(os.environ['HOME'], path)

Expand All @@ -412,3 +411,43 @@ def can_file_be_synced_on_current_platform(path):
can_be_synced = False

return can_be_synced


def get_xdg_config_home():
"""
Get and validate the $XDG_CONFIG_HOME path if set

Returns:
(string|None): $XDG_CONFIG_HOME (if set); else None
"""
xdg_config_home = os.environ.get('XDG_CONFIG_HOME')
if xdg_config_home:
if not os.path.exists(xdg_config_home):
raise ValueError('$XDG_CONFIG_HOME: {} does not exist'
.format(xdg_config_home))

home = os.path.expanduser('~/')
if not xdg_config_home.startswith(home):
raise ValueError('$XDG_CONFIG_HOME: {} must be '
'somewhere within your home '
'directory: {}'
.format(xdg_config_home, home))

return xdg_config_home


def get_mackup_config_home():
"""
Get the full path to the mackup configuration home
(where .mackup & .mackup.cfg are stored)

Returns:
(string): $XDG_CONFIG_HOME (if set); else full path of user's home dir
"""
xdg_config_home = get_xdg_config_home()
if xdg_config_home:
mackup_config_home = xdg_config_home
else:
mackup_config_home = os.path.expanduser('~/')

return mackup_config_home