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