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

Add remove backup dir functionality #101

Merged
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
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- [Moritz Schillinger](https://github.com/schilli91)
- [Brandon Temple](https://github.com/Brand-Temp)
- [Peter Yasi](https://github.com/pyasi)
- [Nicole Tibay](https://github.com/neequole)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Options:
-reinstall_configs Reinstall configs from configs backup.
-delete_config Remove config file.
-v Display version and author information and exit.
-destroy_backup Removes the backup directory and its content.
-h, -help, --help Show this message and exit.
```

Expand Down
27 changes: 25 additions & 2 deletions shallow_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,16 @@ def prompt_for_path_update(config):
move_git_folder_to_path(current_path, abs_path)


def destroy_backup_dir(backup_path):
"""
Deletes the backup directory and its content
"""
try:
print("{} Deleting backup directory {} {}...".format(Fore.RED, backup_path, Style.BRIGHT))
shutil.rmtree(backup_path)
except OSError as e:
print("{} Error: {} - {}. {}".format(Fore.RED, e.filename, e.strerror, Style.RESET_ALL))

def backup_prompt():
"""
Use pick library to prompt user with choice of what to backup.
Expand All @@ -683,7 +693,8 @@ def backup_prompt():
' Back up fonts',
' Back up everything',
' Reinstall configs',
' Reinstall packages'
' Reinstall packages',
' Destroy backup'
],
),
]
Expand All @@ -706,7 +717,8 @@ def backup_prompt():
@click.option('-reinstall_configs', is_flag=True, default=False, help="Reinstall configs from configs backup.")
@click.option('-delete_config', is_flag=True, default=False, help="Remove config file.")
@click.option('-v', is_flag=True, default=False, help='Display version and author information and exit.')
def cli(complete, dotfiles, configs, packages, fonts, old_path, new_path, remote, reinstall_packages, reinstall_configs, delete_config, v):
@click.option('-destroy_backup', is_flag=True, default=False, help='Removes the backup directory and its content.')
def cli(complete, dotfiles, configs, packages, fonts, old_path, new_path, remote, reinstall_packages, reinstall_configs, delete_config, v, destroy_backup):
"""
Easily back up installed packages, dotfiles, and more. You can edit which dotfiles are backed up in ~/.shallow-backup.
"""
Expand All @@ -722,6 +734,10 @@ def cli(complete, dotfiles, configs, packages, fonts, old_path, new_path, remote
print(Fore.RED + Style.BRIGHT +
"Removed config file..." + Style.RESET_ALL)
sys.exit()
elif destroy_backup:
backup_home_path = get_config()["backup_path"]
destroy_backup_dir(backup_home_path)
sys.exit()

splash_screen()

Expand Down Expand Up @@ -800,6 +816,13 @@ def cli(complete, dotfiles, configs, packages, fonts, old_path, new_path, remote
reinstall_package(packages_path)
elif selection == "reinstall configs":
reinstall_config_files(configs_path)
elif selection == "destroy backup":
if prompt_yes_no("Erase backup directory: {}?".format(backup_home_path), Fore.RED):
destroy_backup_dir(backup_home_path)
else:
print("{} Exiting to prevent accidental deletion of backup directory... {}".format(
Fore.RED, Style.RESET_ALL))
sys.exit()

git_add_all_commit(repo, backup_home_path)
git_push_if_possible(repo)
Expand Down
50 changes: 50 additions & 0 deletions tests/test_deletes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
import shutil

from shallow_backup import destroy_backup_dir

BACKUP_DIR = 'shallow-backup-test-copy-backup-dir'
TEST_BACKUP_TEXT_FILE = os.path.join(BACKUP_DIR, 'test-file.txt')
DIRS = [BACKUP_DIR]


class TestDeleteMethods:
"""
Test the functionality of deleting
"""

def setup_method(self):
for directory in DIRS:
try:
os.mkdir(directory)
except FileExistsError:
shutil.rmtree(directory)
os.mkdir(directory)
f = open(TEST_BACKUP_TEXT_FILE, "w+")
f.close()

def teardown_method(self):
for directory in DIRS:
try:
shutil.rmtree(directory)
except OSError:
pass

def test_clean_an_existing_backup_directory(self):
"""
Test that deleting the backup directory works as expected
"""
assert os.path.isdir(BACKUP_DIR)
assert os.path.isfile(TEST_BACKUP_TEXT_FILE)
destroy_backup_dir(BACKUP_DIR)
assert not os.path.isdir(BACKUP_DIR)
assert not os.path.isfile(TEST_BACKUP_TEXT_FILE)

def test_can_handle_cleaning_non_existing_backup_directory(self):
"""
Test that we exit gracefully when cleaning an non existing backup directory
"""
nonexist_backup_dir = BACKUP_DIR + "-dummy"
assert not os.path.isdir(nonexist_backup_dir)
destroy_backup_dir(nonexist_backup_dir)
assert not os.path.isdir(nonexist_backup_dir)