Upgrade Click and make use of shell detection

This introduces an additional environment variable "PIPENV_SHELL". If
set, shell detection is skipped completely. The old "SHELL"
introspection is kept as a fallback if detection fails.
This commit is contained in:
Tzu-ping Chung
2018-06-15 17:40:40 +08:00
parent 1a00484f16
commit adcb4974ee
3 changed files with 35 additions and 14 deletions
+10 -13
View File
@@ -14,9 +14,9 @@ from click import (
version_option,
BadParameter,
)
from click_completion import init as init_completion
from click_completion import get_code
from click_didyoumean import DYMCommandCollection
import click_completion
import crayons
import delegator
@@ -26,7 +26,7 @@ from . import environments
from .utils import is_valid_url
# Enable shell completion.
init_completion()
click_completion.init()
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@@ -165,20 +165,17 @@ def cli(
completion=False,
):
if completion: # Handle this ASAP to make shell startup fast.
if environments.PIPENV_SHELL:
from . import shells
try:
shell = shells.detect_info()[0]
except shells.ShellDetectionFailure:
echo(
get_code(
shell=environments.PIPENV_SHELL.split(os.sep)[-1],
prog_name='pipenv'
)
)
else:
echo(
'Please ensure that the {0} environment variable '
'is set.'.format(crayons.normal('SHELL', bold=True)),
'Fail to detect shell. Please provide the {0} environment '
'variable.'.format(crayons.normal('PIPENV_SHELL', bold=True)),
err=True,
)
sys.exit(1)
print(click_completion.get_code(shell=shell, prog_name='pipenv'))
sys.exit(0)
from .core import (
+2 -1
View File
@@ -74,7 +74,8 @@ PYENV_INSTALLED = (
bool(os.environ.get('PYENV_SHELL')) or bool(os.environ.get('PYENV_ROOT'))
)
SESSION_IS_INTERACTIVE = bool(os.isatty(sys.stdout.fileno()))
PIPENV_SHELL_EXPLICIT = os.environ.get('PIPENV_SHELL')
PIPENV_SHELL = os.environ.get('SHELL') or os.environ.get('PYENV_SHELL')
PIPENV_CACHE_DIR = os.environ.get('PIPENV_CACHE_DIR', user_cache_dir('pipenv'))
# Tells pipenv to override PyPI index urls with a mirror.
PIPENV_PYPI_MIRROR = os.environ.get('PIPENV_PYPI_MIRROR')
PIPENV_PYPI_MIRROR = os.environ.get('PIPENV_PYPI_MIRROR')
+23
View File
@@ -0,0 +1,23 @@
import os
from .environments import PIPENV_SHELL_EXPLICIT, PIPENV_SHELL
from .vendor import shellingham
class ShellDetectionFailure(shellingham.ShellDetectionFailure):
pass
def _build_info(value):
return (os.path.splitext(os.path.basename(value))[0], value)
def detect_info():
if PIPENV_SHELL_EXPLICIT:
return _build_info(PIPENV_SHELL_EXPLICIT)
try:
return shellingham.detect_shell()
except (shellingham.ShellDetectionFailure, TypeError):
if PIPENV_SHELL:
return _build_info(PIPENV_SHELL)
raise ShellDetectionFailure