Skip to content

Commit

Permalink
Merge 6784673 into 2edb60a
Browse files Browse the repository at this point in the history
  • Loading branch information
alichtman committed Jan 27, 2020
2 parents 2edb60a + 6784673 commit ab36c61
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 9 deletions.
25 changes: 17 additions & 8 deletions shallow_backup/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,34 @@
from .printing import *
from .compatibility import *
from .config import get_config
from shutil import copytree, copyfile
from pathlib import Path
from shutil import copytree, copyfile, copy

# NOTE: Naming convention is like this since the CLI flags would otherwise
# conflict with the function names.


def reinstall_dots_sb(dots_path):
def reinstall_dots_sb(dots_path, home_path=os.path.expanduser("~")):
"""
Reinstall all dotfiles and folders by copying them to the home dir.
"""
empty_backup_dir_check(dots_path, 'dotfile')
print_section_header("REINSTALLING DOTFILES", Fore.BLUE)

home_path = os.path.expanduser('~')
dotfiles_source = Path(dots_path)

# Create intermediate directories if needed, and then copy file.
for file in get_abs_path_subfiles(dots_path):
if os.path.isdir(file):
copytree(file, home_path, symlinks=True)
parent_dir_of_dotfile = Path(os.path.dirname(file))

if dotfiles_source in parent_dir_of_dotfile.parents:
missing_dirs = parent_dir_of_dotfile.relative_to(dotfiles_source)
destination = os.path.join(home_path, missing_dirs)
if not os.path.exists(destination):
os.makedirs(destination)
else:
copyfile(file, home_path)
destination = home_path
copy(file, destination)
print_section_header("DOTFILE REINSTALLATION COMPLETED", Fore.BLUE)


Expand Down Expand Up @@ -116,8 +125,8 @@ def reinstall_packages_sb(packages_path):
elif pm == "gem":
print_red_bold("WARNING: Gem reinstallation is not supported.")
elif pm == "cargo":
print_red_bold("WARNING: Cargo reinstallation is not possible at the moment."
"\n -> https://github.com/rust-lang/cargo/issues/5593")
print_red_bold("WARNING: Cargo reinstallation is not possible at the moment.\
\n -> https://github.com/rust-lang/cargo/issues/5593")

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

Expand Down
2 changes: 1 addition & 1 deletion shallow_backup/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def destroy_backup_dir(backup_path):

def get_abs_path_subfiles(directory):
"""
Returns list of absolute paths of immediate files and folders in a directory.
Returns list of absolute paths of files and folders contained in a directory.
"""
file_paths = []
for path, subdirs, files in os.walk(directory):
Expand Down
74 changes: 74 additions & 0 deletions tests/test_reinstall_dotfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import sys
import shutil
from .test_utils import FAKE_HOME_DIR, DIRS, setup_env_vars, create_config_for_test
sys.path.insert(0, "../shallow_backup")
from shallow_backup.reinstall import reinstall_dots_sb

TEST_TEXT_CONTENT = 'THIS IS TEST CONTENT FOR THE DOTFILES'
DOTFILES_PATH = os.path.join(FAKE_HOME_DIR, "dotfiles/")


class TestReinstallDotfiles:
"""
Test the functionality of reinstalling dotfiles
"""

@staticmethod
def setup_method():
setup_env_vars()
create_config_for_test()
for directory in DIRS:
try:
os.mkdir(directory)
except FileExistsError:
shutil.rmtree(directory)
os.mkdir(directory)

# SAMPLE DOTFILES FOLDER PATH
try:
os.mkdir(DOTFILES_PATH)
except FileExistsError:
shutil.rmtree(DOTFILES_PATH)
os.mkdir(DOTFILES_PATH)

# SAMPLE SUBFOLDER IN DOTFILES PATH
testfolder = os.path.join(DOTFILES_PATH, "testfolder1/")
print(f"Creating {testfolder}")
os.mkdir(testfolder)

testfolder2 = os.path.join(testfolder, "testfolder2/")
print(f"Creating {testfolder2}")
os.mkdir(testfolder2)

# SAMPLE DOTFILES TO REINSTALL
file = os.path.join(testfolder2, ".testsubfolder_rc1")
print(f"Creating {file}")
with open(file, "w+") as f:
f.write(TEST_TEXT_CONTENT)

file = os.path.join(testfolder2, ".testsubfolder_rc2")
print(f"Creating {file}")
with open(file, "w+") as f:
f.write(TEST_TEXT_CONTENT)

file = os.path.join(DOTFILES_PATH, ".testrc")
print(f"Creating {file}")
with open(file, "w+") as f:
f.write(TEST_TEXT_CONTENT)

@staticmethod
def teardown_method():
for directory in DIRS:
shutil.rmtree(directory)

def test_reinstall_dotfiles(self):
"""
Test resintalling dotfiles to fake home dir
"""
reinstall_dots_sb(DOTFILES_PATH, home_path=FAKE_HOME_DIR)
assert os.path.isfile(os.path.join(FAKE_HOME_DIR, '.testrc'))
testfolder2 = os.path.join(os.path.join(FAKE_HOME_DIR, 'testfolder1'), 'testfolder2')
assert os.path.isdir(testfolder2)
assert os.path.isfile(os.path.join(testfolder2, '.testsubfolder_rc1'))
assert os.path.isfile(os.path.join(testfolder2, '.testsubfolder_rc2'))

0 comments on commit ab36c61

Please sign in to comment.