Skip to content
This repository has been archived by the owner on Apr 21, 2021. It is now read-only.

Commit

Permalink
Validate absolute python paths
Browse files Browse the repository at this point in the history
This makes it easier for someone to realize when they have provided an
invalid Python path without missing that message due to the sizable
virtualenv traceback which follows it. I first encountered this when
someone copy-and-pasted the example from the Conda documentation which
has `--python=/path/to/anaconda/python`.

See pypa#1862
  • Loading branch information
acdha committed Mar 28, 2018
1 parent b0445a2 commit a07e0d6
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pipenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ def setup_verbose(ctx, param, value):
return value


def validate_python_path(ctx, param, value):
# Validating the Python path is complicated by accepting a number of
# friendly options: the default will be boolean False to enable
# autodetection but it may also be a value which will be searched in
# the path or an absolute path. To report errors as early as possible
# we'll report absolute paths which do not exist:
if isinstance(value, (str, bytes)):
if os.path.isabs(value) and not os.path.isfile(value):
raise click.BadParameter('Expected Python at path %s does not exist' % value)
return value


@click.group(
cls=PipenvGroup,
invoke_without_command=True,
Expand Down Expand Up @@ -106,6 +118,7 @@ def setup_verbose(ctx, param, value):
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@click.option(
Expand Down Expand Up @@ -263,6 +276,7 @@ def cli(
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@click.option(
Expand Down Expand Up @@ -382,6 +396,7 @@ def install(
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@click.option(
Expand Down Expand Up @@ -453,6 +468,7 @@ def uninstall(
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@click.option(
Expand Down Expand Up @@ -527,6 +543,7 @@ def lock(
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@click.option(
Expand Down Expand Up @@ -594,6 +611,7 @@ def shell(
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
def run(command, args, three=None, python=False):
Expand All @@ -616,6 +634,7 @@ def run(command, args, three=None, python=False):
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@click.option(
Expand Down Expand Up @@ -655,6 +674,7 @@ def check(
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@click.option(
Expand Down Expand Up @@ -819,6 +839,7 @@ def graph(bare=False, json=False, reverse=False):
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@click.argument('module', nargs=1)
Expand Down Expand Up @@ -874,6 +895,7 @@ def run_open(module, three=None, python=None):
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@click.option('--bare', is_flag=True, default=False, help="Minimal output.")
Expand Down Expand Up @@ -940,6 +962,7 @@ def sync(
'--python',
default=False,
nargs=1,
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@click.option(
Expand Down

0 comments on commit a07e0d6

Please sign in to comment.