##// END OF EJS Templates
Add kernelspec command line entry point
Thomas Kluyver -
Show More
@@ -0,0 +1,109 b''
1
2 # Copyright (c) IPython Development Team.
3 # Distributed under the terms of the Modified BSD License.
4
5 import errno
6 import os.path
7
8 from IPython.config.application import Application
9 from IPython.core.application import BaseIPythonApplication, base_flags
10 from IPython.utils.traitlets import Instance, Dict, Unicode, Bool
11
12 from .kernelspec import KernelSpecManager
13
14 def _pythonfirst(s):
15 "Sort key function that will put strings starting with 'python' first."
16 if s.startswith('python'):
17 return ''
18 return s
19
20 class ListKernelSpecs(BaseIPythonApplication):
21 description = """List installed kernel specifications."""
22 kernel_spec_manager = Instance(KernelSpecManager)
23
24 def _kernel_spec_manager_default(self):
25 return KernelSpecManager(ipython_dir=self.ipython_dir)
26
27 def start(self):
28 print("Available kernels:")
29 for kernelname in sorted(self.kernel_spec_manager.find_kernel_specs(),
30 key=_pythonfirst):
31 print(" %s" % kernelname)
32
33
34 class InstallKernelSpec(BaseIPythonApplication):
35 description = """Install a kernel specification directory."""
36 kernel_spec_manager = Instance(KernelSpecManager)
37
38 def _kernel_spec_manager_default(self):
39 return KernelSpecManager(ipython_dir=self.ipython_dir)
40
41 sourcedir = Unicode()
42 kernel_name = Unicode("", config=True,
43 help="Install the kernel spec with this name"
44 )
45 def _kernel_name_default(self):
46 return os.path.basename(self.sourcedir)
47
48 system = Bool(False, config=True,
49 help="""
50 Try to install the kernel spec to the systemwide directory instead of
51 the per-user directory.
52 """
53 )
54 replace = Bool(False, config=True,
55 help="Replace any existing kernel spec with this name."
56 )
57
58 aliases = {'name': 'InstallKernelSpec.kernel_name'}
59
60 flags = {'system': ({'InstallKernelSpec': {'system': True}},
61 "Install to the systemwide kernel registry"),
62 'replace': ({'InstallKernelSpec': {'replace': True}},
63 "Replace any existing kernel spec with this name."),
64 }
65 flags.update(base_flags)
66
67 def parse_command_line(self, argv):
68 super(InstallKernelSpec, self).parse_command_line(argv)
69 # accept positional arg as profile name
70 if self.extra_args:
71 self.sourcedir = self.extra_args[0]
72 else:
73 print("No source directory specified.")
74 self.exit(1)
75
76 def start(self):
77 try:
78 self.kernel_spec_manager.install_kernel_spec(self.sourcedir,
79 kernel_name=self.kernel_name,
80 system=self.system,
81 replace=self.replace,
82 )
83 except OSError as e:
84 if e.errno == errno.EACCES:
85 print("Permission denied")
86 self.exit(1)
87 elif e.errno == errno.EEXIST:
88 print("A kernel spec named %r is already present" % self.kernel_name)
89 self.exit(1)
90 raise
91
92 class KernelSpecApp(Application):
93 name = "ipython-kernelspec"
94 description = """Manage IPython kernel specifications."""
95
96 subcommands = Dict(dict(
97 list = (ListKernelSpecs, ListKernelSpecs.description.splitlines()[0]),
98 install = (InstallKernelSpec, InstallKernelSpec.description.splitlines()[0])
99 ))
100
101 def start(self):
102 if self.subapp is None:
103 print("No subcommand specified. Must specify one of: %s"% list(self.subcommands))
104 print()
105 self.print_description()
106 self.print_subcommands()
107 self.exit(1)
108 else:
109 return self.subapp.start()
@@ -250,6 +250,9 b' class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):'
250 trust=('IPython.nbformat.sign.TrustNotebookApp',
250 trust=('IPython.nbformat.sign.TrustNotebookApp',
251 "Sign notebooks to trust their potentially unsafe contents at load."
251 "Sign notebooks to trust their potentially unsafe contents at load."
252 ),
252 ),
253 kernelspec=('IPython.kernel.kernelspecapp.KernelSpecApp',
254 "Manage IPython kernel specifications."
255 ),
253 )
256 )
254 subcommands['install-nbextension'] = (
257 subcommands['install-nbextension'] = (
255 "IPython.html.nbextensions.NBExtensionApp",
258 "IPython.html.nbextensions.NBExtensionApp",
General Comments 0
You need to be logged in to leave comments. Login now