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