Fix crash when bash is not found on Windows. Also, look up GitForWindows (e.g. MSYS bash) if bash is not on PATH.

This commit is contained in:
Morten Enemark Lund 2016-06-06 14:13:01 +02:00
parent 6f8f82646e
commit 3f8f8f9981
2 changed files with 44 additions and 10 deletions

View file

@ -34,6 +34,12 @@ for ((i=0;i<${{#COMPREPLY[*]}};i++)) do echo ${{COMPREPLY[i]}}; done
"""
if ON_WINDOWS:
from xonsh.platform import WINDOWS_BASH_COMMAND as BASH_COMMAND
else:
BASH_COMMAND = 'bash'
def update_bash_completion():
global BASH_COMPLETE_FUNCS, BASH_COMPLETE_FILES, BASH_COMPLETE_HASH
global CACHED_FUNCS, CACHED_FILES, CACHED_HASH, INITED
@ -108,9 +114,9 @@ def complete_from_bash(prefix, line, begidx, endidx, ctx):
end=endidx + 1, prefix=prefix, prev=shlex.quote(prev))
try:
out = subprocess.check_output(
['bash'], input=script, universal_newlines=True,
[BASH_COMMAND], input=script, universal_newlines=True,
stderr=subprocess.PIPE, env=builtins.__xonsh_env__.detype())
except subprocess.CalledProcessError:
except (subprocess.CalledProcessError, FileNotFoundError):
out = ''
rtn = set(out.splitlines())
@ -161,11 +167,15 @@ def _load_bash_complete_files():
def _source_completions(source):
return subprocess.check_output(
['bash'], input='\n'.join(source), universal_newlines=True,
try:
import pdb; pdb.set_trace()
return subprocess.check_output(
[BASH_COMMAND], input='\n'.join(source), universal_newlines=True,
env=builtins.__xonsh_env__.detype(), stderr=subprocess.DEVNULL)
except FileNotFoundError:
return ''
def _collect_completions_sources():
sources = []
completers = builtins.__xonsh_env__.get('BASH_COMPLETIONS', ())

View file

@ -188,7 +188,31 @@ if ON_WINDOWS:
else:
win_unicode_console = None
if ON_WINDOWS:
import winreg
try:
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\GitForWindows')
GIT_FOR_WINDOWS_PATH, type = winreg.QueryValueEx(key, "InstallPath")
except FileNotFoundError:
GIT_FOR_WINDOWS_PATH = None
if ON_WINDOWS:
# Check that bash in on path otherwise try the default bin directory
# used by Git for windows
import subprocess
WINDOWS_BASH_COMMAND = 'bash'
try:
subprocess.check_call([WINDOWS_BASH_COMMAND,'--version'])
except FileNotFoundError:
if GIT_FOR_WINDOWS_PATH:
bashcmd = os.path.join(GIT_FOR_WINDOWS_PATH, 'bin\\bash.exe')
if os.path.isfile(bashcmd):
WINDOWS_BASH_COMMAND = bashcmd
#
# Bash completions defaults
#
@ -210,13 +234,13 @@ elif ON_DARWIN:
BASH_COMPLETIONS_DEFAULT = (
'/usr/local/etc/bash_completion',
'/opt/local/etc/profile.d/bash_completion.sh')
elif ON_WINDOWS:
progamfiles = os.environ.get('PROGRAMFILES', 'C:/Program Files')
elif ON_WINDOWS and GIT_FOR_WINDOWS_PATH:
BASH_COMPLETIONS_DEFAULT = (
progamfiles + '/Git/usr/share/bash-completion',
progamfiles + '/Git/usr/share/bash-completion/completions',
progamfiles + '/Git/mingw64/share/git/completion/git-completion.bash')
os.path.join(GIT_FOR_WINDOWS_PATH, 'usr\\share\\bash-completion'),
os.path.join(GIT_FOR_WINDOWS_PATH, 'usr\\share\\bash-completion\\completions'),
os.path.join(GIT_FOR_WINDOWS_PATH, 'mingw64\\share\\git\\completion\\git-completion.bash'))
#
# All constants as a dict
#