f5e019105e
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:1def7754b7/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 in7593d8c134
. 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.
242 lines
10 KiB
Python
242 lines
10 KiB
Python
import os
|
|
import glob
|
|
import shutil
|
|
import dotbot
|
|
import dotbot.util
|
|
import subprocess
|
|
|
|
|
|
class Link(dotbot.Plugin):
|
|
'''
|
|
Symbolically links dotfiles.
|
|
'''
|
|
|
|
_directive = 'link'
|
|
|
|
def can_handle(self, directive):
|
|
return directive == self._directive
|
|
|
|
def handle(self, directive, data):
|
|
if directive != self._directive:
|
|
raise ValueError('Link cannot handle directive %s' % directive)
|
|
return self._process_links(data)
|
|
|
|
def _process_links(self, links):
|
|
success = True
|
|
defaults = self._context.defaults().get('link', {})
|
|
for destination, source in links.items():
|
|
destination = os.path.expandvars(destination)
|
|
relative = defaults.get('relative', False)
|
|
canonical_path = 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)
|
|
test = defaults.get('if', None)
|
|
ignore_missing = defaults.get('ignore-missing', False)
|
|
if isinstance(source, dict):
|
|
# extended config
|
|
test = source.get('if', test)
|
|
relative = source.get('relative', relative)
|
|
canonical_path = 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)
|
|
ignore_missing = source.get('ignore-missing', ignore_missing)
|
|
path = self._default_source(destination, source.get('path'))
|
|
else:
|
|
path = self._default_source(destination, source)
|
|
if test is not None and not self._test_success(test):
|
|
self._log.lowinfo('Skipping %s' % destination)
|
|
continue
|
|
path = os.path.expandvars(os.path.expanduser(path))
|
|
if use_glob:
|
|
self._log.debug("Globbing with path: " + str(path))
|
|
glob_results = glob.glob(path)
|
|
if len(glob_results) == 0:
|
|
self._log.warning("Globbing couldn't find anything matching " + str(path))
|
|
success = False
|
|
continue
|
|
glob_star_loc = path.find('*')
|
|
if glob_star_loc == -1 and destination[-1] == '/':
|
|
self._log.error("Ambiguous action requested.")
|
|
self._log.error("No wildcard in glob, directory use undefined: " +
|
|
destination + " -> " + str(glob_results))
|
|
self._log.warning("Did you want to link the directory or into it?")
|
|
success = False
|
|
continue
|
|
elif glob_star_loc == -1 and len(glob_results) == 1:
|
|
# perform a normal link operation
|
|
if create:
|
|
success &= self._create(destination)
|
|
if force or relink:
|
|
success &= self._delete(path, destination, relative, canonical_path, force)
|
|
success &= self._link(path, destination, relative, canonical_path, ignore_missing)
|
|
else:
|
|
self._log.lowinfo("Globs from '" + path + "': " + str(glob_results))
|
|
glob_base = path[:glob_star_loc]
|
|
for glob_full_item in glob_results:
|
|
glob_item = glob_full_item[len(glob_base):]
|
|
glob_link_destination = os.path.join(destination, glob_item)
|
|
if create:
|
|
success &= self._create(glob_link_destination)
|
|
if force or relink:
|
|
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)
|
|
else:
|
|
if create:
|
|
success &= self._create(destination)
|
|
if not ignore_missing and not self._exists(os.path.join(self._context.base_directory(), path)):
|
|
# 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)
|
|
success = False
|
|
self._log.warning('Nonexistent source %s -> %s' %
|
|
(destination, path))
|
|
continue
|
|
if force or relink:
|
|
success &= self._delete(path, destination, relative, canonical_path, force)
|
|
success &= self._link(path, destination, relative, canonical_path, ignore_missing)
|
|
if success:
|
|
self._log.info('All links have been set up')
|
|
else:
|
|
self._log.error('Some links were not successfully set up')
|
|
return success
|
|
|
|
def _test_success(self, command):
|
|
ret = dotbot.util.shell_command(command, cwd=self._context.base_directory())
|
|
if ret != 0:
|
|
self._log.debug('Test \'%s\' returned false' % command)
|
|
return ret == 0
|
|
|
|
def _default_source(self, destination, source):
|
|
if source is None:
|
|
basename = os.path.basename(destination)
|
|
if basename.startswith('.'):
|
|
return basename[1:]
|
|
else:
|
|
return basename
|
|
else:
|
|
return source
|
|
|
|
def _is_link(self, path):
|
|
'''
|
|
Returns true if the path is a symbolic link.
|
|
'''
|
|
return os.path.islink(os.path.expanduser(path))
|
|
|
|
def _link_destination(self, path):
|
|
'''
|
|
Returns the destination of the symbolic link.
|
|
'''
|
|
path = os.path.expanduser(path)
|
|
return os.readlink(path)
|
|
|
|
def _exists(self, path):
|
|
'''
|
|
Returns true if the path exists.
|
|
'''
|
|
path = os.path.expanduser(path)
|
|
return os.path.exists(path)
|
|
|
|
def _create(self, path):
|
|
success = True
|
|
parent = os.path.abspath(os.path.join(os.path.expanduser(path), os.pardir))
|
|
if not self._exists(parent):
|
|
self._log.debug("Try to create parent: " + str(parent))
|
|
try:
|
|
os.makedirs(parent)
|
|
except OSError:
|
|
self._log.warning('Failed to create directory %s' % parent)
|
|
success = False
|
|
else:
|
|
self._log.lowinfo('Creating directory %s' % parent)
|
|
return success
|
|
|
|
def _delete(self, source, path, relative, canonical_path, force):
|
|
success = True
|
|
source = os.path.join(self._context.base_directory(canonical_path=canonical_path), source)
|
|
fullpath = os.path.expanduser(path)
|
|
if relative:
|
|
source = self._relative_path(source, fullpath)
|
|
if ((self._is_link(path) and self._link_destination(path) != source) or
|
|
(self._exists(path) and not self._is_link(path))):
|
|
removed = False
|
|
try:
|
|
if os.path.islink(fullpath):
|
|
os.unlink(fullpath)
|
|
removed = True
|
|
elif force:
|
|
if os.path.isdir(fullpath):
|
|
shutil.rmtree(fullpath)
|
|
removed = True
|
|
else:
|
|
os.remove(fullpath)
|
|
removed = True
|
|
except OSError:
|
|
self._log.warning('Failed to remove %s' % path)
|
|
success = False
|
|
else:
|
|
if removed:
|
|
self._log.lowinfo('Removing %s' % path)
|
|
return success
|
|
|
|
def _relative_path(self, source, destination):
|
|
'''
|
|
Returns the relative path to get to the source file from the
|
|
destination file.
|
|
'''
|
|
destination_dir = os.path.dirname(destination)
|
|
return os.path.relpath(source, destination_dir)
|
|
|
|
def _link(self, source, link_name, relative, canonical_path, ignore_missing):
|
|
'''
|
|
Links link_name to source.
|
|
|
|
Returns true if successfully linked files.
|
|
'''
|
|
success = False
|
|
destination = os.path.expanduser(link_name)
|
|
base_directory = self._context.base_directory(canonical_path=canonical_path)
|
|
absolute_source = os.path.join(base_directory, source)
|
|
if relative:
|
|
source = self._relative_path(absolute_source, destination)
|
|
else:
|
|
source = absolute_source
|
|
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)))
|
|
# 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
|
|
elif not self._exists(link_name) and (ignore_missing or self._exists(absolute_source)):
|
|
try:
|
|
os.symlink(source, destination)
|
|
except OSError:
|
|
self._log.warning('Linking failed %s -> %s' % (link_name, source))
|
|
else:
|
|
self._log.lowinfo('Creating link %s -> %s' % (link_name, source))
|
|
success = True
|
|
elif self._exists(link_name) and not self._is_link(link_name):
|
|
self._log.warning(
|
|
'%s already exists but is a regular file or directory' %
|
|
link_name)
|
|
elif self._is_link(link_name) and self._link_destination(link_name) != source:
|
|
self._log.warning('Incorrect link %s -> %s' %
|
|
(link_name, self._link_destination(link_name)))
|
|
# again, we use absolute_source to check for existence
|
|
elif not self._exists(absolute_source):
|
|
if self._is_link(link_name):
|
|
self._log.warning('Nonexistent source %s -> %s' %
|
|
(link_name, source))
|
|
else:
|
|
self._log.warning('Nonexistent source for %s : %s' %
|
|
(link_name, source))
|
|
else:
|
|
self._log.lowinfo('Link exists %s -> %s' % (link_name, source))
|
|
success = True
|
|
return success
|