From fab050d29bd87ce856809014e051d42de3db0328 Mon Sep 17 00:00:00 2001 From: Aaron Lichtman Date: Sun, 17 Nov 2019 07:27:43 +0100 Subject: [PATCH] Move config to ~/.config/shallow-backup.conf (#242) * Move config to ~/.config/shallow-backup.conf * Bump version to v3.2 --- README.md | 4 +-- shallow_backup/config.py | 7 +++-- shallow_backup/constants.py | 2 +- shallow_backup/upgrade.py | 61 ++++++++++++++++++++----------------- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index fbea9efb..a0f8b039 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ By default, `shallow-backup` backs these up. * `.bash_profile` * `.gitconfig` * `.pypirc` - * `.shallow-backup` + * `.config/shallow-backup.conf` * `.ssh/` * `.vim/` * `.zshrc` @@ -120,7 +120,7 @@ By default, `shallow-backup` backs these up. ### Backup Customization -If you'd like to modify which files are backed up, you have to edit the `~/.shallow-backup` file. There are two recommended ways of doing this. +If you'd like to modify which files are backed up, you have to edit the `~/.config/shallow-backup.conf` file. There are two recommended ways of doing this. 1. Select the appropriate option in the CLI and follow the prompts. 2. Open the file in a text editor and make your changes. diff --git a/shallow_backup/config.py b/shallow_backup/config.py index 62368a31..fdd2b315 100644 --- a/shallow_backup/config.py +++ b/shallow_backup/config.py @@ -6,13 +6,16 @@ from .utils import safe_mkdir +def get_xdg_config_path(): + return environ.get('XDG_CONFIG_HOME') or path.join(path.expanduser('~'), '.config') + + def get_config_path(): test_config_path = environ.get('SHALLOW_BACKUP_TEST_CONFIG_PATH', None) if test_config_path: return test_config_path else: - xdg_config_home = environ.get('XDG_CONFIG_HOME') or path.join(path.expanduser('~'), '.config') - return path.join(xdg_config_home, "shallow-backup", "shallow-backup.conf") + return path.join(get_xdg_config_path(), "shallow-backup.conf") def get_config(): diff --git a/shallow_backup/constants.py b/shallow_backup/constants.py index 55178f1e..47f52d47 100644 --- a/shallow_backup/constants.py +++ b/shallow_backup/constants.py @@ -1,6 +1,6 @@ class ProjInfo: PROJECT_NAME = 'shallow-backup' - VERSION = '3.1' + VERSION = '3.2' AUTHOR_GITHUB = 'alichtman' AUTHOR_FULL_NAME = 'Aaron Lichtman' DESCRIPTION = "Easily create lightweight backups of installed packages, dotfiles, and more." diff --git a/shallow_backup/upgrade.py b/shallow_backup/upgrade.py index 3ae3f479..069ad572 100644 --- a/shallow_backup/upgrade.py +++ b/shallow_backup/upgrade.py @@ -2,7 +2,7 @@ import sys from shutil import move from colorama import Fore -from .config import get_config_path +from .config import get_config_path, get_xdg_config_path from .printing import prompt_yes_no, print_green_bold, print_red_bold from .utils import home_prefix, safe_mkdir @@ -12,35 +12,40 @@ def upgrade_from_pre_v3(): Before v3.0, the config file was stored at ~/.shallow-backup. In v3.0, the XDG Base Directory specification was adopted and the new config is stored in either $XDG_CONFIG_HOME/shallow-backup/shallow-backup.conf or - ~/.config/shallow-backup/shallow-backup.conf. This method upgrades from - v < 3.0 to v3.0 if required. + ~/.config/shallow-backup.conf. This method upgrades from + v < 3.0 to v3.0 or v3.1 if required. """ - old_config_name = ".shallow-backup" - old_config_path = home_prefix(old_config_name) - if os.path.isfile(old_config_path): - if prompt_yes_no("Config file from a version before v3.0 detected. Would you like to upgrade?", Fore.GREEN): - new_config_path = get_config_path() - print_green_bold(f"Moving {old_config_path} to {new_config_path}") - if os.path.exists(new_config_path): - print_red_bold(f"ERROR: {new_config_path} already exists. Manual intervention is required.") - sys.exit(1) + old_config_names = [".shallow-backup", ".config/shallow-backup/shallow-backup.conf"] + for old_config_path, old_config_name in [(home_prefix(old_config_name), old_config_name) for old_config_name in old_config_names]: + if os.path.isfile(old_config_path): + if prompt_yes_no("Config file from a version before v3.1 detected. Would you like to upgrade?", Fore.GREEN): + new_config_path = get_config_path() + print_green_bold(f"Moving {old_config_path} to {new_config_path}") + if os.path.exists(new_config_path): + print_red_bold(f"ERROR: {new_config_path} already exists. Manual intervention is required.") + sys.exit(1) - safe_mkdir(os.path.split(new_config_path)[0]) - move(old_config_path, new_config_path) + safe_mkdir(os.path.split(new_config_path)[0]) + move(old_config_path, new_config_path) - print_green_bold("Replacing old shallow-backup config path with new config path in config file.") - with open(new_config_path, "r") as f: - contents = f.read() - contents = contents.replace(old_config_name, - new_config_path.replace(os.path.expanduser('~') + "/", "")) + print_green_bold("Replacing old shallow-backup config path with new config path in config file.") + with open(new_config_path, "r") as f: + contents = f.read() + contents = contents.replace(old_config_name, + new_config_path.replace(os.path.expanduser('~') + "/", "")) - with open(new_config_path, "w") as f: - f.write(contents) + with open(new_config_path, "w") as f: + f.write(contents) - print_green_bold("Successful upgrade.") - else: - print_red_bold("Please downgrade to a version of shallow-backup before v3.0 if you do not want to upgrade your config.") - sys.exit() - elif os.path.isdir(old_config_path): - print_red_bold(f"ERROR: {old_config_path} is a directory, when we were expecting a file. Manual intervention is required.") - sys.exit(1) + print_green_bold("Successful upgrade.") + else: + print_red_bold("Please downgrade to a version of shallow-backup before v3.0 if you do not want to upgrade your config.") + sys.exit() + elif os.path.isdir(old_config_path): + print_red_bold(f"ERROR: {old_config_path} is a directory, when we were expecting a file. Manual intervention is required.") + sys.exit(1) + + # Clean up ~/.config/shallow-backup + incorrect_config_dir = os.path.join(get_xdg_config_path(), "shallow-backup") + if os.path.isdir(incorrect_config_dir): + os.rmdir(incorrect_config_dir)