Skip to content
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

Respect XDG Base Directory spec #239

Merged
merged 1 commit into from
Nov 14, 2019
Merged
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
3 changes: 3 additions & 0 deletions shallow_backup/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down
10 changes: 5 additions & 5 deletions shallow_backup/config.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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
Expand All @@ -44,7 +45,7 @@ def get_default_config():
".gitconfig",
".profile",
".pypirc",
".shallow-backup",
f"{get_config_path}",
".tmux.conf",
".vimrc",
".zlogin",
Expand Down Expand Up @@ -121,4 +122,3 @@ def show_config():
print_red_bold("\n{}: ".format(section.capitalize()))
for item in contents:
print(" {}".format(item))

46 changes: 46 additions & 0 deletions shallow_backup/upgrade.py
Original file line number Diff line number Diff line change
@@ -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}")
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy Issue found: invalid syntax

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)