Show More
@@ -244,6 +244,24 b' def get_xdg_dir():' | |||
|
244 | 244 | return None |
|
245 | 245 | |
|
246 | 246 | |
|
247 | def get_xdg_cache_dir(): | |
|
248 | """Return the XDG_CACHE_HOME, if it is defined and exists, else None. | |
|
249 | ||
|
250 | This is only for non-OS X posix (Linux,Unix,etc.) systems. | |
|
251 | """ | |
|
252 | ||
|
253 | env = os.environ | |
|
254 | ||
|
255 | if os.name == 'posix' and sys.platform != 'darwin': | |
|
256 | # Linux, Unix, AIX, etc. | |
|
257 | # use ~/.cache if empty OR not set | |
|
258 | xdg = env.get("XDG_CACHE_HOME", None) or os.path.join(get_home_dir(), '.cache') | |
|
259 | if xdg and _writable_dir(xdg): | |
|
260 | return py3compat.cast_unicode(xdg, fs_encoding) | |
|
261 | ||
|
262 | return None | |
|
263 | ||
|
264 | ||
|
247 | 265 | def get_ipython_dir(): |
|
248 | 266 | """Get the IPython directory for this platform and user. |
|
249 | 267 | |
@@ -300,6 +318,20 b' def get_ipython_dir():' | |||
|
300 | 318 | return py3compat.cast_unicode(ipdir, fs_encoding) |
|
301 | 319 | |
|
302 | 320 | |
|
321 | def get_ipython_cache_dir(): | |
|
322 | """Get the cache directory it is created if it does not exist.""" | |
|
323 | xdgdir = get_xdg_cache_dir() | |
|
324 | if xdgdir is None: | |
|
325 | return get_ipython_dir() | |
|
326 | ipdir = os.path.join(xdgdir, "ipython") | |
|
327 | if not os.path.exists(ipdir) and _writable_dir(xdgdir): | |
|
328 | os.makedirs(ipdir) | |
|
329 | elif not _writable_dir(xdgdir): | |
|
330 | return get_ipython_dir() | |
|
331 | ||
|
332 | return py3compat.cast_unicode(ipdir, fs_encoding) | |
|
333 | ||
|
334 | ||
|
303 | 335 | def get_ipython_package_dir(): |
|
304 | 336 | """Get the base directory where IPython itself is installed.""" |
|
305 | 337 | ipdir = os.path.dirname(IPython.__file__) |
@@ -60,6 +60,7 b' TEST_FILE_PATH = split(abspath(__file__))[0]' | |||
|
60 | 60 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
61 | 61 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") |
|
62 | 62 | XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir") |
|
63 | XDG_CACHE_DIR = join(HOME_TEST_DIR, "xdg_cache_dir") | |
|
63 | 64 | IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython') |
|
64 | 65 | # |
|
65 | 66 | # Setup/teardown functions/decorators |
@@ -74,6 +75,7 b' def setup():' | |||
|
74 | 75 | # problem because that exception is only defined on Windows... |
|
75 | 76 | os.makedirs(IP_TEST_DIR) |
|
76 | 77 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
78 | os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython')) | |
|
77 | 79 | |
|
78 | 80 | |
|
79 | 81 | def teardown(): |
@@ -361,6 +363,26 b' def test_filefind():' | |||
|
361 | 363 | t = path.filefind(f.name, alt_dirs) |
|
362 | 364 | # print 'found:',t |
|
363 | 365 | |
|
366 | @with_environment | |
|
367 | def test_get_ipython_cache_dir(): | |
|
368 | os.environ["HOME"] = HOME_TEST_DIR | |
|
369 | if os.name == 'posix' and sys.platform != 'darwin': | |
|
370 | # test default | |
|
371 | os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) | |
|
372 | os.environ.pop("XDG_CACHE_HOME", None) | |
|
373 | ipdir = path.get_ipython_cache_dir() | |
|
374 | nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), | |
|
375 | ipdir) | |
|
376 | nt.assert_true(os.path.isdir(ipdir)) | |
|
377 | ||
|
378 | # test env override | |
|
379 | os.environ["XDG_CACHE_HOME"] = XDG_CACHE_DIR | |
|
380 | ipdir = path.get_ipython_cache_dir() | |
|
381 | nt.assert_true(os.path.isdir(ipdir)) | |
|
382 | nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) | |
|
383 | else: | |
|
384 | nt.assert_equal(path.get_ipython_cache_dir(), | |
|
385 | path.get_ipython_dir()) | |
|
364 | 386 | |
|
365 | 387 | def test_get_ipython_package_dir(): |
|
366 | 388 | ipdir = path.get_ipython_package_dir() |
General Comments 0
You need to be logged in to leave comments.
Login now