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

Clean up #119

Merged
merged 2 commits into from
Oct 19, 2018
Merged
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
75 changes: 26 additions & 49 deletions shallow_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

def print_version_info():
version = "{} v{} by {} -> (Github: {})".format(Constants.PROJECT_NAME,
Constants.VERSION,
Constants.AUTHOR_FULL_NAME,
Constants.AUTHOR_GITHUB)
Constants.VERSION,
Constants.AUTHOR_FULL_NAME,
Constants.AUTHOR_GITHUB)
line = "-" * (len(version))
print(Fore.RED + Style.BRIGHT + line)
print(version)
Expand Down Expand Up @@ -51,8 +51,6 @@ def print_section_header(title, COLOR):
def prompt_yes_no(message, color):
"""
Print question and return True or False depending on user selection from list.
bottom_line should be used for one liners.
Otherwise, it's the second line you want printed.
"""
questions = [inquirer.List('choice',
message=color + Style.BRIGHT + message + Fore.BLUE,
Expand All @@ -67,7 +65,6 @@ def prompt_yes_no(message, color):
# Utilities
###########


def run_shell_cmd(command):
"""
Wrapper on subprocess.run that handles both lists and strings as commands.
Expand All @@ -79,7 +76,7 @@ def run_shell_cmd(command):
else:
process = sp.run(command, stdout=sp.PIPE)
return process
except FileNotFoundError: # If package manager is missing
except FileNotFoundError: # If package manager is missing
return None


Expand All @@ -101,7 +98,7 @@ def make_dir_warn_overwrite(path):
subdirs = ["dotfiles", "packages", "fonts", "configs"]
if os.path.exists(path) and path.split("/")[-1] in subdirs:
print(Fore.RED + Style.BRIGHT +
"Directory {} already exists".format(path) + "\n" + Style.RESET_ALL)
"Directory {} already exists".format(path) + "\n" + Style.RESET_ALL)
if prompt_yes_no("Erase directory and make new back up?", Fore.RED):
shutil.rmtree(path)
os.makedirs(path)
Expand Down Expand Up @@ -161,10 +158,10 @@ def get_configs_path_mapping():
return {
"Library/Application Support/Sublime Text 2/Packages/User/": "sublime_2",
"Library/Application Support/Sublime Text 3/Packages/User/": "sublime_3",
"Library/Preferences/IntelliJIdea2018.2/":"intellijidea_2018.2",
"Library/Preferences/PyCharm2018.2/":"pycharm_2018.2",
"Library/Preferences/CLion2018.2/":"clion_2018.2",
"Library/Preferences/PhpStorm2018.2":"phpstorm_2018.2",
"Library/Preferences/IntelliJIdea2018.2/": "intellijidea_2018.2",
"Library/Preferences/PyCharm2018.2/": "pycharm_2018.2",
"Library/Preferences/CLion2018.2/": "clion_2018.2",
"Library/Preferences/PhpStorm2018.2": "phpstorm_2018.2",
}


Expand Down Expand Up @@ -204,15 +201,11 @@ def backup_dotfiles(backup_path):
dotfolders_mp_in.append(
(os.path.join(home_path, dotfolder), backup_path))


dotfiles_mp_in = []
for dotfile in dotfiles:
dotfiles_mp_in.append((os.path.join(home_path, dotfile), os.path.join(backup_path, dotfile)))

####
# Back up System and Application Preferences and Settings
####

# TODO: Extract these paths to constants

# Sublime Text Configs
Expand Down Expand Up @@ -450,11 +443,10 @@ def reinstall_package(packages_path):
elif pm == "macports":
print(Fore.RED + "WARNING: Macports reinstallation is not supported." + Style.RESET_ALL)
elif pm == "gem":
print(
Fore.RED + "WARNING: Gem reinstallation is not supported." + Style.RESET_ALL)
print(Fore.RED + "WARNING: Gem reinstallation is not supported." + Style.RESET_ALL)
elif pm == "cargo":
print(Fore.RED + "WARNING: Cargo reinstallation is not possible at the moment. "
"\n -> https://github.com/rust-lang/cargo/issues/5593" + Style.RESET_ALL)
print(Fore.RED + "WARNING: Cargo reinstallation is not possible at the moment."
"\n -> https://github.com/rust-lang/cargo/issues/5593" + Style.RESET_ALL)

print_section_header("SUCCESSFUL PACKAGE REINSTALLATION", Fore.BLUE)
sys.exit()
Expand Down Expand Up @@ -511,7 +503,7 @@ def create_gitignore_if_needed(dir_path):
def git_init_if_needed(dir_path):
"""
If there is no git repo inside the dir_path, intialize one.
Returns a Repo object
Returns git.Repo object
"""
if not os.path.isdir(os.path.join(dir_path, ".git")):
print(Fore.GREEN + Style.BRIGHT + "Initializing new git repo..." + Style.RESET_ALL)
Expand Down Expand Up @@ -552,7 +544,8 @@ def git_push_if_possible(repo):
"""
if "origin" in [remote.name for remote in repo.remotes]:
origin = repo.remotes.origin
print(Fore.GREEN + Style.BRIGHT + "Pushing to master at " + Fore.RED+ "{}...".format(origin.url) + Style.RESET_ALL)
print(Fore.GREEN + Style.BRIGHT + "Pushing to master at " + Fore.RED + "{}...".format(
origin.url) + Style.RESET_ALL)
repo.heads.master.set_tracking_branch(origin.refs.master)
origin.pull()
origin.push(refspec='master:master')
Expand Down Expand Up @@ -591,7 +584,7 @@ def get_default_config():
"""
return {
"backup_path": "~/shallow-backup",
"dotfiles" : [
"dotfiles": [
".bashrc",
".bash_profile",
".gitconfig",
Expand All @@ -601,11 +594,11 @@ def get_default_config():
".vimrc",
".zshrc"
],
"dotfolders" : [
"dotfolders": [
".ssh",
".vim"
],
"gitignore" : [
"gitignore": [
"dotfiles/.ssh",
"packages/",
"dotfiles/.pypirc",
Expand All @@ -624,10 +617,9 @@ def create_config_file_if_needed():
write_config(backup_config)



#######
#####
# CLI
#######
#####

def move_git_folder_to_path(source_path, new_path):
"""
Expand All @@ -650,18 +642,12 @@ def prompt_for_path_update(config):
If yes, update. If no... don't.
"""
current_path = config["backup_path"]
print(
Fore.BLUE + Style.BRIGHT + "Current shallow-backup path -> " + Style.NORMAL + "{}".format(
current_path) + Style.RESET_ALL)
print(Fore.BLUE + Style.BRIGHT + "Current shallow-backup path -> " + Style.NORMAL + "{}".format(current_path) + Style.RESET_ALL)

if prompt_yes_no("Would you like to update this?", Fore.GREEN):
print(Fore.GREEN + Style.BRIGHT +
"Enter relative path:" + Style.RESET_ALL)

print(Fore.GREEN + Style.BRIGHT + "Enter relative path:" + Style.RESET_ALL)
abs_path = os.path.abspath(input())

print(Fore.BLUE + "\nUpdating shallow-backup path to {}".format(
abs_path) + Style.RESET_ALL)
print(Fore.BLUE + "\nUpdating shallow-backup path to {}".format(abs_path) + Style.RESET_ALL)
config["backup_path"] = abs_path
write_config(config)
make_dir_warn_overwrite(abs_path)
Expand Down Expand Up @@ -729,26 +715,21 @@ def cli(complete, dotfiles, configs, packages, fonts, old_path, new_path, remote
elif delete_config:
os.remove(backup_config_path)
print(Fore.RED + Style.BRIGHT +
"Removed config file..." + Style.RESET_ALL)
"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()

# Start CLI
splash_screen()

#####
# Update backup path from CLI args, prompt user, or skip updating
#####

create_config_file_if_needed()
backup_config = get_config()

# User entered a new path, so update the config
if new_path != "":
abs_path = os.path.abspath(new_path)

print(Fore.BLUE + Style.NORMAL + "\nUpdating shallow-backup path to -> " + Style.BRIGHT + "{}".format(
abs_path) + Style.RESET_ALL)
backup_config["backup_path"] = abs_path
Expand All @@ -758,11 +739,7 @@ def cli(complete, dotfiles, configs, packages, fonts, old_path, new_path, remote
elif not (old_path or complete or dotfiles or packages or fonts):
prompt_for_path_update(backup_config)


###
# Create backup directory and set up git stuff
###

# Create backup directory and do git setup
backup_home_path = get_config()["backup_path"]
make_dir_warn_overwrite(backup_home_path)
repo = git_init_if_needed(backup_home_path)
Expand Down