Skip to content

Commit

Permalink
Applied changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
xmnlab committed Jun 11, 2019
1 parent dc9ae4b commit 7fe4c25
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions jupyter_core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import sys
from subprocess import Popen

from distutils.spawn import find_executable
from .utils.shutil_which import which

from . import paths
from .version import __version__
Expand Down Expand Up @@ -115,19 +115,24 @@ def _execvp(cmd, argv):
os.execvp(cmd, argv)


def _jupyter_realpath(path, subcommand):
# get currrent PATH
_path_env = os.environ['PATH']
# set new PATH with self
os.environ['PATH'] = os.pathsep.join(_path_with_self())
# get the real path for the jupyter-<subcommand>
real_path = find_executable('{}-{}'.format(path, subcommand))
# RESTORE previous PATH
os.environ['PATH'] = _path_env
return (
real_path if real_path is not None and os.access(real_path, os.X_OK)
else None
)
def _jupyter_abspath(path, subcommand):
"""This method get the abspath of jupyter with no changes on ENV."""
# get env PATH with self
search_path = os.pathsep.join(_path_with_self())
# get the abs path for the jupyter-<subcommand>
jupyter_subcommand = '{}-{}'.format(path, subcommand)
abs_path = which(jupyter_subcommand, path=search_path)
if abs_path is None:
raise Exception(
'Jupyter command `{}` not found.'.format(jupyter_subcommand)
)

if not os.access(abs_path, os.X_OK):
raise Exception(
'Jupyter command `{}` is not executable.'.format(jupyter_subcommand)
)

return abs_path


def _path_with_self():
Expand Down Expand Up @@ -216,7 +221,7 @@ def main():
parser.print_usage(file=sys.stderr)
sys.exit("subcommand is required")

command = _jupyter_realpath(sys.argv[0], subcommand)
command = _jupyter_abspath(sys.argv[0], subcommand)

try:
_execvp(command, sys.argv[1:])
Expand Down

0 comments on commit 7fe4c25

Please sign in to comment.