Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the environment scripts directory to the list of places to search for subcommands #162

Merged
merged 2 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion jupyter_core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import json
import os
import sys
import sysconfig
from subprocess import Popen

try:
Expand Down Expand Up @@ -148,12 +149,28 @@ def _path_with_self():
will do /path/to/jupyter-subcommand
even if /other/jupyter-subcommand is ahead of it on PATH
"""
path_list = (os.environ.get('PATH') or os.defpath).split(os.pathsep)

# Insert the "scripts" directory for this Python installation
# This allows the "jupyter" command to be relocated, while still
# finding subcommands that have been installed in the default
# location.
# We put the scripts directory at the *end* of PATH, so that
# if the user explicitly overrides a subcommand, that override
# still takes effect.
try:
bindir = sysconfig.get_path('scripts')
except KeyError:
# The Python environment does not specify a "scripts" location
pass
else:
path_list.append(bindir)

scripts = [sys.argv[0]]
if os.path.islink(scripts[0]):
# include realpath, if `jupyter` is a symlink
scripts.append(os.path.realpath(scripts[0]))

path_list = (os.environ.get('PATH') or os.defpath).split(os.pathsep)
for script in scripts:
bindir = os.path.dirname(script)
if (
Expand Down
27 changes: 19 additions & 8 deletions jupyter_core/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import os
import sys
import sysconfig
from subprocess import check_output, CalledProcessError

import pytest
Expand Down Expand Up @@ -108,17 +109,27 @@ def test_subcommand_list(tmpdir):
'jupyterstuff',
'jupyter-yo-eyropa-ganymyde-callysto'):
b.join(cmd).write('')
c = tmpdir.mkdir("c")
for cmd in ('jupyter-baz',
'jupyter-bop'):
c.join(cmd).write('')

path = os.pathsep.join(map(str, [a, b]))

def get_path(dummy):
return str(c)

with patch.dict('os.environ', {'PATH': path}):
subcommands = list_subcommands()
assert subcommands == [
'babel-fish',
'foo',
'xyz',
'yo-eyropa-ganymyde-callysto',
]
with patch.object(sysconfig, 'get_path', get_path):
with patch.dict('os.environ', {'PATH': path}):
subcommands = list_subcommands()
assert subcommands == [
'babel-fish',
'baz',
'bop',
'foo',
'xyz',
'yo-eyropa-ganymyde-callysto',
]

def test_not_on_path(tmpdir):
a = tmpdir.mkdir("a")
Expand Down