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 os
|
2022-04-26 08:04:07 -04:00
|
|
|
import sys
|
|
|
|
|
2022-04-30 21:19:22 -04:00
|
|
|
from ..plugin import Plugin
|
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
|
|
|
|
2014-06-08 14:30:10 -04:00
|
|
|
|
2022-04-30 21:19:22 -04:00
|
|
|
class Clean(Plugin):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-06-08 14:30:10 -04:00
|
|
|
Cleans broken symbolic links.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-06-08 14:30:10 -04:00
|
|
|
|
2022-01-30 18:48:30 -05:00
|
|
|
_directive = "clean"
|
2014-06-08 14:30:10 -04:00
|
|
|
|
|
|
|
def can_handle(self, directive):
|
|
|
|
return directive == self._directive
|
|
|
|
|
|
|
|
def handle(self, directive, data):
|
|
|
|
if directive != self._directive:
|
2022-01-30 18:48:30 -05:00
|
|
|
raise ValueError("Clean cannot handle directive %s" % directive)
|
2014-06-08 14:30:10 -04:00
|
|
|
return self._process_clean(data)
|
|
|
|
|
|
|
|
def _process_clean(self, targets):
|
|
|
|
success = True
|
2017-03-22 06:22:10 -04:00
|
|
|
defaults = self._context.defaults().get(self._directive, {})
|
2014-06-08 14:30:10 -04:00
|
|
|
for target in targets:
|
2022-01-30 18:48:30 -05:00
|
|
|
force = defaults.get("force", False)
|
|
|
|
recursive = defaults.get("recursive", False)
|
2019-12-31 14:47:32 -05:00
|
|
|
if isinstance(targets, dict) and isinstance(targets[target], dict):
|
2022-01-30 18:48:30 -05:00
|
|
|
force = targets[target].get("force", force)
|
|
|
|
recursive = targets[target].get("recursive", recursive)
|
2019-12-31 19:14:23 -05:00
|
|
|
success &= self._clean(target, force, recursive)
|
2014-06-08 14:30:10 -04:00
|
|
|
if success:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.info("All targets have been cleaned")
|
2014-06-08 14:30:10 -04:00
|
|
|
else:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.error("Some targets were not successfully cleaned")
|
2014-06-08 14:30:10 -04:00
|
|
|
return success
|
|
|
|
|
2019-12-31 19:14:23 -05:00
|
|
|
def _clean(self, target, force, recursive):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2017-03-22 06:22:10 -04:00
|
|
|
Cleans all the broken symbolic links in target if they point to
|
|
|
|
a subdirectory of the base directory or if forced to clean.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2018-08-06 15:12:44 -04:00
|
|
|
if not os.path.isdir(os.path.expandvars(os.path.expanduser(target))):
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.debug("Ignoring nonexistent directory %s" % target)
|
2015-01-26 10:38:08 -05:00
|
|
|
return True
|
2018-08-06 15:12:44 -04:00
|
|
|
for item in os.listdir(os.path.expandvars(os.path.expanduser(target))):
|
2022-04-30 21:42:36 -04:00
|
|
|
path = os.path.abspath(
|
|
|
|
os.path.join(os.path.expandvars(os.path.expanduser(target)), item)
|
|
|
|
)
|
2019-12-31 19:14:23 -05:00
|
|
|
if recursive and os.path.isdir(path):
|
|
|
|
# isdir implies not islink -- we don't want to descend into
|
|
|
|
# symlinked directories. okay to do a recursive call here
|
|
|
|
# because depth should be fairly limited
|
|
|
|
self._clean(path, force, recursive)
|
2014-06-08 14:30:10 -04:00
|
|
|
if not os.path.exists(path) and os.path.islink(path):
|
2017-03-22 06:22:10 -04:00
|
|
|
points_at = os.path.join(os.path.dirname(path), os.readlink(path))
|
2022-04-26 08:04:07 -04:00
|
|
|
if sys.platform[:5] == "win32" and points_at.startswith("\\\\?\\"):
|
|
|
|
points_at = points_at[4:]
|
2017-03-22 06:22:10 -04:00
|
|
|
if self._in_directory(path, self._context.base_directory()) or force:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.lowinfo("Removing invalid link %s -> %s" % (path, points_at))
|
2014-06-08 14:30:10 -04:00
|
|
|
os.remove(path)
|
2017-03-22 06:22:10 -04:00
|
|
|
else:
|
2022-01-30 18:48:30 -05:00
|
|
|
self._log.lowinfo("Link %s -> %s not removed." % (path, points_at))
|
2014-06-08 14:30:10 -04:00
|
|
|
return True
|
|
|
|
|
|
|
|
def _in_directory(self, path, directory):
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
2014-06-08 14:30:10 -04:00
|
|
|
Returns true if the path is in the directory.
|
2022-01-30 18:48:30 -05:00
|
|
|
"""
|
|
|
|
directory = os.path.join(os.path.realpath(directory), "")
|
2014-06-08 14:30:10 -04:00
|
|
|
path = os.path.realpath(path)
|
|
|
|
return os.path.commonprefix([path, directory]) == directory
|