Skip to content

Commit 4672639

Browse files
committed
Add --kernel-make-path-relative option
1 parent 19ca2bf commit 4672639

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ These are envkernel-specific options:
9595
used as a template for all kernel options (`--language`,
9696
`--kernel-cmd`, etc). Also, any other file in this directory (such
9797
as logos) are copied to the new kernel (like kernel.js in irkernel).
98+
* `--kernel-make-path-relative` removes an absolute path from the
99+
kernel command (mainly useful with `--kernel-template`). This would
100+
be useful, for example, where you are setting up an lmod install and
101+
the absolute path of the module might change, but you want it to
102+
always run Python relative to that module anyway.
98103
* `--env=NAME=VALUE`. Set these environment variables when running
99104
the kernel. These are actually just saved in the `kernel.json` file
100105
under the `env` key, which is used by Jupyter itself. So, this is

envkernel.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ def setup(self):
119119
help="Python command to run (default 'python')")
120120
parser.add_argument('--kernel-cmd',
121121
help="Kernel command to run, separated by spaces. If this is given, --python is not used.")
122+
parser.add_argument('--kernel-make-path-relative', action='store_true',
123+
help="Remove any leading absolute path from the kernel command. Mainly "
124+
"useful with --kernel-template.")
122125
parser.add_argument('--language',
123126
help="Language to put into kernel file (default based on --kernel)")
124127
parser.add_argument('--env', action='append', default=[],
@@ -172,6 +175,9 @@ def setup(self):
172175
self.kernel['argv'][0] = args.python
173176
if args.display_name:
174177
self.kernel['display_name'] = args.display_name
178+
# Make the kernel path relative
179+
if args.kernel_make_path_relative:
180+
self.kernel['argv'][0] = self.kernel['argv'][0].rsplit('/', 1)[-1]
175181
# Copy logos from upstream packages, if exists
176182
self.logos = None
177183
if self.kernel['language'] == 'python':

test_envkernel.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ def test_template_copyfiles(d, mode):
121121
assert os.path.exists(pjoin(kern['dir'], 'A.txt'))
122122
assert open(pjoin(kern['dir'], 'A.txt')).read() == 'LMNO'
123123

124+
@all_modes(['conda'])
125+
def test_template_make_path_relative(d, mode):
126+
os.environ['JUPYTER_PATH'] = pjoin(d, 'share/jupyter')
127+
subprocess.call("python -m ipykernel install --name=aaa-ipy --display-name=BBB --prefix=%s"%d, shell=True)
128+
# First test it without, ensure it has the full path
129+
kern = install(d, "%s --kernel-template aaa-ipy MOD1"%mode)
130+
assert kern['k'][0] != 'python' # This is an absolete path
131+
# Now test it with --kernel-make-path-relative and ensure it's relative
132+
kern = install(d, "%s --kernel-template aaa-ipy --kernel-make-path-relative MOD1"%mode)
133+
assert kern['k'][0] == 'python' # This is an absolete path
134+
124135
def test_help():
125136
"""Test that the global -h option works and prints module names"""
126137
p = subprocess.Popen("python -m envkernel -h", shell=True, stdout=subprocess.PIPE)

0 commit comments

Comments
 (0)