##// END OF EJS Templates
Kernelspec handle case where folder already exists...
Matthias BUSSONNIER -
Show More
@@ -1,145 +1,148 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 system = 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 systemwide directory instead of
51 the per-user directory.
51 the per-user 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 = {'system': ({'InstallKernelSpec': {'system': True}},
63 "Install to the systemwide kernel registry"),
63 "Install to the systemwide 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 system=self.system,
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 system = 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 systemwide directory instead of
104 the per-user directory.
104 the per-user 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': ({'InstallOwnKernelSpec': {'system': True}},
110 flags = {'system': ({'InstallOwnKernelSpec': {'system': True}},
111 "Install to the systemwide kernel registry"),
111 "Install to the systemwide 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(system=self.system)
118 except OSError as e:
118 except OSError as e:
119 if e.errno == errno.EACCES:
119 if e.errno == errno.EACCES:
120 print("Permission denied")
120 print("Permission denied")
121 self.exit(1)
121 self.exit(1)
122 if e.errno == errno.EEXIST:
123 print("File or folder already exists")
124 self.exit(1)
122 raise
125 raise
123
126
124 class KernelSpecApp(Application):
127 class KernelSpecApp(Application):
125 name = "ipython kernelspec"
128 name = "ipython kernelspec"
126 description = """Manage IPython kernel specifications."""
129 description = """Manage IPython kernel specifications."""
127
130
128 subcommands = Dict({
131 subcommands = Dict({
129 'list': (ListKernelSpecs, ListKernelSpecs.description.splitlines()[0]),
132 'list': (ListKernelSpecs, ListKernelSpecs.description.splitlines()[0]),
130 'install': (InstallKernelSpec, InstallKernelSpec.description.splitlines()[0]),
133 'install': (InstallKernelSpec, InstallKernelSpec.description.splitlines()[0]),
131 'install-self': (InstallNativeKernelSpec, InstallNativeKernelSpec.description.splitlines()[0]),
134 'install-self': (InstallNativeKernelSpec, InstallNativeKernelSpec.description.splitlines()[0]),
132 })
135 })
133
136
134 aliases = {}
137 aliases = {}
135 flags = {}
138 flags = {}
136
139
137 def start(self):
140 def start(self):
138 if self.subapp is None:
141 if self.subapp is None:
139 print("No subcommand specified. Must specify one of: %s"% list(self.subcommands))
142 print("No subcommand specified. Must specify one of: %s"% list(self.subcommands))
140 print()
143 print()
141 self.print_description()
144 self.print_description()
142 self.print_subcommands()
145 self.print_subcommands()
143 self.exit(1)
146 self.exit(1)
144 else:
147 else:
145 return self.subapp.start()
148 return self.subapp.start()
General Comments 0
You need to be logged in to leave comments. Login now