Permalink
Browse files

allow IPython to run without writable home dir

get_ipython_dir() ensures that the *IPython* dir is writable, which is more relevant, but the home dir need not be writable. Some optional behaviors (e.g. `%logstart global`) will not work if the home dir is not writable, but IPython should not crash.  Approximately no other operations actually depend on writing directly to $HOME.
  • Loading branch information...
1 parent b5ca646 commit db42b5f7fb7ce7f68bfff274a2b6e5d7805daef1 @minrk committed Nov 23, 2011
Showing with 26 additions and 21 deletions.
  1. +5 −10 IPython/core/interactiveshell.py
  2. +17 −7 IPython/utils/path.py
  3. +4 −4 IPython/utils/tests/test_path.py
@@ -552,10 +552,7 @@ def init_syntax_highlighting(self):
def init_pushd_popd_magic(self):
# for pushd/popd management
- try:
- self.home_dir = get_home_dir()
- except HomeDirError, msg:
- fatal(msg)
+ self.home_dir = get_home_dir()
self.dir_stack = []
@@ -1751,12 +1748,10 @@ def init_readline(self):
# Or if libedit is used, load editrc.
inputrc_name = os.environ.get('INPUTRC')
if inputrc_name is None:
- home_dir = get_home_dir()
- if home_dir is not None:
- inputrc_name = '.inputrc'
- if readline.uses_libedit:
- inputrc_name = '.editrc'
- inputrc_name = os.path.join(home_dir, inputrc_name)
+ inputrc_name = '.inputrc'
+ if readline.uses_libedit:
+ inputrc_name = '.editrc'
+ inputrc_name = os.path.join(self.home_dir, inputrc_name)
if os.path.isfile(inputrc_name):
try:
readline.read_init_file(inputrc_name)
View
@@ -167,15 +167,24 @@ class HomeDirError(Exception):
pass
-def get_home_dir():
- """Return the closest possible equivalent to a 'home' directory.
+def get_home_dir(require_writable=False):
+ """Return the 'home' directory, as a unicode string.
* First, check for frozen env in case of py2exe
- * Otherwise, defer to os.path.expanduser('~'), ensuring unicode
+ * Otherwise, defer to os.path.expanduser('~')
See stdlib docs for how this is determined.
-
$HOME is first priority on *ALL* platforms.
+
+ Parameters
+ ----------
+
+ require_writable : bool [default: False]
+ if True:
+ guarantees the return value is a writable directory, otherwise
+ raises HomeDirError
+ if False:
+ The path is resolved, but it is not guaranteed to exist or be writable.
"""
# first, check py2exe distribution root directory for _ipython.
@@ -192,10 +201,10 @@ def get_home_dir():
return py3compat.cast_unicode(root, fs_encoding)
homedir = os.path.expanduser('~')
- if _writable_dir(homedir):
+ if (not require_writable) or _writable_dir(homedir):
return py3compat.cast_unicode(homedir, fs_encoding)
else:
- raise HomeDirError('%s not a writable dir, set $HOME env to override' % homedir)
+ raise HomeDirError('%s is not a writable dir, set $HOME env to override' % homedir)
def get_xdg_dir():
"""Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
@@ -207,7 +216,7 @@ def get_xdg_dir():
if os.name == 'posix':
# Linux, Unix, AIX, OS X
- # use ~/.config if not set OR empty
+ # use ~/.config if empty OR not set
xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
if xdg and _writable_dir(xdg):
return py3compat.cast_unicode(xdg, fs_encoding)
@@ -231,6 +240,7 @@ def get_ipython_dir():
home_dir = get_home_dir()
xdg_dir = get_xdg_dir()
+
# import pdb; pdb.set_trace() # dbg
ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None))
if ipdir is None:
@@ -141,15 +141,15 @@ def test_get_home_dir_2():
#fake filename for IPython.__init__
IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
- home_dir = path.get_home_dir()
+ home_dir = path.get_home_dir(True)
nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
@with_environment
def test_get_home_dir_3():
"""get_home_dir() uses $HOME if set"""
env["HOME"] = HOME_TEST_DIR
- home_dir = path.get_home_dir()
+ home_dir = path.get_home_dir(True)
nt.assert_equal(home_dir, env["HOME"])
@@ -159,14 +159,14 @@ def test_get_home_dir_4():
if 'HOME' in env: del env['HOME']
# this should still succeed, but we don't know what the answer should be
- home = path.get_home_dir()
+ home = path.get_home_dir(True)
nt.assert_true(path._writable_dir(home))
@with_environment
def test_get_home_dir_5():
"""raise HomeDirError if $HOME is specified, but not a writable dir"""
env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
- nt.assert_raises(path.HomeDirError, path.get_home_dir)
+ nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
@with_environment
def test_get_ipython_dir_1():

0 comments on commit db42b5f

Please sign in to comment.