##// END OF EJS Templates
Update kernelspec command line interface
Thomas Kluyver -
Show More
@@ -1,142 +1,142 b''
1
1
2 # Copyright (c) IPython Development Team.
2 # Copyright (c) IPython Development Team.
3 # Distributed under the terms of the Modified BSD License.
3 # Distributed under the terms of the Modified BSD License.
4
4
5 import errno
5 import errno
6 import os.path
6 import os.path
7
7
8 from IPython.config.application import Application
8 from IPython.config.application import Application
9 from IPython.core.application import (
9 from IPython.core.application import (
10 BaseIPythonApplication, base_flags, base_aliases
10 BaseIPythonApplication, base_flags, base_aliases
11 )
11 )
12 from IPython.utils.traitlets import Instance, Dict, Unicode, Bool
12 from IPython.utils.traitlets import Instance, Dict, Unicode, Bool
13
13
14 from .kernelspec import KernelSpecManager, _pythonfirst
14 from .kernelspec import KernelSpecManager, _pythonfirst
15
15
16 class ListKernelSpecs(BaseIPythonApplication):
16 class ListKernelSpecs(BaseIPythonApplication):
17 description = """List installed kernel specifications."""
17 description = """List installed kernel specifications."""
18 kernel_spec_manager = Instance(KernelSpecManager)
18 kernel_spec_manager = Instance(KernelSpecManager)
19
19
20 # Not all of the base aliases are meaningful (e.g. profile)
20 # Not all of the base aliases are meaningful (e.g. profile)
21 aliases = {k: base_aliases[k] for k in ['ipython-dir', 'log-level']}
21 aliases = {k: base_aliases[k] for k in ['ipython-dir', 'log-level']}
22 flags = {'debug': base_flags['debug'],}
22 flags = {'debug': base_flags['debug'],}
23
23
24 def _kernel_spec_manager_default(self):
24 def _kernel_spec_manager_default(self):
25 return KernelSpecManager(ipython_dir=self.ipython_dir)
25 return KernelSpecManager(ipython_dir=self.ipython_dir)
26
26
27 def start(self):
27 def start(self):
28 print("Available kernels:")
28 print("Available kernels:")
29 for kernelname in sorted(self.kernel_spec_manager.find_kernel_specs(),
29 for kernelname in sorted(self.kernel_spec_manager.find_kernel_specs(),
30 key=_pythonfirst):
30 key=_pythonfirst):
31 print(" %s" % kernelname)
31 print(" %s" % kernelname)
32
32
33
33
34 class InstallKernelSpec(BaseIPythonApplication):
34 class InstallKernelSpec(BaseIPythonApplication):
35 description = """Install a kernel specification directory."""
35 description = """Install a kernel specification directory."""
36 kernel_spec_manager = Instance(KernelSpecManager)
36 kernel_spec_manager = Instance(KernelSpecManager)
37
37
38 def _kernel_spec_manager_default(self):
38 def _kernel_spec_manager_default(self):
39 return KernelSpecManager(ipython_dir=self.ipython_dir)
39 return KernelSpecManager(ipython_dir=self.ipython_dir)
40
40
41 sourcedir = Unicode()
41 sourcedir = Unicode()
42 kernel_name = Unicode("", config=True,
42 kernel_name = Unicode("", config=True,
43 help="Install the kernel spec with this name"
43 help="Install the kernel spec with this name"
44 )
44 )
45 def _kernel_name_default(self):
45 def _kernel_name_default(self):
46 return os.path.basename(self.sourcedir)
46 return os.path.basename(self.sourcedir)
47
47
48 system = Bool(False, config=True,
48 user = Bool(False, config=True,
49 help="""
49 help="""
50 Try to install the kernel spec to the systemwide directory instead of
50 Try to install the kernel spec to the per-user directory instead of
51 the per-user directory.
51 the system or environment directory.
52 """
52 """
53 )
53 )
54 replace = Bool(False, config=True,
54 replace = Bool(False, config=True,
55 help="Replace any existing kernel spec with this name."
55 help="Replace any existing kernel spec with this name."
56 )
56 )
57
57
58 aliases = {'name': 'InstallKernelSpec.kernel_name'}
58 aliases = {'name': 'InstallKernelSpec.kernel_name'}
59 for k in ['ipython-dir', 'log-level']:
59 for k in ['ipython-dir', 'log-level']:
60 aliases[k] = base_aliases[k]
60 aliases[k] = base_aliases[k]
61
61
62 flags = {'system': ({'InstallKernelSpec': {'system': True}},
62 flags = {'user': ({'InstallKernelSpec': {'user': True}},
63 "Install to the systemwide kernel registry"),
63 "Install to the per-user kernel registry"),
64 'replace': ({'InstallKernelSpec': {'replace': True}},
64 'replace': ({'InstallKernelSpec': {'replace': True}},
65 "Replace any existing kernel spec with this name."),
65 "Replace any existing kernel spec with this name."),
66 'debug': base_flags['debug'],
66 'debug': base_flags['debug'],
67 }
67 }
68
68
69 def parse_command_line(self, argv):
69 def parse_command_line(self, argv):
70 super(InstallKernelSpec, self).parse_command_line(argv)
70 super(InstallKernelSpec, self).parse_command_line(argv)
71 # accept positional arg as profile name
71 # accept positional arg as profile name
72 if self.extra_args:
72 if self.extra_args:
73 self.sourcedir = self.extra_args[0]
73 self.sourcedir = self.extra_args[0]
74 else:
74 else:
75 print("No source directory specified.")
75 print("No source directory specified.")
76 self.exit(1)
76 self.exit(1)
77
77
78 def start(self):
78 def start(self):
79 try:
79 try:
80 self.kernel_spec_manager.install_kernel_spec(self.sourcedir,
80 self.kernel_spec_manager.install_kernel_spec(self.sourcedir,
81 kernel_name=self.kernel_name,
81 kernel_name=self.kernel_name,
82 system=self.system,
82 user=self.user,
83 replace=self.replace,
83 replace=self.replace,
84 )
84 )
85 except OSError as e:
85 except OSError as e:
86 if e.errno == errno.EACCES:
86 if e.errno == errno.EACCES:
87 print("Permission denied")
87 print("Permission denied")
88 self.exit(1)
88 self.exit(1)
89 elif e.errno == errno.EEXIST:
89 elif e.errno == errno.EEXIST:
90 print("A kernel spec is already present at %s" % e.filename)
90 print("A kernel spec is already present at %s" % e.filename)
91 self.exit(1)
91 self.exit(1)
92 raise
92 raise
93
93
94 class InstallNativeKernelSpec(BaseIPythonApplication):
94 class InstallNativeKernelSpec(BaseIPythonApplication):
95 description = """Install the native kernel spec directory for this Python."""
95 description = """Install the native kernel spec directory for this Python."""
96 kernel_spec_manager = Instance(KernelSpecManager)
96 kernel_spec_manager = Instance(KernelSpecManager)
97
97
98 def _kernel_spec_manager_default(self):
98 def _kernel_spec_manager_default(self):
99 return KernelSpecManager(ipython_dir=self.ipython_dir)
99 return KernelSpecManager(ipython_dir=self.ipython_dir)
100
100
101 system = Bool(False, config=True,
101 user = Bool(False, config=True,
102 help="""
102 help="""
103 Try to install the kernel spec to the systemwide directory instead of
103 Try to install the kernel spec to the per-user directory instead of
104 the per-user directory.
104 the system or environment directory.
105 """
105 """
106 )
106 )
107
107
108 # Not all of the base aliases are meaningful (e.g. profile)
108 # Not all of the base aliases are meaningful (e.g. profile)
109 aliases = {k: base_aliases[k] for k in ['ipython-dir', 'log-level']}
109 aliases = {k: base_aliases[k] for k in ['ipython-dir', 'log-level']}
110 flags = {'system': ({'InstallNativeKernelSpec': {'system': True}},
110 flags = {'user': ({'InstallNativeKernelSpec': {'user': True}},
111 "Install to the systemwide kernel registry"),
111 "Install to the per-user kernel registry"),
112 'debug': base_flags['debug'],
112 'debug': base_flags['debug'],
113 }
113 }
114
114
115 def start(self):
115 def start(self):
116 try:
116 try:
117 self.kernel_spec_manager.install_native_kernel_spec(system=self.system)
117 self.kernel_spec_manager.install_native_kernel_spec(user=self.user)
118 except OSError as e:
118 except OSError as e:
119 self.exit(e)
119 self.exit(e)
120
120
121 class KernelSpecApp(Application):
121 class KernelSpecApp(Application):
122 name = "ipython kernelspec"
122 name = "ipython kernelspec"
123 description = """Manage IPython kernel specifications."""
123 description = """Manage IPython kernel specifications."""
124
124
125 subcommands = Dict({
125 subcommands = Dict({
126 'list': (ListKernelSpecs, ListKernelSpecs.description.splitlines()[0]),
126 'list': (ListKernelSpecs, ListKernelSpecs.description.splitlines()[0]),
127 'install': (InstallKernelSpec, InstallKernelSpec.description.splitlines()[0]),
127 'install': (InstallKernelSpec, InstallKernelSpec.description.splitlines()[0]),
128 'install-self': (InstallNativeKernelSpec, InstallNativeKernelSpec.description.splitlines()[0]),
128 'install-self': (InstallNativeKernelSpec, InstallNativeKernelSpec.description.splitlines()[0]),
129 })
129 })
130
130
131 aliases = {}
131 aliases = {}
132 flags = {}
132 flags = {}
133
133
134 def start(self):
134 def start(self):
135 if self.subapp is None:
135 if self.subapp is None:
136 print("No subcommand specified. Must specify one of: %s"% list(self.subcommands))
136 print("No subcommand specified. Must specify one of: %s"% list(self.subcommands))
137 print()
137 print()
138 self.print_description()
138 self.print_description()
139 self.print_subcommands()
139 self.print_subcommands()
140 self.exit(1)
140 self.exit(1)
141 else:
141 else:
142 return self.subapp.start()
142 return self.subapp.start()
General Comments 0
You need to be logged in to leave comments. Login now