Show More
@@ -18,8 +18,8 b" if os.name == 'nt':" | |||
|
18 | 18 | else: # PROGRAMDATA is not defined by default on XP. |
|
19 | 19 | SYSTEM_KERNEL_DIRS = [] |
|
20 | 20 | else: |
|
21 |
SYSTEM_KERNEL_DIRS = ["/usr/share/ |
|
|
22 |
"/usr/local/share/ |
|
|
21 | SYSTEM_KERNEL_DIRS = ["/usr/share/jupyter/kernels", | |
|
22 | "/usr/local/share/jupyter/kernels", | |
|
23 | 23 | ] |
|
24 | 24 | |
|
25 | 25 | NATIVE_KERNEL_NAME = 'python3' if PY3 else 'python2' |
@@ -87,14 +87,20 b' class KernelSpecManager(HasTraits):' | |||
|
87 | 87 | user_kernel_dir = Unicode() |
|
88 | 88 | def _user_kernel_dir_default(self): |
|
89 | 89 | return pjoin(self.ipython_dir, 'kernels') |
|
90 | ||
|
91 | @property | |
|
92 | def env_kernel_dir(self): | |
|
93 | return pjoin(sys.prefix, 'share', 'jupyter', 'kernels') | |
|
90 | 94 | |
|
91 | 95 | kernel_dirs = List( |
|
92 | 96 | help="List of kernel directories to search. Later ones take priority over earlier." |
|
93 | 97 | ) |
|
94 | 98 | def _kernel_dirs_default(self): |
|
95 |
|
|
|
96 |
|
|
|
97 | ] | |
|
99 | dirs = SYSTEM_KERNEL_DIRS[:] | |
|
100 | if self.env_kernel_dir not in dirs: | |
|
101 | dirs.append(self.env_kernel_dir) | |
|
102 | dirs.append(self.user_kernel_dir) | |
|
103 | return dirs | |
|
98 | 104 | |
|
99 | 105 | @property |
|
100 | 106 | def _native_kernel_dict(self): |
@@ -137,16 +143,17 b' class KernelSpecManager(HasTraits):' | |||
|
137 | 143 | raise NoSuchKernel(kernel_name) |
|
138 | 144 | return KernelSpec.from_resource_dir(resource_dir) |
|
139 | 145 | |
|
140 |
def _get_destination_dir(self, kernel_name, |
|
|
141 |
if |
|
|
146 | def _get_destination_dir(self, kernel_name, user=False): | |
|
147 | if user: | |
|
148 | return os.path.join(self.user_kernel_dir, kernel_name) | |
|
149 | else: | |
|
142 | 150 | if SYSTEM_KERNEL_DIRS: |
|
143 | 151 | return os.path.join(SYSTEM_KERNEL_DIRS[-1], kernel_name) |
|
144 | 152 | else: |
|
145 | 153 | raise EnvironmentError("No system kernel directory is available") |
|
146 | else: | |
|
147 | return os.path.join(self.user_kernel_dir, kernel_name) | |
|
148 | 154 | |
|
149 | def install_kernel_spec(self, source_dir, kernel_name=None, system=False, | |
|
155 | ||
|
156 | def install_kernel_spec(self, source_dir, kernel_name=None, user=False, | |
|
150 | 157 | replace=False): |
|
151 | 158 | """Install a kernel spec by copying its directory. |
|
152 | 159 | |
@@ -165,14 +172,14 b' class KernelSpecManager(HasTraits):' | |||
|
165 | 172 | kernel_name = os.path.basename(source_dir) |
|
166 | 173 | kernel_name = kernel_name.lower() |
|
167 | 174 | |
|
168 |
destination = self._get_destination_dir(kernel_name, |
|
|
175 | destination = self._get_destination_dir(kernel_name, user=user) | |
|
169 | 176 | |
|
170 | 177 | if replace and os.path.isdir(destination): |
|
171 | 178 | shutil.rmtree(destination) |
|
172 | 179 | |
|
173 | 180 | shutil.copytree(source_dir, destination) |
|
174 | 181 | |
|
175 |
def install_native_kernel_spec(self, |
|
|
182 | def install_native_kernel_spec(self, user=False): | |
|
176 | 183 | """Install the native kernel spec to the filesystem |
|
177 | 184 | |
|
178 | 185 | This allows a Python 3 frontend to use a Python 2 kernel, or vice versa. |
@@ -183,7 +190,7 b' class KernelSpecManager(HasTraits):' | |||
|
183 | 190 | kernel registry. If the process does not have appropriate permissions, |
|
184 | 191 | an :exc:`OSError` will be raised. |
|
185 | 192 | """ |
|
186 |
path = self._get_destination_dir(NATIVE_KERNEL_NAME, |
|
|
193 | path = self._get_destination_dir(NATIVE_KERNEL_NAME, user=user) | |
|
187 | 194 | os.makedirs(path, mode=0o755) |
|
188 | 195 | with open(pjoin(path, 'kernel.json'), 'w') as f: |
|
189 | 196 | json.dump(self._native_kernel_dict, f, indent=1) |
@@ -203,13 +210,13 b' def get_kernel_spec(kernel_name):' | |||
|
203 | 210 | """ |
|
204 | 211 | return KernelSpecManager().get_kernel_spec(kernel_name) |
|
205 | 212 | |
|
206 |
def install_kernel_spec(source_dir, kernel_name=None, |
|
|
213 | def install_kernel_spec(source_dir, kernel_name=None, user=False, replace=False): | |
|
207 | 214 | return KernelSpecManager().install_kernel_spec(source_dir, kernel_name, |
|
208 |
|
|
|
215 | user, replace) | |
|
209 | 216 | |
|
210 | 217 | install_kernel_spec.__doc__ = KernelSpecManager.install_kernel_spec.__doc__ |
|
211 | 218 | |
|
212 |
def install_native_kernel_spec( |
|
|
213 |
return KernelSpecManager().install_native_kernel_spec( |
|
|
219 | def install_native_kernel_spec(user=False): | |
|
220 | return KernelSpecManager().install_native_kernel_spec(user=user) | |
|
214 | 221 | |
|
215 | 222 | install_native_kernel_spec.__doc__ = KernelSpecManager.install_native_kernel_spec.__doc__ |
@@ -45,10 +45,10 b' class InstallKernelSpec(BaseIPythonApplication):' | |||
|
45 | 45 | def _kernel_name_default(self): |
|
46 | 46 | return os.path.basename(self.sourcedir) |
|
47 | 47 | |
|
48 |
|
|
|
48 | user = Bool(False, config=True, | |
|
49 | 49 | help=""" |
|
50 |
Try to install the kernel spec to the |
|
|
51 |
the |
|
|
50 | Try to install the kernel spec to the per-user directory instead of | |
|
51 | the system or environment directory. | |
|
52 | 52 | """ |
|
53 | 53 | ) |
|
54 | 54 | replace = Bool(False, config=True, |
@@ -59,8 +59,8 b' class InstallKernelSpec(BaseIPythonApplication):' | |||
|
59 | 59 | for k in ['ipython-dir', 'log-level']: |
|
60 | 60 | aliases[k] = base_aliases[k] |
|
61 | 61 | |
|
62 |
flags = {' |
|
|
63 |
"Install to the |
|
|
62 | flags = {'user': ({'InstallKernelSpec': {'user': True}}, | |
|
63 | "Install to the per-user kernel registry"), | |
|
64 | 64 | 'replace': ({'InstallKernelSpec': {'replace': True}}, |
|
65 | 65 | "Replace any existing kernel spec with this name."), |
|
66 | 66 | 'debug': base_flags['debug'], |
@@ -79,7 +79,7 b' class InstallKernelSpec(BaseIPythonApplication):' | |||
|
79 | 79 | try: |
|
80 | 80 | self.kernel_spec_manager.install_kernel_spec(self.sourcedir, |
|
81 | 81 | kernel_name=self.kernel_name, |
|
82 |
|
|
|
82 | user=self.user, | |
|
83 | 83 | replace=self.replace, |
|
84 | 84 | ) |
|
85 | 85 | except OSError as e: |
@@ -98,23 +98,23 b' class InstallNativeKernelSpec(BaseIPythonApplication):' | |||
|
98 | 98 | def _kernel_spec_manager_default(self): |
|
99 | 99 | return KernelSpecManager(ipython_dir=self.ipython_dir) |
|
100 | 100 | |
|
101 |
|
|
|
101 | user = Bool(False, config=True, | |
|
102 | 102 | help=""" |
|
103 |
Try to install the kernel spec to the |
|
|
104 |
the |
|
|
103 | Try to install the kernel spec to the per-user directory instead of | |
|
104 | the system or environment 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 |
flags = {' |
|
|
111 |
"Install to the |
|
|
110 | flags = {'user': ({'InstallNativeKernelSpec': {'user': True}}, | |
|
111 | "Install to the per-user kernel registry"), | |
|
112 | 112 | 'debug': base_flags['debug'], |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | def start(self): |
|
116 | 116 | try: |
|
117 |
self.kernel_spec_manager.install_native_kernel_spec( |
|
|
117 | self.kernel_spec_manager.install_native_kernel_spec(user=self.user) | |
|
118 | 118 | except OSError as e: |
|
119 | 119 | self.exit(e) |
|
120 | 120 |
@@ -42,21 +42,23 b' class KernelSpecTests(unittest.TestCase):' | |||
|
42 | 42 | |
|
43 | 43 | def test_install_kernel_spec(self): |
|
44 | 44 | self.ksm.install_kernel_spec(self.installable_kernel, |
|
45 |
kernel_name='tstinstalled' |
|
|
45 | kernel_name='tstinstalled', | |
|
46 | user=True) | |
|
46 | 47 | self.assertIn('tstinstalled', self.ksm.find_kernel_specs()) |
|
47 | 48 | |
|
48 | 49 | with self.assertRaises(OSError): |
|
49 | 50 | self.ksm.install_kernel_spec(self.installable_kernel, |
|
50 |
kernel_name='tstinstalled' |
|
|
51 | kernel_name='tstinstalled', | |
|
52 | user=True) | |
|
51 | 53 | |
|
52 | 54 | # Smoketest that this succeeds |
|
53 | 55 | self.ksm.install_kernel_spec(self.installable_kernel, |
|
54 | 56 | kernel_name='tstinstalled', |
|
55 | replace=True) | |
|
56 | ||
|
57 | replace=True, user=True) | |
|
58 | ||
|
57 | 59 | @onlyif(os.name != 'nt' and not os.access('/usr/local/share', os.W_OK), "needs Unix system without root privileges") |
|
58 | 60 | def test_cant_install_kernel_spec(self): |
|
59 | 61 | with self.assertRaises(OSError): |
|
60 | 62 | self.ksm.install_kernel_spec(self.installable_kernel, |
|
61 | 63 | kernel_name='tstinstalled', |
|
62 |
|
|
|
64 | user=False) |
General Comments 0
You need to be logged in to leave comments.
Login now