Skip to content

Commit 2621c8c

Browse files
committed
With kernel templates, also copy any other files in the kernel dir
- This copies logo files that may exist - This copies the kernel.js that exists in irkernel - This replaces the previous custom Python logo handling.
1 parent 88365fe commit 2621c8c

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

envkernel.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def setup(self):
128128
else:
129129
self.prefix = args.prefix
130130
self.replace = args.replace
131+
self.copy_files = { }
131132

132133
# Setting the kernel. Go through least-specific to
133134
# most-specific, updating self.kernel with the latest (most
@@ -140,6 +141,8 @@ def setup(self):
140141
if args.kernel_template:
141142
import jupyter_client.kernelspec
142143
template = jupyter_client.kernelspec.KernelSpecManager().get_kernel_spec(args.kernel_template)
144+
template_dir = template.resource_dir
145+
self.copy_files.update({x: pjoin(template_dir, x) for x in os.listdir(template_dir)})
143146
self.kernel = json.loads(template.to_json())
144147
# --kernel which sets to default well-known kernels.
145148
if args.kernel is None and 'argv' not in self.kernel:
@@ -169,7 +172,10 @@ def setup(self):
169172
import ipykernel
170173
ipykernel_dir = os.path.dirname(ipykernel.__file__)
171174
logos = glob.glob(pjoin(ipykernel_dir, 'resources', '*'))
172-
self.logos = logos
175+
for fullpath in logos:
176+
fname = os.path.basename(fullpath)
177+
if fname not in self.copy_files:
178+
self.copy_files[fname] = fullpath
173179
except ImportError:
174180
LOG.debug("Could not automatically find ipykernel logos")
175181
# env
@@ -206,14 +212,13 @@ def install_kernel(self, kernel, name, user=False, replace=None, prefix=None, lo
206212
umask = os.umask(0)
207213
os.umask(umask) # Restore previous, this is just how it works...
208214
os.chmod(kernel_dir, 0o777& (~umask))
209-
#
215+
# Copy files from template. This also copies kernel.json,
216+
# but we will overwrite that next.
217+
for fname, fullpath in self.copy_files.items():
218+
shutil.copy(fullpath, pjoin(kernel_dir, fname))
219+
# Write kernel.json
210220
open(pjoin(kernel_dir, 'kernel.json'), 'w').write(
211221
json.dumps(kernel, sort_keys=True, indent=1))
212-
if self.logos:
213-
if isinstance(self.logos, str) and os.path.isdir(self.logos):
214-
self.logos = os.listdir(self.logos)
215-
for logo in self.logos:
216-
shutil.copy(logo, kernel_dir)
217222
jupyter_client.kernelspec.KernelSpecManager().install_kernel_spec(
218223
kernel_dir, kernel_name=name,
219224
user=user, replace=replace, prefix=prefix)

test_envkernel.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,23 @@ def test_display_name(d, mode):
9696

9797
@all_modes(['conda'])
9898
def test_template(d, mode):
99+
os.environ['JUPYTER_PATH'] = pjoin(d, 'share/jupyter')
99100
subprocess.call("python -m ipykernel install --name=aaa-ipy --display-name=BBB --prefix=%s"%d, shell=True)
100101
#os.environ['ENVKERNEL_TESTPATH'] = os.path.join(d, 'share/jupyter/kernels')
101-
os.environ['JUPYTER_PATH'] = pjoin(d, 'share/jupyter')
102102
kern = install(d, "%s --kernel-template aaa-ipy MOD1"%mode)
103103
assert kern['kernel']['display_name'] == 'BBB'
104104

105+
@all_modes(['conda'])
106+
def test_template_copyfiles(d, mode):
107+
os.environ['JUPYTER_PATH'] = pjoin(d, 'share/jupyter')
108+
subprocess.call("python -m ipykernel install --name=aaa-ipy --display-name=BBB --prefix=%s"%d, shell=True)
109+
f = open(pjoin(d, 'share/jupyter/kernels/', 'aaa-ipy', 'A.txt'), 'w')
110+
f.write('LMNO')
111+
f.close()
112+
kern = install(d, "%s --kernel-template aaa-ipy MOD1"%mode)
113+
assert os.path.exists(pjoin(kern['dir'], 'A.txt'))
114+
assert open(pjoin(kern['dir'], 'A.txt')).read() == 'LMNO'
115+
105116
def test_help():
106117
"""Test that the global -h option works and prints module names"""
107118
p = subprocess.Popen("python -m envkernel -h", shell=True, stdout=subprocess.PIPE)

0 commit comments

Comments
 (0)