Show More
@@ -16,6 +16,7 b' Utilities for path handling.' | |||
|
16 | 16 | |
|
17 | 17 | import os |
|
18 | 18 | import sys |
|
19 | import tempfile | |
|
19 | 20 | from hashlib import md5 |
|
20 | 21 | |
|
21 | 22 | import IPython |
@@ -41,6 +42,10 b' def _get_long_path_name(path):' | |||
|
41 | 42 | """Dummy no-op.""" |
|
42 | 43 | return path |
|
43 | 44 | |
|
45 | def _writable_dir(path): | |
|
46 | """Whether `path` is a directory, to which the user has write access.""" | |
|
47 | return os.path.isdir(path) and os.access(path, os.W_OK) | |
|
48 | ||
|
44 | 49 | if sys.platform == 'win32': |
|
45 | 50 | def _get_long_path_name(path): |
|
46 | 51 | """Get a long path name (expand ~) on Windows using ctypes. |
@@ -166,7 +171,6 b' def get_home_dir():' | |||
|
166 | 171 | raised for all other OSes. |
|
167 | 172 | """ |
|
168 | 173 | |
|
169 | isdir = os.path.isdir | |
|
170 | 174 | env = os.environ |
|
171 | 175 | |
|
172 | 176 | # first, check py2exe distribution root directory for _ipython. |
@@ -178,7 +182,7 b' def get_home_dir():' | |||
|
178 | 182 | else: |
|
179 | 183 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") |
|
180 | 184 | root=os.path.abspath(root).rstrip('\\') |
|
181 |
if |
|
|
185 | if _writable_dir(os.path.join(root, '_ipython')): | |
|
182 | 186 | os.environ["IPYKITROOT"] = root |
|
183 | 187 | return _cast_unicode(root, fs_encoding) |
|
184 | 188 | |
@@ -212,7 +216,7 b' def get_home_dir():' | |||
|
212 | 216 | except KeyError: |
|
213 | 217 | pass |
|
214 | 218 | else: |
|
215 |
if |
|
|
219 | if _writable_dir(homedir): | |
|
216 | 220 | return _cast_unicode(homedir, fs_encoding) |
|
217 | 221 | |
|
218 | 222 | # Now look for a local home directory |
@@ -221,7 +225,7 b' def get_home_dir():' | |||
|
221 | 225 | except KeyError: |
|
222 | 226 | pass |
|
223 | 227 | else: |
|
224 |
if |
|
|
228 | if _writable_dir(homedir): | |
|
225 | 229 | return _cast_unicode(homedir, fs_encoding) |
|
226 | 230 | |
|
227 | 231 | # Now the users profile directory |
@@ -230,7 +234,7 b' def get_home_dir():' | |||
|
230 | 234 | except KeyError: |
|
231 | 235 | pass |
|
232 | 236 | else: |
|
233 |
if |
|
|
237 | if _writable_dir(homedir): | |
|
234 | 238 | return _cast_unicode(homedir, fs_encoding) |
|
235 | 239 | |
|
236 | 240 | # Use the registry to get the 'My Documents' folder. |
@@ -245,7 +249,7 b' def get_home_dir():' | |||
|
245 | 249 | except: |
|
246 | 250 | pass |
|
247 | 251 | else: |
|
248 |
if |
|
|
252 | if _writable_dir(homedir): | |
|
249 | 253 | return _cast_unicode(homedir, fs_encoding) |
|
250 | 254 | |
|
251 | 255 | # A user with a lot of unix tools in win32 may have defined $HOME. |
@@ -255,7 +259,7 b' def get_home_dir():' | |||
|
255 | 259 | except KeyError: |
|
256 | 260 | pass |
|
257 | 261 | else: |
|
258 |
if |
|
|
262 | if _writable_dir(homedir): | |
|
259 | 263 | return _cast_unicode(homedir, fs_encoding) |
|
260 | 264 | |
|
261 | 265 | # If all else fails, raise HomeDirError |
@@ -272,14 +276,13 b' def get_xdg_dir():' | |||
|
272 | 276 | This is only for posix (Linux,Unix,OS X, etc) systems. |
|
273 | 277 | """ |
|
274 | 278 | |
|
275 | isdir = os.path.isdir | |
|
276 | 279 | env = os.environ |
|
277 | 280 | |
|
278 | 281 | if os.name == 'posix': |
|
279 | 282 | # Linux, Unix, AIX, OS X |
|
280 | 283 | # use ~/.config if not set OR empty |
|
281 | 284 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') |
|
282 |
if xdg and |
|
|
285 | if xdg and _writable_dir(xdg): | |
|
283 | 286 | return _cast_unicode(xdg, fs_encoding) |
|
284 | 287 | |
|
285 | 288 | return None |
@@ -294,7 +297,7 b' def get_ipython_dir():' | |||
|
294 | 297 | |
|
295 | 298 | env = os.environ |
|
296 | 299 | pjoin = os.path.join |
|
297 | exists = os.path.exists | |
|
300 | ||
|
298 | 301 | |
|
299 | 302 | ipdir_def = '.ipython' |
|
300 | 303 | xdg_def = 'ipython' |
@@ -312,7 +315,7 b' def get_ipython_dir():' | |||
|
312 | 315 | |
|
313 | 316 | xdg_ipdir = pjoin(xdg_dir, xdg_def) |
|
314 | 317 | |
|
315 |
if |
|
|
318 | if _writable_dir(xdg_ipdir) or not _writable_dir(home_ipdir): | |
|
316 | 319 | ipdir = xdg_ipdir |
|
317 | 320 | |
|
318 | 321 | if ipdir is None: |
@@ -320,6 +323,19 b' def get_ipython_dir():' | |||
|
320 | 323 | ipdir = home_ipdir |
|
321 | 324 | |
|
322 | 325 | ipdir = os.path.normpath(os.path.expanduser(ipdir)) |
|
326 | ||
|
327 | if os.path.exists(ipdir) and not _writable_dir(ipdir): | |
|
328 | # ipdir exists, but is not writable | |
|
329 | warn.warn("IPython dir '%s' is not a writable location," | |
|
330 | " using a temp directory."%ipdir) | |
|
331 | ipdir = tempfile.mkdtemp() | |
|
332 | elif not os.path.exists(ipdir): | |
|
333 | parent = ipdir.rsplit(os.path.sep, 1)[0] | |
|
334 | if not _writable_dir(parent): | |
|
335 | # ipdir does not exist and parent isn't writable | |
|
336 | warn.warn("IPython parent '%s' is not a writable location," | |
|
337 | " using a temp directory."%parent) | |
|
338 | ipdir = tempfile.mkdtemp() | |
|
323 | 339 | |
|
324 | 340 | return _cast_unicode(ipdir, fs_encoding) |
|
325 | 341 |
@@ -16,6 +16,7 b' import os' | |||
|
16 | 16 | import shutil |
|
17 | 17 | import sys |
|
18 | 18 | import tempfile |
|
19 | import StringIO | |
|
19 | 20 | |
|
20 | 21 | from os.path import join, abspath, split |
|
21 | 22 | |
@@ -26,7 +27,7 b' from nose import with_setup' | |||
|
26 | 27 | import IPython |
|
27 | 28 | from IPython.testing import decorators as dec |
|
28 | 29 | from IPython.testing.decorators import skip_if_not_win32, skip_win32 |
|
29 | from IPython.utils import path | |
|
30 | from IPython.utils import path, io | |
|
30 | 31 | |
|
31 | 32 | # Platform-dependent imports |
|
32 | 33 | try: |
@@ -92,7 +93,8 b' def teardown_environment():' | |||
|
92 | 93 | """Restore things that were remebered by the setup_environment function |
|
93 | 94 | """ |
|
94 | 95 | (oldenv, os.name, path.get_home_dir, IPython.__file__,) = oldstuff |
|
95 | ||
|
96 | reload(path) | |
|
97 | ||
|
96 | 98 | for key in env.keys(): |
|
97 | 99 | if key not in oldenv: |
|
98 | 100 | del env[key] |
@@ -229,6 +231,7 b' def test_get_home_dir_8():' | |||
|
229 | 231 | def test_get_ipython_dir_1(): |
|
230 | 232 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
231 | 233 | env_ipdir = os.path.join("someplace", ".ipython") |
|
234 | path._writable_dir = lambda path: True | |
|
232 | 235 | env['IPYTHON_DIR'] = env_ipdir |
|
233 | 236 | ipdir = path.get_ipython_dir() |
|
234 | 237 | nt.assert_equal(ipdir, env_ipdir) |
@@ -238,6 +241,8 b' def test_get_ipython_dir_1():' | |||
|
238 | 241 | def test_get_ipython_dir_2(): |
|
239 | 242 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
240 | 243 | path.get_home_dir = lambda : "someplace" |
|
244 | path.get_xdg_dir = lambda : None | |
|
245 | path._writable_dir = lambda path: True | |
|
241 | 246 | os.name = "posix" |
|
242 | 247 | env.pop('IPYTHON_DIR', None) |
|
243 | 248 | env.pop('IPYTHONDIR', None) |
@@ -249,6 +254,7 b' def test_get_ipython_dir_2():' | |||
|
249 | 254 | def test_get_ipython_dir_3(): |
|
250 | 255 | """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist.""" |
|
251 | 256 | path.get_home_dir = lambda : "someplace" |
|
257 | path._writable_dir = lambda path: True | |
|
252 | 258 | os.name = "posix" |
|
253 | 259 | env.pop('IPYTHON_DIR', None) |
|
254 | 260 | env.pop('IPYTHONDIR', None) |
@@ -283,18 +289,23 b' def test_get_ipython_dir_5():' | |||
|
283 | 289 | @with_environment |
|
284 | 290 | def test_get_ipython_dir_6(): |
|
285 | 291 | """test_get_ipython_dir_6, use XDG if defined and neither exist.""" |
|
286 | path.get_home_dir = lambda : 'somehome' | |
|
287 | path.get_xdg_dir = lambda : 'somexdg' | |
|
292 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') | |
|
293 | os.mkdir(xdg) | |
|
294 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) | |
|
295 | path.get_home_dir = lambda : HOME_TEST_DIR | |
|
296 | path.get_xdg_dir = lambda : xdg | |
|
288 | 297 | os.name = "posix" |
|
289 | 298 | env.pop('IPYTHON_DIR', None) |
|
290 | 299 | env.pop('IPYTHONDIR', None) |
|
291 | xdg_ipdir = os.path.join("somexdg", "ipython") | |
|
300 | env.pop('XDG_CONFIG_HOME', None) | |
|
301 | xdg_ipdir = os.path.join(xdg, "ipython") | |
|
292 | 302 | ipdir = path.get_ipython_dir() |
|
293 | 303 | nt.assert_equal(ipdir, xdg_ipdir) |
|
294 | 304 | |
|
295 | 305 | @with_environment |
|
296 | 306 | def test_get_ipython_dir_7(): |
|
297 | 307 | """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR""" |
|
308 | path._writable_dir = lambda path: True | |
|
298 | 309 | home_dir = os.path.expanduser('~') |
|
299 | 310 | env['IPYTHON_DIR'] = os.path.join('~', 'somewhere') |
|
300 | 311 | ipdir = path.get_ipython_dir() |
@@ -305,6 +316,7 b' def test_get_ipython_dir_7():' | |||
|
305 | 316 | def test_get_xdg_dir_1(): |
|
306 | 317 | """test_get_xdg_dir_1, check xdg_dir""" |
|
307 | 318 | reload(path) |
|
319 | path._writable_dir = lambda path: True | |
|
308 | 320 | path.get_home_dir = lambda : 'somewhere' |
|
309 | 321 | os.name = "posix" |
|
310 | 322 | env.pop('IPYTHON_DIR', None) |
@@ -369,3 +381,24 b' def test_get_long_path_name():' | |||
|
369 | 381 | p = path.get_long_path_name('/usr/local') |
|
370 | 382 | nt.assert_equals(p,'/usr/local') |
|
371 | 383 | |
|
384 | @dec.skip_win32 # can't create not-user-writable dir on win | |
|
385 | @with_environment | |
|
386 | def test_not_writable_ipdir(): | |
|
387 | tmpdir = tempfile.mkdtemp() | |
|
388 | os.name = "posix" | |
|
389 | env.pop('IPYTHON_DIR', None) | |
|
390 | env.pop('IPYTHONDIR', None) | |
|
391 | env.pop('XDG_CONFIG_HOME', None) | |
|
392 | env['HOME'] = tmpdir | |
|
393 | ipdir = os.path.join(tmpdir, '.ipython') | |
|
394 | os.mkdir(ipdir) | |
|
395 | os.chmod(ipdir, 600) | |
|
396 | stderr = io.stderr | |
|
397 | pipe = StringIO.StringIO() | |
|
398 | io.stderr = pipe | |
|
399 | ipdir = path.get_ipython_dir() | |
|
400 | io.stderr.flush() | |
|
401 | io.stderr = stderr | |
|
402 | nt.assert_true('WARNING' in pipe.getvalue()) | |
|
403 | env.pop('IPYTHON_DIR', None) | |
|
404 | No newline at end of file |
@@ -164,8 +164,14 b' class Kernel(Configurable):' | |||
|
164 | 164 | # reason for this to be anything less than ~ 0.1s |
|
165 | 165 | # since it is a real poller and will respond |
|
166 | 166 | # to events immediately |
|
167 | poller.poll(10*1000*self._poll_interval) | |
|
168 | self.do_one_iteration() | |
|
167 | ||
|
168 | # double nested try/except, to properly catch KeyboardInterrupt | |
|
169 | # due to pyzmq Issue #130 | |
|
170 | try: | |
|
171 | poller.poll(10*1000*self._poll_interval) | |
|
172 | self.do_one_iteration() | |
|
173 | except: | |
|
174 | raise | |
|
169 | 175 | except KeyboardInterrupt: |
|
170 | 176 | # Ctrl-C shouldn't crash the kernel |
|
171 | 177 | io.raw_print("KeyboardInterrupt caught in kernel") |
General Comments 0
You need to be logged in to leave comments.
Login now