Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cython cache #3184

Merged
merged 2 commits into from Apr 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion IPython/extensions/cythonmagic.py
Expand Up @@ -59,6 +59,7 @@
from IPython.core.magic import Magics, magics_class, cell_magic
from IPython.testing.skipdoctest import skip_doctest
from IPython.utils import py3compat
from IPython.utils.path import get_ipython_cache_dir

import Cython
from Cython.Compiler.Errors import CompileError
Expand Down Expand Up @@ -192,7 +193,7 @@ def f(x):
"""
args = magic_arguments.parse_argstring(self.cython, line)
code = cell if cell.endswith('\n') else cell+'\n'
lib_dir = os.path.join(self.shell.ipython_dir, 'cython')
lib_dir = os.path.join(get_ipython_cache_dir(), 'cython')
quiet = True
key = code, sys.version_info, sys.executable, Cython.__version__

Expand Down
32 changes: 32 additions & 0 deletions IPython/utils/path.py
Expand Up @@ -244,6 +244,24 @@ def get_xdg_dir():
return None


def get_xdg_cache_dir():
"""Return the XDG_CACHE_HOME, if it is defined and exists, else None.

This is only for non-OS X posix (Linux,Unix,etc.) systems.
"""

env = os.environ

if os.name == 'posix' and sys.platform != 'darwin':
# Linux, Unix, AIX, etc.
# use ~/.cache if empty OR not set
xdg = env.get("XDG_CACHE_HOME", None) or os.path.join(get_home_dir(), '.cache')
if xdg and _writable_dir(xdg):
return py3compat.cast_unicode(xdg, fs_encoding)

return None


def get_ipython_dir():
"""Get the IPython directory for this platform and user.

Expand Down Expand Up @@ -300,6 +318,20 @@ def get_ipython_dir():
return py3compat.cast_unicode(ipdir, fs_encoding)


def get_ipython_cache_dir():
"""Get the cache directory it is created if it does not exist."""
xdgdir = get_xdg_cache_dir()
if xdgdir is None:
return get_ipython_dir()
ipdir = os.path.join(xdgdir, "ipython")
if not os.path.exists(ipdir) and _writable_dir(xdgdir):
os.makedirs(ipdir)
elif not _writable_dir(xdgdir):
return get_ipython_dir()

return py3compat.cast_unicode(ipdir, fs_encoding)


def get_ipython_package_dir():
"""Get the base directory where IPython itself is installed."""
ipdir = os.path.dirname(IPython.__file__)
Expand Down
22 changes: 22 additions & 0 deletions IPython/utils/tests/test_path.py
Expand Up @@ -60,6 +60,7 @@
TMP_TEST_DIR = tempfile.mkdtemp()
HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
XDG_TEST_DIR = join(HOME_TEST_DIR, "xdg_test_dir")
XDG_CACHE_DIR = join(HOME_TEST_DIR, "xdg_cache_dir")
IP_TEST_DIR = join(HOME_TEST_DIR,'.ipython')
#
# Setup/teardown functions/decorators
Expand All @@ -74,6 +75,7 @@ def setup():
# problem because that exception is only defined on Windows...
os.makedirs(IP_TEST_DIR)
os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython'))
os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython'))


def teardown():
Expand Down Expand Up @@ -361,6 +363,26 @@ def test_filefind():
t = path.filefind(f.name, alt_dirs)
# print 'found:',t

@with_environment
def test_get_ipython_cache_dir():
os.environ["HOME"] = HOME_TEST_DIR
if os.name == 'posix' and sys.platform != 'darwin':
# test default
os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
os.environ.pop("XDG_CACHE_HOME", None)
ipdir = path.get_ipython_cache_dir()
nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"),
ipdir)
nt.assert_true(os.path.isdir(ipdir))

# test env override
os.environ["XDG_CACHE_HOME"] = XDG_CACHE_DIR
ipdir = path.get_ipython_cache_dir()
nt.assert_true(os.path.isdir(ipdir))
nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython"))
else:
nt.assert_equal(path.get_ipython_cache_dir(),
path.get_ipython_dir())

def test_get_ipython_package_dir():
ipdir = path.get_ipython_package_dir()
Expand Down