diff --git a/shallow_backup/__main__.py b/shallow_backup/__main__.py index 1c640f78..16aa57c2 100644 --- a/shallow_backup/__main__.py +++ b/shallow_backup/__main__.py @@ -6,6 +6,8 @@ from .utils import ( mkdir_warn_overwrite, destroy_backup_dir, expand_to_abs_path, new_dir_is_valid) +from .config import * +from .upgrade import upgrade_from_pre_v3 # custom help options @@ -40,6 +42,7 @@ def cli(all, configs, delete_config, destroy_backup, dotfiles, fonts, new_path, Written by Aaron Lichtman (@alichtman). """ + upgrade_from_pre_v3() # Process CLI args admin_action = any([version, delete_config, destroy_backup, show]) diff --git a/shallow_backup/config.py b/shallow_backup/config.py index 6e31ef46..995c4bc0 100644 --- a/shallow_backup/config.py +++ b/shallow_backup/config.py @@ -1,12 +1,13 @@ import sys import json +from os import path, environ from .printing import * from .compatibility import * -from .utils import home_prefix def get_config_path(): - return home_prefix(".shallow-backup") + 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") def get_config(): @@ -18,7 +19,7 @@ def get_config(): with open(config_path) as f: try: config = json.load(f) - except json.decoder.JSONDecodeError as e: + except json.decoder.JSONDecodeError: print_red_bold(f"ERROR: Invalid syntax in {config_path}") sys.exit(1) return config @@ -44,7 +45,7 @@ def get_default_config(): ".gitconfig", ".profile", ".pypirc", - ".shallow-backup", + f"{get_config_path}", ".tmux.conf", ".vimrc", ".zlogin", @@ -121,4 +122,3 @@ def show_config(): print_red_bold("\n{}: ".format(section.capitalize())) for item in contents: print(" {}".format(item)) - diff --git a/shallow_backup/upgrade.py b/shallow_backup/upgrade.py new file mode 100644 index 00000000..3ae3f479 --- /dev/null +++ b/shallow_backup/upgrade.py @@ -0,0 +1,46 @@ +import os +import sys +from shutil import move +from colorama import Fore +from .config import get_config_path +from .printing import prompt_yes_no, print_green_bold, print_red_bold +from .utils import home_prefix, safe_mkdir + + +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. + """ + 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) + + 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('~') + "/", "")) + + 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)