Skip to content

Commit

Permalink
Refactor reinstallation code
Browse files Browse the repository at this point in the history
  • Loading branch information
alichtman committed Jan 27, 2020
1 parent 6bdf4d4 commit 6784673
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
31 changes: 15 additions & 16 deletions shallow_backup/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,27 @@
# conflict with the function names.


def reinstall_dots_sb(dots_path,home_path=os.path.expanduser("~")):
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)
parent = Path(dots_path)

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:
son=Path(os.path.dirname(file))
destination=""
if parent in son.parents:
folderLevel = son.relative_to(parent)
destination = os.path.join(home_path,folderLevel)
if not os.path.exists(os.path.join(home_path,folderLevel)):
os.makedirs(os.path.join(home_path,folderLevel))
else:
destination = home_path
copy(file,destination)
destination = home_path
copy(file, destination)
print_section_header("DOTFILE REINSTALLATION COMPLETED", Fore.BLUE)


Expand Down Expand Up @@ -126,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
32 changes: 20 additions & 12 deletions tests/test_reinstall_dotfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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
Expand All @@ -32,20 +33,26 @@ def setup_method():
os.mkdir(DOTFILES_PATH)

# SAMPLE SUBFOLDER IN DOTFILES PATH
print(os.path.join(DOTFILES_PATH, "testfolder/"))
try:
os.mkdir(os.path.join(DOTFILES_PATH, "testfolder/"))
except FileExistsError:
shutil.rmtree(os.path.join(DOTFILES_PATH, "testfolder/"))
os.mkdir(os.path.join(DOTFILES_PATH, "testfolder/"))
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(DOTFILES_PATH, ".testrc")
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(DOTFILES_PATH, "testfolder/.testsubfolder_rc")
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)
Expand All @@ -59,8 +66,9 @@ def test_reinstall_dotfiles(self):
"""
Test resintalling dotfiles to fake home dir
"""
reinstall_dots_sb(DOTFILES_PATH,home_path=FAKE_HOME_DIR)
reinstall_dots_sb(DOTFILES_PATH, home_path=FAKE_HOME_DIR)
assert os.path.isfile(os.path.join(FAKE_HOME_DIR, '.testrc'))
print(os.path.join(FAKE_HOME_DIR, 'testfolder/'))
assert os.path.isdir(os.path.join(FAKE_HOME_DIR, 'testfolder/'))
assert os.path.isfile(os.path.join(FAKE_HOME_DIR, 'testfolder/.testsubfolder_rc'))
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 6784673

Please sign in to comment.