From 7fe4c2527a2627defac17beb9f088d32de4dc6c4 Mon Sep 17 00:00:00 2001 From: Ivan Ogasawara Date: Tue, 11 Jun 2019 19:06:28 -0400 Subject: [PATCH] Applied changes from review --- jupyter_core/command.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/jupyter_core/command.py b/jupyter_core/command.py index f9d2afce..b3b2da40 100644 --- a/jupyter_core/command.py +++ b/jupyter_core/command.py @@ -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__ @@ -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- - 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- + 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(): @@ -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:])