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.
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
import os
|
|
import dotbot
|
|
|
|
|
|
class Create(dotbot.Plugin):
|
|
'''
|
|
Create empty paths.
|
|
'''
|
|
|
|
_directive = 'create'
|
|
|
|
def can_handle(self, directive):
|
|
return directive == self._directive
|
|
|
|
def handle(self, directive, data):
|
|
if directive != self._directive:
|
|
raise ValueError('Create cannot handle directive %s' % directive)
|
|
return self._process_paths(data)
|
|
|
|
def _process_paths(self, paths):
|
|
success = True
|
|
for path in paths:
|
|
path = os.path.expandvars(os.path.expanduser(path))
|
|
success &= self._create(path)
|
|
if success:
|
|
self._log.info('All paths have been set up')
|
|
else:
|
|
self._log.error('Some paths were not successfully set up')
|
|
return success
|
|
|
|
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
|
|
if not self._exists(path):
|
|
self._log.debug('Trying to create path %s' % path)
|
|
try:
|
|
self._log.lowinfo('Creating path %s' % path)
|
|
os.makedirs(path)
|
|
except OSError:
|
|
self._log.warning('Failed to create path %s' % path)
|
|
success = False
|
|
else:
|
|
self._log.lowinfo('Path exists %s' % path)
|
|
return success
|