Skip to content

Commit

Permalink
Merge 693a7f3 into 34a2265
Browse files Browse the repository at this point in the history
  • Loading branch information
alichtman committed Nov 17, 2019
2 parents 34a2265 + 693a7f3 commit 8d33c1a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ By default, `shallow-backup` backs these up.
* `.bash_profile`
* `.gitconfig`
* `.pypirc`
* `.shallow-backup`
* `.config/shallow-backup.conf`
* `.ssh/`
* `.vim/`
* `.zshrc`
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions shallow_backup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
61 changes: 33 additions & 28 deletions shallow_backup/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

0 comments on commit 8d33c1a

Please sign in to comment.