2016-11-19 10:42:02 -05:00
|
|
|
import os
|
2021-05-27 12:58:55 -04:00
|
|
|
import sys
|
2018-01-27 02:27:44 -05:00
|
|
|
import glob
|
2016-11-19 10:42:02 -05:00
|
|
|
import shutil
|
|
|
|
import dotbot
|
Work around subprocess.call() issue on Windows
On POSIX-like systems, calling `subprocess.call()` with both
`shell=True` and `executable='...'` has the following behavior:
> If `shell=True`, on POSIX the _executable_ argument specifies a
> replacement shell for the default `/bin/sh`.
(via https://docs.python.org/3/library/subprocess.html?highlight=subprocess#popen-constructor)
This seems to have a similar behavior on Windows, but this is
problematic when a POSIX shell is substituted for cmd.exe. This is
because when `shell=True`, the shell is invoked with a '/c' argument,
which is the correct argument for cmd.exe but not for Bash, which
expects a '-c' argument instead. See here:
https://github.com/python/cpython/blob/1def7754b7a41fe57efafaf5eff24cfa15353444/Lib/subprocess.py#L1407
This is problematic when combined with Dotbot's behavior, where the
`executable` argument is set based on `$SHELL`. For example, when
running in Git Bash, the `$SHELL` environment variable is set to Bash,
so any commands run by Dotbot will fail (because it'll invoke Bash with
a '/c' argument).
This behavior of setting the `executable` argument based on `$SHELL` was
introduced in 7593d8c13479b382357be065c7bf51562a130660. This is the
desired behavior. See discussion in
https://github.com/anishathalye/dotbot/issues/97 and
https://github.com/anishathalye/dotbot/pull/100.
Unfortunately, this doesn't work quite right on Windows. This patch
works around the issue by avoiding setting the `executable` argument
when the platform is Windows, which is tested using
`platform.system() == 'Windows'`. This means that shell commands
executed by Dotbot on this platform will always be run using cmd.exe.
Invocations of single programs or simple commands will probably work
just fine in cmd.exe. If Bash-like behavior is desired, the user will
have to write their command as `bash -c '...'`.
This shouldn't have any implications for backwards-compatibility,
because setting the `executable` argument on Windows didn't do the right
thing anyways. Previous workarounds that users had should continue to
work with the new code.
When using Python from CYGWIN, `platform.system()` returns something
like 'CYGWIN_NT-...', so it won't be detected with the check, but this
is the correct behavior, because CYGWIN Python's `subprocess.call()` has
the POSIX-like behavior.
This patch also refactors the code to factor out the
`subprocess.call()`, which was being called in both `link.py` and
`shell.py`, so the workaround can be applied in a single place.
See the following issues/pull requests for a discussion of this bug:
- https://github.com/anishathalye/dotbot/issues/170
- https://github.com/anishathalye/dotbot/pull/177
- https://github.com/anishathalye/dotbot/issues/219
An issue has also been raised in Python's issue tracker:
- https://bugs.python.org/issue40467
Thanks to @shivapoudel for originally reporting the issue, @SuJiKiNen
for debugging it and submitting a pull request, and @mohkale for
suggesting factoring out the code so that other plugins could use it.
2020-05-01 11:52:51 -04:00
|
|
|
import dotbot.util
|
2018-02-23 16:56:19 -05:00
|
|
|
import subprocess
|
2016-11-19 10:42:02 -05:00
|
|
|
|
2014-03-19 23:07:30 -04:00
|
|
|
|
2016-01-16 22:00:15 -05:00
|
|
|
class Link(dotbot.Plugin):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-03-19 23:07:30 -04:00
|
|
|
Symbolically links dotfiles.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-03-19 23:07:30 -04:00
|
|
|
|
2022-01-30 18:48:30 -05:00
|
|
|
_directive = "link"
|
2014-04-24 15:41:34 -04:00
|
|
|
|
2014-03-19 23:07:30 -04:00
|
|
|
def can_handle(self, directive):
|
2014-04-24 15:41:34 -04:00
|
|
|
return directive == self._directive
|
2014-03-19 23:07:30 -04:00
|
|
|
|
|
|
|
def handle(self, directive, data):
|
2014-04-24 15:41:34 -04:00
|
|
|
if directive != self._directive:
|
2022-01-30 18:48:30 -05:00
|
|
|
raise ValueError("Link cannot handle directive %s" % directive)
|
2014-03-19 23:07:30 -04:00
|
|
|
return self._process_links(data)
|
|
|
|
|
|
|
|
def _process_links(self, links):
|
|
|
|
success = True
|
2022-01-30 18:48:30 -05:00
|
|
|
defaults = self._context.defaults().get("link", {})
|
2014-03-19 23:07:30 -04:00
|
|
|
for destination, source in links.items():
|
2015-05-12 20:04:42 -04:00
|
|
|
destination = os.path.expandvars(destination)
|
2022-01-30 18:48:30 -05:00
|
|
|
relative = defaults.get("relative", False)
|
2021-02-25 08:26:19 -05:00
|
|
|
# support old "canonicalize-path" key for compatibility
|
2022-01-30 18:48:30 -05:00
|
|
|
canonical_path = defaults.get("canonicalize", defaults.get("canonicalize-path", True))
|
|
|
|
force = defaults.get("force", False)
|
|
|
|
relink = defaults.get("relink", False)
|
|
|
|
create = defaults.get("create", False)
|
|
|
|
use_glob = defaults.get("glob", False)
|
|
|
|
base_prefix = defaults.get("prefix", "")
|
|
|
|
test = defaults.get("if", None)
|
|
|
|
ignore_missing = defaults.get("ignore-missing", False)
|
|
|
|
exclude_paths = defaults.get("exclude", [])
|
2014-10-22 13:27:34 -04:00
|
|
|
if isinstance(source, dict):
|
|
|
|
# extended config
|
2022-01-30 18:48:30 -05:00
|
|
|
test = source.get("if", test)
|
|
|
|
relative = source.get("relative", relative)
|
|
|
|
canonical_path = source.get(
|
|
|
|
"canonicalize", source.get("canonicalize-path", canonical_path)
|
|
|
|
)
|
|
|
|
force = source.get("force", force)
|
|
|
|
relink = source.get("relink", relink)
|
|
|
|
create = source.get("create", create)
|
|
|
|
use_glob = source.get("glob", use_glob)
|
|
|
|
base_prefix = source.get("prefix", base_prefix)
|
|
|
|
ignore_missing = source.get("ignore-missing", ignore_missing)
|
|
|
|
exclude_paths = source.get("exclude", exclude_paths)
|
|
|
|
path = self._default_source(destination, source.get("path"))
|
2014-10-22 13:27:34 -04:00
|
|
|
else:
|
2016-11-18 18:21:27 -05:00
|
|
|
path = self._default_source(destination, source)
|
2018-11-17 16:00:18 -05:00
|
|
|
if test is not None and not self._test_success(test):
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.lowinfo("Skipping %s" % destination)
|
2018-11-17 16:00:18 -05:00
|
|
|
continue
|
2016-08-17 21:15:02 -04:00
|
|
|
path = os.path.expandvars(os.path.expanduser(path))
|
2018-01-27 02:27:44 -05:00
|
|
|
if use_glob:
|
2020-11-22 12:59:30 -05:00
|
|
|
glob_results = self._create_glob_results(path, exclude_paths)
|
2020-01-03 14:42:45 -05:00
|
|
|
if len(glob_results) == 0:
|
2018-01-27 02:27:44 -05:00
|
|
|
self._log.warning("Globbing couldn't find anything matching " + str(path))
|
|
|
|
success = False
|
|
|
|
continue
|
2022-01-30 18:48:30 -05:00
|
|
|
if len(glob_results) == 1 and destination[-1] == "/":
|
2018-01-27 02:27:44 -05:00
|
|
|
self._log.error("Ambiguous action requested.")
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.error(
|
|
|
|
"No wildcard in glob, directory use undefined: "
|
|
|
|
+ destination
|
|
|
|
+ " -> "
|
|
|
|
+ str(glob_results)
|
|
|
|
)
|
2018-01-27 02:27:44 -05:00
|
|
|
self._log.warning("Did you want to link the directory or into it?")
|
|
|
|
success = False
|
|
|
|
continue
|
2022-01-30 18:48:30 -05:00
|
|
|
elif len(glob_results) == 1 and destination[-1] != "/":
|
2018-01-27 02:27:44 -05:00
|
|
|
# perform a normal link operation
|
|
|
|
if create:
|
|
|
|
success &= self._create(destination)
|
|
|
|
if force or relink:
|
2018-05-21 13:10:17 -04:00
|
|
|
success &= self._delete(path, destination, relative, canonical_path, force)
|
2022-01-30 18:48:30 -05:00
|
|
|
success &= self._link(
|
|
|
|
path, destination, relative, canonical_path, ignore_missing
|
|
|
|
)
|
2018-01-27 02:27:44 -05:00
|
|
|
else:
|
2018-01-27 04:11:11 -05:00
|
|
|
self._log.lowinfo("Globs from '" + path + "': " + str(glob_results))
|
2018-01-27 02:27:44 -05:00
|
|
|
for glob_full_item in glob_results:
|
2021-04-16 00:28:19 -04:00
|
|
|
# Find common dirname between pattern and the item:
|
|
|
|
glob_dirname = os.path.dirname(os.path.commonprefix([path, glob_full_item]))
|
2022-01-30 18:48:30 -05:00
|
|
|
glob_item = (
|
|
|
|
glob_full_item
|
|
|
|
if len(glob_dirname) == 0
|
|
|
|
else glob_full_item[len(glob_dirname) + 1 :]
|
|
|
|
)
|
2021-05-27 13:07:17 -04:00
|
|
|
# Add prefix to basepath, if provided
|
|
|
|
if base_prefix:
|
2021-06-02 20:23:55 -04:00
|
|
|
glob_item = base_prefix + glob_item
|
2021-04-16 00:28:19 -04:00
|
|
|
# where is it going
|
2018-04-06 12:56:16 -04:00
|
|
|
glob_link_destination = os.path.join(destination, glob_item)
|
2018-01-27 02:27:44 -05:00
|
|
|
if create:
|
|
|
|
success &= self._create(glob_link_destination)
|
2018-01-27 04:11:11 -05:00
|
|
|
if force or relink:
|
2022-01-30 18:48:30 -05:00
|
|
|
success &= self._delete(
|
|
|
|
glob_full_item,
|
|
|
|
glob_link_destination,
|
|
|
|
relative,
|
|
|
|
canonical_path,
|
|
|
|
force,
|
|
|
|
)
|
|
|
|
success &= self._link(
|
|
|
|
glob_full_item,
|
|
|
|
glob_link_destination,
|
|
|
|
relative,
|
|
|
|
canonical_path,
|
|
|
|
ignore_missing,
|
|
|
|
)
|
2018-01-27 02:27:44 -05:00
|
|
|
else:
|
|
|
|
if create:
|
|
|
|
success &= self._create(destination)
|
2022-01-30 18:48:30 -05:00
|
|
|
if not ignore_missing and not self._exists(
|
|
|
|
os.path.join(self._context.base_directory(), path)
|
|
|
|
):
|
2018-05-25 16:04:04 -04:00
|
|
|
# we seemingly check this twice (here and in _link) because
|
|
|
|
# if the file doesn't exist and force is True, we don't
|
|
|
|
# want to remove the original (this is tested by
|
|
|
|
# link-force-leaves-when-nonexistent.bash)
|
2018-01-27 02:27:44 -05:00
|
|
|
success = False
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.warning("Nonexistent source %s -> %s" % (destination, path))
|
2018-01-27 02:27:44 -05:00
|
|
|
continue
|
|
|
|
if force or relink:
|
2018-05-21 13:10:17 -04:00
|
|
|
success &= self._delete(path, destination, relative, canonical_path, force)
|
|
|
|
success &= self._link(path, destination, relative, canonical_path, ignore_missing)
|
2014-03-19 23:07:30 -04:00
|
|
|
if success:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.info("All links have been set up")
|
2014-03-19 23:07:30 -04:00
|
|
|
else:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.error("Some links were not successfully set up")
|
2014-03-19 23:07:30 -04:00
|
|
|
return success
|
|
|
|
|
2018-02-23 16:56:19 -05:00
|
|
|
def _test_success(self, command):
|
Work around subprocess.call() issue on Windows
On POSIX-like systems, calling `subprocess.call()` with both
`shell=True` and `executable='...'` has the following behavior:
> If `shell=True`, on POSIX the _executable_ argument specifies a
> replacement shell for the default `/bin/sh`.
(via https://docs.python.org/3/library/subprocess.html?highlight=subprocess#popen-constructor)
This seems to have a similar behavior on Windows, but this is
problematic when a POSIX shell is substituted for cmd.exe. This is
because when `shell=True`, the shell is invoked with a '/c' argument,
which is the correct argument for cmd.exe but not for Bash, which
expects a '-c' argument instead. See here:
https://github.com/python/cpython/blob/1def7754b7a41fe57efafaf5eff24cfa15353444/Lib/subprocess.py#L1407
This is problematic when combined with Dotbot's behavior, where the
`executable` argument is set based on `$SHELL`. For example, when
running in Git Bash, the `$SHELL` environment variable is set to Bash,
so any commands run by Dotbot will fail (because it'll invoke Bash with
a '/c' argument).
This behavior of setting the `executable` argument based on `$SHELL` was
introduced in 7593d8c13479b382357be065c7bf51562a130660. This is the
desired behavior. See discussion in
https://github.com/anishathalye/dotbot/issues/97 and
https://github.com/anishathalye/dotbot/pull/100.
Unfortunately, this doesn't work quite right on Windows. This patch
works around the issue by avoiding setting the `executable` argument
when the platform is Windows, which is tested using
`platform.system() == 'Windows'`. This means that shell commands
executed by Dotbot on this platform will always be run using cmd.exe.
Invocations of single programs or simple commands will probably work
just fine in cmd.exe. If Bash-like behavior is desired, the user will
have to write their command as `bash -c '...'`.
This shouldn't have any implications for backwards-compatibility,
because setting the `executable` argument on Windows didn't do the right
thing anyways. Previous workarounds that users had should continue to
work with the new code.
When using Python from CYGWIN, `platform.system()` returns something
like 'CYGWIN_NT-...', so it won't be detected with the check, but this
is the correct behavior, because CYGWIN Python's `subprocess.call()` has
the POSIX-like behavior.
This patch also refactors the code to factor out the
`subprocess.call()`, which was being called in both `link.py` and
`shell.py`, so the workaround can be applied in a single place.
See the following issues/pull requests for a discussion of this bug:
- https://github.com/anishathalye/dotbot/issues/170
- https://github.com/anishathalye/dotbot/pull/177
- https://github.com/anishathalye/dotbot/issues/219
An issue has also been raised in Python's issue tracker:
- https://bugs.python.org/issue40467
Thanks to @shivapoudel for originally reporting the issue, @SuJiKiNen
for debugging it and submitting a pull request, and @mohkale for
suggesting factoring out the code so that other plugins could use it.
2020-05-01 11:52:51 -04:00
|
|
|
ret = dotbot.util.shell_command(command, cwd=self._context.base_directory())
|
2018-02-23 16:56:19 -05:00
|
|
|
if ret != 0:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.debug("Test '%s' returned false" % command)
|
2018-02-23 16:56:19 -05:00
|
|
|
return ret == 0
|
|
|
|
|
2016-11-18 18:21:27 -05:00
|
|
|
def _default_source(self, destination, source):
|
|
|
|
if source is None:
|
|
|
|
basename = os.path.basename(destination)
|
2022-01-30 18:48:30 -05:00
|
|
|
if basename.startswith("."):
|
2016-11-18 18:21:27 -05:00
|
|
|
return basename[1:]
|
|
|
|
else:
|
|
|
|
return basename
|
|
|
|
else:
|
|
|
|
return source
|
2021-02-25 08:26:19 -05:00
|
|
|
|
2021-05-27 12:58:55 -04:00
|
|
|
def _glob(self, path):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2021-05-27 12:58:55 -04:00
|
|
|
Wrap `glob.glob` in a python agnostic way, catching errors in usage.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
|
|
|
if sys.version_info < (3, 5) and "**" in path:
|
|
|
|
self._log.error(
|
|
|
|
'Link cannot handle recursive glob ("**") for Python < version 3.5: "%s"' % path
|
|
|
|
)
|
2021-05-27 12:58:55 -04:00
|
|
|
return []
|
|
|
|
# call glob.glob; only python >= 3.5 supports recursive globs
|
2022-01-30 18:48:30 -05:00
|
|
|
found = glob.glob(path) if (sys.version_info < (3, 5)) else glob.glob(path, recursive=True)
|
2021-05-27 12:58:55 -04:00
|
|
|
# if using recursive glob (`**`), filter results to return only files:
|
2022-01-30 18:48:30 -05:00
|
|
|
if "**" in path and not path.endswith(str(os.sep)):
|
|
|
|
self._log.debug("Excluding directories from recursive glob: " + str(path))
|
|
|
|
found = [f for f in found if os.path.isfile(f)]
|
2021-05-27 12:58:55 -04:00
|
|
|
# return matched results
|
|
|
|
return found
|
|
|
|
|
2020-11-22 12:59:30 -05:00
|
|
|
def _create_glob_results(self, path, exclude_paths):
|
2021-05-27 12:58:55 -04:00
|
|
|
self._log.debug("Globbing with pattern: " + str(path))
|
|
|
|
include = self._glob(path)
|
|
|
|
self._log.debug("Glob found : " + str(include))
|
|
|
|
# filter out any paths matching the exclude globs:
|
|
|
|
exclude = []
|
|
|
|
for expat in exclude_paths:
|
|
|
|
self._log.debug("Excluding globs with pattern: " + str(expat))
|
2022-01-30 18:48:30 -05:00
|
|
|
exclude.extend(self._glob(expat))
|
2021-05-27 12:58:55 -04:00
|
|
|
self._log.debug("Excluded globs from '" + path + "': " + str(exclude))
|
|
|
|
ret = set(include) - set(exclude)
|
2020-11-22 12:59:30 -05:00
|
|
|
return list(ret)
|
2016-11-18 18:21:27 -05:00
|
|
|
|
2014-03-19 23:07:30 -04:00
|
|
|
def _is_link(self, path):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-03-19 23:07:30 -04:00
|
|
|
Returns true if the path is a symbolic link.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-03-19 23:07:30 -04:00
|
|
|
return os.path.islink(os.path.expanduser(path))
|
|
|
|
|
|
|
|
def _link_destination(self, path):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2016-04-05 18:04:42 -04:00
|
|
|
Returns the destination of the symbolic link.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-03-19 23:07:30 -04:00
|
|
|
path = os.path.expanduser(path)
|
2016-04-05 18:04:42 -04:00
|
|
|
return os.readlink(path)
|
2014-03-19 23:07:30 -04:00
|
|
|
|
|
|
|
def _exists(self, path):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-03-19 23:07:30 -04:00
|
|
|
Returns true if the path exists.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-03-19 23:07:30 -04:00
|
|
|
path = os.path.expanduser(path)
|
|
|
|
return os.path.exists(path)
|
|
|
|
|
2014-10-22 14:44:40 -04:00
|
|
|
def _create(self, path):
|
|
|
|
success = True
|
|
|
|
parent = os.path.abspath(os.path.join(os.path.expanduser(path), os.pardir))
|
|
|
|
if not self._exists(parent):
|
2018-01-27 02:27:44 -05:00
|
|
|
self._log.debug("Try to create parent: " + str(parent))
|
2014-10-22 14:44:40 -04:00
|
|
|
try:
|
|
|
|
os.makedirs(parent)
|
|
|
|
except OSError:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.warning("Failed to create directory %s" % parent)
|
2014-10-22 14:44:40 -04:00
|
|
|
success = False
|
|
|
|
else:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.lowinfo("Creating directory %s" % parent)
|
2014-10-22 14:44:40 -04:00
|
|
|
return success
|
|
|
|
|
2018-05-21 13:10:17 -04:00
|
|
|
def _delete(self, source, path, relative, canonical_path, force):
|
2014-10-22 13:27:34 -04:00
|
|
|
success = True
|
2018-05-21 13:10:17 -04:00
|
|
|
source = os.path.join(self._context.base_directory(canonical_path=canonical_path), source)
|
2016-04-05 18:04:42 -04:00
|
|
|
fullpath = os.path.expanduser(path)
|
|
|
|
if relative:
|
|
|
|
source = self._relative_path(source, fullpath)
|
2022-01-30 18:48:30 -05:00
|
|
|
if (self._is_link(path) and self._link_destination(path) != source) or (
|
|
|
|
self._exists(path) and not self._is_link(path)
|
|
|
|
):
|
2015-05-02 22:27:05 -04:00
|
|
|
removed = False
|
2014-10-22 13:27:34 -04:00
|
|
|
try:
|
2015-04-27 12:30:23 -04:00
|
|
|
if os.path.islink(fullpath):
|
|
|
|
os.unlink(fullpath)
|
2015-05-02 22:27:05 -04:00
|
|
|
removed = True
|
|
|
|
elif force:
|
|
|
|
if os.path.isdir(fullpath):
|
|
|
|
shutil.rmtree(fullpath)
|
|
|
|
removed = True
|
|
|
|
else:
|
|
|
|
os.remove(fullpath)
|
|
|
|
removed = True
|
2014-10-22 13:27:34 -04:00
|
|
|
except OSError:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.warning("Failed to remove %s" % path)
|
2014-10-22 13:27:34 -04:00
|
|
|
success = False
|
|
|
|
else:
|
2015-05-02 22:27:05 -04:00
|
|
|
if removed:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.lowinfo("Removing %s" % path)
|
2014-10-22 13:27:34 -04:00
|
|
|
return success
|
|
|
|
|
2016-04-05 18:04:42 -04:00
|
|
|
def _relative_path(self, source, destination):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2016-04-05 18:04:42 -04:00
|
|
|
Returns the relative path to get to the source file from the
|
|
|
|
destination file.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2016-04-05 18:04:42 -04:00
|
|
|
destination_dir = os.path.dirname(destination)
|
|
|
|
return os.path.relpath(source, destination_dir)
|
|
|
|
|
2018-05-21 13:10:17 -04:00
|
|
|
def _link(self, source, link_name, relative, canonical_path, ignore_missing):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-03-19 23:07:30 -04:00
|
|
|
Links link_name to source.
|
|
|
|
|
|
|
|
Returns true if successfully linked files.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-03-19 23:07:30 -04:00
|
|
|
success = False
|
2016-04-05 18:04:42 -04:00
|
|
|
destination = os.path.expanduser(link_name)
|
2018-05-21 13:10:17 -04:00
|
|
|
base_directory = self._context.base_directory(canonical_path=canonical_path)
|
|
|
|
absolute_source = os.path.join(base_directory, source)
|
2016-04-05 18:04:42 -04:00
|
|
|
if relative:
|
|
|
|
source = self._relative_path(absolute_source, destination)
|
|
|
|
else:
|
|
|
|
source = absolute_source
|
2022-01-30 18:48:30 -05:00
|
|
|
if (
|
|
|
|
not self._exists(link_name)
|
|
|
|
and self._is_link(link_name)
|
|
|
|
and self._link_destination(link_name) != source
|
|
|
|
):
|
|
|
|
self._log.warning(
|
|
|
|
"Invalid link %s -> %s" % (link_name, self._link_destination(link_name))
|
|
|
|
)
|
2016-04-05 18:04:42 -04:00
|
|
|
# we need to use absolute_source below because our cwd is the dotfiles
|
|
|
|
# directory, and if source is relative, it will be relative to the
|
|
|
|
# destination directory
|
2018-05-25 16:04:04 -04:00
|
|
|
elif not self._exists(link_name) and (ignore_missing or self._exists(absolute_source)):
|
2014-07-17 13:02:53 -04:00
|
|
|
try:
|
2016-02-14 22:39:48 -05:00
|
|
|
os.symlink(source, destination)
|
2014-08-20 16:58:42 -04:00
|
|
|
except OSError:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.warning("Linking failed %s -> %s" % (link_name, source))
|
2014-07-17 13:02:53 -04:00
|
|
|
else:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.lowinfo("Creating link %s -> %s" % (link_name, source))
|
2014-07-17 13:02:53 -04:00
|
|
|
success = True
|
2014-03-19 23:07:30 -04:00
|
|
|
elif self._exists(link_name) and not self._is_link(link_name):
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.warning("%s already exists but is a regular file or directory" % link_name)
|
2014-07-15 21:25:53 -04:00
|
|
|
elif self._is_link(link_name) and self._link_destination(link_name) != source:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.warning(
|
|
|
|
"Incorrect link %s -> %s" % (link_name, self._link_destination(link_name))
|
|
|
|
)
|
2016-04-05 18:04:42 -04:00
|
|
|
# again, we use absolute_source to check for existence
|
|
|
|
elif not self._exists(absolute_source):
|
2014-07-15 21:25:53 -04:00
|
|
|
if self._is_link(link_name):
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.warning("Nonexistent source %s -> %s" % (link_name, source))
|
2014-07-15 21:25:53 -04:00
|
|
|
else:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.warning("Nonexistent source for %s : %s" % (link_name, source))
|
2014-03-19 23:07:30 -04:00
|
|
|
else:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.lowinfo("Link exists %s -> %s" % (link_name, source))
|
2014-03-19 23:07:30 -04:00
|
|
|
success = True
|
|
|
|
return success
|