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

Refactor config file architecture. #180

Merged
merged 1 commit into from
Nov 8, 2018
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
26 changes: 11 additions & 15 deletions shallow_backup/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,27 @@ def backup_configs(backup_path, skip=False):
"""
Creates `configs` directory and places config backups there.
Configs are application settings, generally. .plist files count.
In the config file, the value of the configs dictionary is the dest
path relative to the configs/ directory.
"""
print_section_header("CONFIGS", Fore.BLUE)
overwrite_dir_prompt_if_needed(backup_path, skip)
config = get_config()
configs_dir_mapping = config["config_path_to_dest_map"]
plist_files = config["plist_path_to_dest_map"]

print_blue_bold("Backing up configs...")

# backup config dirs in backup_path/<target>/
# backup config files + dirs in backup_path/configs/<target>/
for config, target in configs_dir_mapping.items():
src_dir = home_prefix(config)
configs_backup_path = os.path.join(backup_path, target)
if os.path.isdir(src_dir):
path_to_backup = home_prefix(config)
dest = os.path.join(backup_path, target)
if os.path.isdir(path_to_backup):
# TODO: Exclude Sublime/Atom/VS Code Packages here to speed things up
copytree(src_dir, configs_backup_path, symlinks=True)

# backup plist files in backup_path/configs/plist/
print_blue_bold("Backing up plist files...")
plist_backup_path = os.path.join(backup_path, "plist")
safe_mkdir(plist_backup_path)
for plist, dest in plist_files.items():
plist_path = home_prefix(plist)
if os.path.exists(plist_path):
copyfile(plist_path, os.path.join(backup_path, dest))
copytree(path_to_backup, dest, symlinks=True)
elif os.path.isfile(path_to_backup):
parent_dir = dest[:dest.rfind("/")]
safe_mkdir(parent_dir)
copyfile(path_to_backup, dest)


def backup_packages(backup_path, skip=False):
Expand Down
9 changes: 1 addition & 8 deletions shallow_backup/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys
import json
from utils import home_prefix
from printing import *
Expand Down Expand Up @@ -61,9 +60,7 @@ def get_default_config():
"Library/Preferences/CLion2018.2/" : "clion_2018.2",
"Library/Preferences/PhpStorm2018.2" : "phpstorm_2018.2",
".atom/" : "atom",
},
"plist_path_to_dest_map" : {
"Library/Preferences/com.apple.Terminal.plist": "plist/com.apple.Terminal.plist",
"Library/Preferences/com.apple.Terminal.plist" : "plist/com.apple.Terminal.plist",
},
}

Expand Down Expand Up @@ -96,10 +93,6 @@ def show_config():
print_red_bold("Configs to Backup Path Mapping: ")
for path, dest in contents.items():
print(" {} -> {}".format(path, dest))
elif section == "plist_path_to_dest_map":
print_red_bold("Plist to Backup Path Mapping: ")
for path, dest in contents.items():
print(" {} -> {}".format(path, dest))
# Print section header and then contents indented.
else:
print_red_bold("\n{}: ".format(section.capitalize()))
Expand Down
15 changes: 7 additions & 8 deletions shallow_backup/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ def backup_prefix(path):

config = get_config()
configs_dir_mapping = config["config_path_to_dest_map"]
plist_files = config["plist_path_to_dest_map"]

for target, backup in configs_dir_mapping.items():
if os.path.isdir(backup_prefix(backup)):
copytree(backup_prefix(backup), home_prefix(target))

for target, backup in plist_files.items():
if os.path.exists(backup_prefix(backup)):
copyfile(backup_prefix(backup), home_prefix(target))
path_to_backup = backup_prefix(backup)
dest_path = home_prefix(target)
# TODO: REFACTOR WITH GENERIC COPY FUNCTION.
if os.path.isdir(path_to_backup):
copytree(path_to_backup, dest_path)
elif os.path.isfile(path_to_backup):
copyfile(path_to_backup, dest_path)

print_section_header("CONFIG REINSTALLATION COMPLETED", Fore.BLUE)

Expand Down