diff --git a/dotbot/plugins/link.py b/dotbot/plugins/link.py index 4a9cc53..7f9ba9f 100644 --- a/dotbot/plugins/link.py +++ b/dotbot/plugins/link.py @@ -1,6 +1,8 @@ import os import glob import shutil +from pathlib import Path + import dotbot import dotbot.util import subprocess @@ -253,8 +255,9 @@ class Link(dotbot.Plugin): def _link(self, dotfile_source, target_path_to_link_at, relative_path, canonical_path, ignore_missing): ''' Links link_name to source. - :param target_path_to_link_at is the file path where we are putting a symlink back to - dotfile_source + :param target_path_to_link_at is the file path where we are putting a symlink + (where the file originally lived) + :param dotfile_source - source file in dotfiles directory, which file should symlink to Returns true if successfully linked files. ''' @@ -276,29 +279,39 @@ class Link(dotbot.Plugin): target_path_exists: bool = self._exists(target_path_to_link_at) target_file_is_link: bool = self._is_link(target_path_to_link_at) + # get the file/ folder the symlink (located at the target path) is pointed to symlink_dest_at_target_path: str = self._get_link_destination(target_path_to_link_at) + # Expanded, os style paths for reporting/ error checking + symlink_loc_clean = os.path.normpath(os.path.expanduser(target_path_to_link_at)) + dotfile_source_expanded = os.path.expanduser(dotfile_source) + # Check case of links are present but incorrect if target_file_is_link and (symlink_dest_at_target_path != dotfile_source): + symlink_dest_clean = os.path.abspath(symlink_dest_at_target_path) if target_path_exists: - self._log.warning("Incorrect link (link exists but target is incorrect) %s -> %s" - % (target_path_to_link_at, symlink_dest_at_target_path)) + self._log.warning("Incorrect link (link exists but target is incorrect):\n\t " + f"{symlink_loc_clean} -> {symlink_dest_clean},\n\t" + f"Expected {symlink_dest_clean}, found " + f"{dotfile_source_expanded}" + ) print("Link found:", symlink_dest_at_target_path, "expected", dotfile_source) else: # Symlink is broken or dangling - self._log.warning("Symlink Invalid %s -> %s" % (target_path_to_link_at, symlink_dest_at_target_path)) + self._log.warning(f"Symlink Invalid:\n\t {symlink_loc_clean}" + f"\n\t -> {symlink_dest_clean}") return success_flag if target_path_exists: # file/ folder we want to put symlink in already exists if target_file_is_link: # already checked if link pointed to wrong location, # so if it's a link we know it's correct - self._log.lowinfo("Link exists %s -> %s" % (target_path_to_link_at, dotfile_source)) + self._log.lowinfo("Link exists %s -> %s" % (symlink_loc_clean, dotfile_source_expanded)) success_flag = True return success_flag else: # Not a link self._log.warning( - "%s already exists but is a regular file or directory" % target_path_to_link_at) + "%s already exists but is a regular file or directory" % symlink_loc_clean) return success_flag else: # target path doesn't exist already, so we try to create the symlink @@ -307,7 +320,8 @@ class Link(dotbot.Plugin): os.symlink(dotfile_source, destination) except OSError as e: msg = textwrap.fill( - f"Linking failed {target_path_to_link_at} -> {dotfile_source}\n ({e})", width=80, subsequent_indent=" ") + f"Linking failed {symlink_loc_clean} -> {dotfile_source_expanded}\n ({e})", + width=80, subsequent_indent=" ") self._log.warning(msg) except Exception as e: @@ -316,7 +330,7 @@ class Link(dotbot.Plugin): ) raise e else: - self._log.lowinfo("Creating link %s -> %s" % (target_path_to_link_at, dotfile_source)) + self._log.lowinfo("Creating link %s -> %s" % (symlink_loc_clean, dotfile_source_expanded)) success_flag = True return success_flag