Skip to content

Commit 319c0fe

Browse files
authored
Merge pull request #119 from alichtman/refactoring
Clean up
2 parents 0da0e6a + ffb5e79 commit 319c0fe

1 file changed

Lines changed: 26 additions & 49 deletions

File tree

shallow_backup.py

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
def print_version_info():
2323
version = "{} v{} by {} -> (Github: {})".format(Constants.PROJECT_NAME,
24-
Constants.VERSION,
25-
Constants.AUTHOR_FULL_NAME,
26-
Constants.AUTHOR_GITHUB)
24+
Constants.VERSION,
25+
Constants.AUTHOR_FULL_NAME,
26+
Constants.AUTHOR_GITHUB)
2727
line = "-" * (len(version))
2828
print(Fore.RED + Style.BRIGHT + line)
2929
print(version)
@@ -51,8 +51,6 @@ def print_section_header(title, COLOR):
5151
def prompt_yes_no(message, color):
5252
"""
5353
Print question and return True or False depending on user selection from list.
54-
bottom_line should be used for one liners.
55-
Otherwise, it's the second line you want printed.
5654
"""
5755
questions = [inquirer.List('choice',
5856
message=color + Style.BRIGHT + message + Fore.BLUE,
@@ -67,7 +65,6 @@ def prompt_yes_no(message, color):
6765
# Utilities
6866
###########
6967

70-
7168
def run_shell_cmd(command):
7269
"""
7370
Wrapper on subprocess.run that handles both lists and strings as commands.
@@ -79,7 +76,7 @@ def run_shell_cmd(command):
7976
else:
8077
process = sp.run(command, stdout=sp.PIPE)
8178
return process
82-
except FileNotFoundError: # If package manager is missing
79+
except FileNotFoundError: # If package manager is missing
8380
return None
8481

8582

@@ -101,7 +98,7 @@ def make_dir_warn_overwrite(path):
10198
subdirs = ["dotfiles", "packages", "fonts", "configs"]
10299
if os.path.exists(path) and path.split("/")[-1] in subdirs:
103100
print(Fore.RED + Style.BRIGHT +
104-
"Directory {} already exists".format(path) + "\n" + Style.RESET_ALL)
101+
"Directory {} already exists".format(path) + "\n" + Style.RESET_ALL)
105102
if prompt_yes_no("Erase directory and make new back up?", Fore.RED):
106103
shutil.rmtree(path)
107104
os.makedirs(path)
@@ -161,10 +158,10 @@ def get_configs_path_mapping():
161158
return {
162159
"Library/Application Support/Sublime Text 2/Packages/User/": "sublime_2",
163160
"Library/Application Support/Sublime Text 3/Packages/User/": "sublime_3",
164-
"Library/Preferences/IntelliJIdea2018.2/":"intellijidea_2018.2",
165-
"Library/Preferences/PyCharm2018.2/":"pycharm_2018.2",
166-
"Library/Preferences/CLion2018.2/":"clion_2018.2",
167-
"Library/Preferences/PhpStorm2018.2":"phpstorm_2018.2",
161+
"Library/Preferences/IntelliJIdea2018.2/": "intellijidea_2018.2",
162+
"Library/Preferences/PyCharm2018.2/": "pycharm_2018.2",
163+
"Library/Preferences/CLion2018.2/": "clion_2018.2",
164+
"Library/Preferences/PhpStorm2018.2": "phpstorm_2018.2",
168165
}
169166

170167

@@ -204,15 +201,11 @@ def backup_dotfiles(backup_path):
204201
dotfolders_mp_in.append(
205202
(os.path.join(home_path, dotfolder), backup_path))
206203

207-
208204
dotfiles_mp_in = []
209205
for dotfile in dotfiles:
210206
dotfiles_mp_in.append((os.path.join(home_path, dotfile), os.path.join(backup_path, dotfile)))
211207

212-
####
213208
# Back up System and Application Preferences and Settings
214-
####
215-
216209
# TODO: Extract these paths to constants
217210

218211
# Sublime Text Configs
@@ -450,11 +443,10 @@ def reinstall_package(packages_path):
450443
elif pm == "macports":
451444
print(Fore.RED + "WARNING: Macports reinstallation is not supported." + Style.RESET_ALL)
452445
elif pm == "gem":
453-
print(
454-
Fore.RED + "WARNING: Gem reinstallation is not supported." + Style.RESET_ALL)
446+
print(Fore.RED + "WARNING: Gem reinstallation is not supported." + Style.RESET_ALL)
455447
elif pm == "cargo":
456-
print(Fore.RED + "WARNING: Cargo reinstallation is not possible at the moment. "
457-
"\n -> https://github.com/rust-lang/cargo/issues/5593" + Style.RESET_ALL)
448+
print(Fore.RED + "WARNING: Cargo reinstallation is not possible at the moment."
449+
"\n -> https://github.com/rust-lang/cargo/issues/5593" + Style.RESET_ALL)
458450

459451
print_section_header("SUCCESSFUL PACKAGE REINSTALLATION", Fore.BLUE)
460452
sys.exit()
@@ -511,7 +503,7 @@ def create_gitignore_if_needed(dir_path):
511503
def git_init_if_needed(dir_path):
512504
"""
513505
If there is no git repo inside the dir_path, intialize one.
514-
Returns a Repo object
506+
Returns git.Repo object
515507
"""
516508
if not os.path.isdir(os.path.join(dir_path, ".git")):
517509
print(Fore.GREEN + Style.BRIGHT + "Initializing new git repo..." + Style.RESET_ALL)
@@ -552,7 +544,8 @@ def git_push_if_possible(repo):
552544
"""
553545
if "origin" in [remote.name for remote in repo.remotes]:
554546
origin = repo.remotes.origin
555-
print(Fore.GREEN + Style.BRIGHT + "Pushing to master at " + Fore.RED+ "{}...".format(origin.url) + Style.RESET_ALL)
547+
print(Fore.GREEN + Style.BRIGHT + "Pushing to master at " + Fore.RED + "{}...".format(
548+
origin.url) + Style.RESET_ALL)
556549
repo.heads.master.set_tracking_branch(origin.refs.master)
557550
origin.pull()
558551
origin.push(refspec='master:master')
@@ -591,7 +584,7 @@ def get_default_config():
591584
"""
592585
return {
593586
"backup_path": "~/shallow-backup",
594-
"dotfiles" : [
587+
"dotfiles": [
595588
".bashrc",
596589
".bash_profile",
597590
".gitconfig",
@@ -601,11 +594,11 @@ def get_default_config():
601594
".vimrc",
602595
".zshrc"
603596
],
604-
"dotfolders" : [
597+
"dotfolders": [
605598
".ssh",
606599
".vim"
607600
],
608-
"gitignore" : [
601+
"gitignore": [
609602
"dotfiles/.ssh",
610603
"packages/",
611604
"dotfiles/.pypirc",
@@ -624,10 +617,9 @@ def create_config_file_if_needed():
624617
write_config(backup_config)
625618

626619

627-
628-
#######
620+
#####
629621
# CLI
630-
#######
622+
#####
631623

632624
def move_git_folder_to_path(source_path, new_path):
633625
"""
@@ -650,18 +642,12 @@ def prompt_for_path_update(config):
650642
If yes, update. If no... don't.
651643
"""
652644
current_path = config["backup_path"]
653-
print(
654-
Fore.BLUE + Style.BRIGHT + "Current shallow-backup path -> " + Style.NORMAL + "{}".format(
655-
current_path) + Style.RESET_ALL)
645+
print(Fore.BLUE + Style.BRIGHT + "Current shallow-backup path -> " + Style.NORMAL + "{}".format(current_path) + Style.RESET_ALL)
656646

657647
if prompt_yes_no("Would you like to update this?", Fore.GREEN):
658-
print(Fore.GREEN + Style.BRIGHT +
659-
"Enter relative path:" + Style.RESET_ALL)
660-
648+
print(Fore.GREEN + Style.BRIGHT + "Enter relative path:" + Style.RESET_ALL)
661649
abs_path = os.path.abspath(input())
662-
663-
print(Fore.BLUE + "\nUpdating shallow-backup path to {}".format(
664-
abs_path) + Style.RESET_ALL)
650+
print(Fore.BLUE + "\nUpdating shallow-backup path to {}".format(abs_path) + Style.RESET_ALL)
665651
config["backup_path"] = abs_path
666652
write_config(config)
667653
make_dir_warn_overwrite(abs_path)
@@ -729,26 +715,21 @@ def cli(complete, dotfiles, configs, packages, fonts, old_path, new_path, remote
729715
elif delete_config:
730716
os.remove(backup_config_path)
731717
print(Fore.RED + Style.BRIGHT +
732-
"Removed config file..." + Style.RESET_ALL)
718+
"Removed config file..." + Style.RESET_ALL)
733719
sys.exit()
734720
elif destroy_backup:
735721
backup_home_path = get_config()["backup_path"]
736722
destroy_backup_dir(backup_home_path)
737723
sys.exit()
738724

725+
# Start CLI
739726
splash_screen()
740-
741-
#####
742-
# Update backup path from CLI args, prompt user, or skip updating
743-
#####
744-
745727
create_config_file_if_needed()
746728
backup_config = get_config()
747729

748730
# User entered a new path, so update the config
749731
if new_path != "":
750732
abs_path = os.path.abspath(new_path)
751-
752733
print(Fore.BLUE + Style.NORMAL + "\nUpdating shallow-backup path to -> " + Style.BRIGHT + "{}".format(
753734
abs_path) + Style.RESET_ALL)
754735
backup_config["backup_path"] = abs_path
@@ -758,11 +739,7 @@ def cli(complete, dotfiles, configs, packages, fonts, old_path, new_path, remote
758739
elif not (old_path or complete or dotfiles or packages or fonts):
759740
prompt_for_path_update(backup_config)
760741

761-
762-
###
763-
# Create backup directory and set up git stuff
764-
###
765-
742+
# Create backup directory and do git setup
766743
backup_home_path = get_config()["backup_path"]
767744
make_dir_warn_overwrite(backup_home_path)
768745
repo = git_init_if_needed(backup_home_path)

0 commit comments

Comments
 (0)