##// END OF EJS Templates
use tempdir if no usable ipython_dir is found...
MinRK -
Show More
@@ -16,6 +16,7 b' Utilities for path handling.'
16
16
17 import os
17 import os
18 import sys
18 import sys
19 import tempfile
19 from hashlib import md5
20 from hashlib import md5
20
21
21 import IPython
22 import IPython
@@ -322,6 +323,19 b' def get_ipython_dir():'
322 ipdir = home_ipdir
323 ipdir = home_ipdir
323
324
324 ipdir = os.path.normpath(os.path.expanduser(ipdir))
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()
325
339
326 return _cast_unicode(ipdir, fs_encoding)
340 return _cast_unicode(ipdir, fs_encoding)
327
341
@@ -16,6 +16,7 b' import os'
16 import shutil
16 import shutil
17 import sys
17 import sys
18 import tempfile
18 import tempfile
19 import StringIO
19
20
20 from os.path import join, abspath, split
21 from os.path import join, abspath, split
21
22
@@ -26,7 +27,7 b' from nose import with_setup'
26 import IPython
27 import IPython
27 from IPython.testing import decorators as dec
28 from IPython.testing import decorators as dec
28 from IPython.testing.decorators import skip_if_not_win32, skip_win32
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 # Platform-dependent imports
32 # Platform-dependent imports
32 try:
33 try:
@@ -92,7 +93,8 b' def teardown_environment():'
92 """Restore things that were remebered by the setup_environment function
93 """Restore things that were remebered by the setup_environment function
93 """
94 """
94 (oldenv, os.name, path.get_home_dir, IPython.__file__,) = oldstuff
95 (oldenv, os.name, path.get_home_dir, IPython.__file__,) = oldstuff
95
96 reload(path)
97
96 for key in env.keys():
98 for key in env.keys():
97 if key not in oldenv:
99 if key not in oldenv:
98 del env[key]
100 del env[key]
@@ -229,6 +231,7 b' def test_get_home_dir_8():'
229 def test_get_ipython_dir_1():
231 def test_get_ipython_dir_1():
230 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
232 """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
231 env_ipdir = os.path.join("someplace", ".ipython")
233 env_ipdir = os.path.join("someplace", ".ipython")
234 path._writable_dir = lambda path: True
232 env['IPYTHON_DIR'] = env_ipdir
235 env['IPYTHON_DIR'] = env_ipdir
233 ipdir = path.get_ipython_dir()
236 ipdir = path.get_ipython_dir()
234 nt.assert_equal(ipdir, env_ipdir)
237 nt.assert_equal(ipdir, env_ipdir)
@@ -238,6 +241,8 b' def test_get_ipython_dir_1():'
238 def test_get_ipython_dir_2():
241 def test_get_ipython_dir_2():
239 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
242 """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
240 path.get_home_dir = lambda : "someplace"
243 path.get_home_dir = lambda : "someplace"
244 path.get_xdg_dir = lambda : None
245 path._writable_dir = lambda path: True
241 os.name = "posix"
246 os.name = "posix"
242 env.pop('IPYTHON_DIR', None)
247 env.pop('IPYTHON_DIR', None)
243 env.pop('IPYTHONDIR', None)
248 env.pop('IPYTHONDIR', None)
@@ -249,6 +254,7 b' def test_get_ipython_dir_2():'
249 def test_get_ipython_dir_3():
254 def test_get_ipython_dir_3():
250 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
255 """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
251 path.get_home_dir = lambda : "someplace"
256 path.get_home_dir = lambda : "someplace"
257 path._writable_dir = lambda path: True
252 os.name = "posix"
258 os.name = "posix"
253 env.pop('IPYTHON_DIR', None)
259 env.pop('IPYTHON_DIR', None)
254 env.pop('IPYTHONDIR', None)
260 env.pop('IPYTHONDIR', None)
@@ -283,18 +289,23 b' def test_get_ipython_dir_5():'
283 @with_environment
289 @with_environment
284 def test_get_ipython_dir_6():
290 def test_get_ipython_dir_6():
285 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
291 """test_get_ipython_dir_6, use XDG if defined and neither exist."""
286 path.get_home_dir = lambda : 'somehome'
292 xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
287 path.get_xdg_dir = lambda : '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 os.name = "posix"
297 os.name = "posix"
289 env.pop('IPYTHON_DIR', None)
298 env.pop('IPYTHON_DIR', None)
290 env.pop('IPYTHONDIR', None)
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 ipdir = path.get_ipython_dir()
302 ipdir = path.get_ipython_dir()
293 nt.assert_equal(ipdir, xdg_ipdir)
303 nt.assert_equal(ipdir, xdg_ipdir)
294
304
295 @with_environment
305 @with_environment
296 def test_get_ipython_dir_7():
306 def test_get_ipython_dir_7():
297 """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR"""
307 """test_get_ipython_dir_7, test home directory expansion on IPYTHON_DIR"""
308 path._writable_dir = lambda path: True
298 home_dir = os.path.expanduser('~')
309 home_dir = os.path.expanduser('~')
299 env['IPYTHON_DIR'] = os.path.join('~', 'somewhere')
310 env['IPYTHON_DIR'] = os.path.join('~', 'somewhere')
300 ipdir = path.get_ipython_dir()
311 ipdir = path.get_ipython_dir()
@@ -305,6 +316,7 b' def test_get_ipython_dir_7():'
305 def test_get_xdg_dir_1():
316 def test_get_xdg_dir_1():
306 """test_get_xdg_dir_1, check xdg_dir"""
317 """test_get_xdg_dir_1, check xdg_dir"""
307 reload(path)
318 reload(path)
319 path._writable_dir = lambda path: True
308 path.get_home_dir = lambda : 'somewhere'
320 path.get_home_dir = lambda : 'somewhere'
309 os.name = "posix"
321 os.name = "posix"
310 env.pop('IPYTHON_DIR', None)
322 env.pop('IPYTHON_DIR', None)
@@ -369,3 +381,23 b' def test_get_long_path_name():'
369 p = path.get_long_path_name('/usr/local')
381 p = path.get_long_path_name('/usr/local')
370 nt.assert_equals(p,'/usr/local')
382 nt.assert_equals(p,'/usr/local')
371
383
384 @with_environment
385 def test_not_writable_ipdir():
386 tmpdir = tempfile.mkdtemp()
387 os.name = "posix"
388 env.pop('IPYTHON_DIR', None)
389 env.pop('IPYTHONDIR', None)
390 env.pop('XDG_CONFIG_HOME', None)
391 env['HOME'] = tmpdir
392 ipdir = os.path.join(tmpdir, '.ipython')
393 os.mkdir(ipdir)
394 os.chmod(ipdir, 600)
395 stderr = io.stderr
396 pipe = StringIO.StringIO()
397 io.stderr = pipe
398 ipdir = path.get_ipython_dir()
399 io.stderr.flush()
400 io.stderr = stderr
401 nt.assert_true('WARNING' in pipe.getvalue())
402 env.pop('IPYTHON_DIR', None)
403 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now