##// END OF EJS Templates
allow IPython to run without writable home dir...
MinRK -
Show More
@@ -552,10 +552,7 b' class InteractiveShell(SingletonConfigurable, Magic):'
552 552
553 553 def init_pushd_popd_magic(self):
554 554 # for pushd/popd management
555 try:
556 self.home_dir = get_home_dir()
557 except HomeDirError, msg:
558 fatal(msg)
555 self.home_dir = get_home_dir()
559 556
560 557 self.dir_stack = []
561 558
@@ -1751,12 +1748,10 b' class InteractiveShell(SingletonConfigurable, Magic):'
1751 1748 # Or if libedit is used, load editrc.
1752 1749 inputrc_name = os.environ.get('INPUTRC')
1753 1750 if inputrc_name is None:
1754 home_dir = get_home_dir()
1755 if home_dir is not None:
1756 inputrc_name = '.inputrc'
1757 if readline.uses_libedit:
1758 inputrc_name = '.editrc'
1759 inputrc_name = os.path.join(home_dir, inputrc_name)
1751 inputrc_name = '.inputrc'
1752 if readline.uses_libedit:
1753 inputrc_name = '.editrc'
1754 inputrc_name = os.path.join(self.home_dir, inputrc_name)
1760 1755 if os.path.isfile(inputrc_name):
1761 1756 try:
1762 1757 readline.read_init_file(inputrc_name)
@@ -167,15 +167,24 b' class HomeDirError(Exception):'
167 167 pass
168 168
169 169
170 def get_home_dir():
171 """Return the closest possible equivalent to a 'home' directory.
170 def get_home_dir(require_writable=False):
171 """Return the 'home' directory, as a unicode string.
172 172
173 173 * First, check for frozen env in case of py2exe
174 * Otherwise, defer to os.path.expanduser('~'), ensuring unicode
174 * Otherwise, defer to os.path.expanduser('~')
175 175
176 176 See stdlib docs for how this is determined.
177
178 177 $HOME is first priority on *ALL* platforms.
178
179 Parameters
180 ----------
181
182 require_writable : bool [default: False]
183 if True:
184 guarantees the return value is a writable directory, otherwise
185 raises HomeDirError
186 if False:
187 The path is resolved, but it is not guaranteed to exist or be writable.
179 188 """
180 189
181 190 # first, check py2exe distribution root directory for _ipython.
@@ -192,10 +201,10 b' def get_home_dir():'
192 201 return py3compat.cast_unicode(root, fs_encoding)
193 202
194 203 homedir = os.path.expanduser('~')
195 if _writable_dir(homedir):
204 if (not require_writable) or _writable_dir(homedir):
196 205 return py3compat.cast_unicode(homedir, fs_encoding)
197 206 else:
198 raise HomeDirError('%s not a writable dir, set $HOME env to override' % homedir)
207 raise HomeDirError('%s is not a writable dir, set $HOME env to override' % homedir)
199 208
200 209 def get_xdg_dir():
201 210 """Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
@@ -207,7 +216,7 b' def get_xdg_dir():'
207 216
208 217 if os.name == 'posix':
209 218 # Linux, Unix, AIX, OS X
210 # use ~/.config if not set OR empty
219 # use ~/.config if empty OR not set
211 220 xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
212 221 if xdg and _writable_dir(xdg):
213 222 return py3compat.cast_unicode(xdg, fs_encoding)
@@ -231,6 +240,7 b' def get_ipython_dir():'
231 240
232 241 home_dir = get_home_dir()
233 242 xdg_dir = get_xdg_dir()
243
234 244 # import pdb; pdb.set_trace() # dbg
235 245 ipdir = env.get('IPYTHON_DIR', env.get('IPYTHONDIR', None))
236 246 if ipdir is None:
@@ -141,7 +141,7 b' def test_get_home_dir_2():'
141 141 #fake filename for IPython.__init__
142 142 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
143 143
144 home_dir = path.get_home_dir()
144 home_dir = path.get_home_dir(True)
145 145 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR).lower())
146 146
147 147
@@ -149,7 +149,7 b' def test_get_home_dir_2():'
149 149 def test_get_home_dir_3():
150 150 """get_home_dir() uses $HOME if set"""
151 151 env["HOME"] = HOME_TEST_DIR
152 home_dir = path.get_home_dir()
152 home_dir = path.get_home_dir(True)
153 153 nt.assert_equal(home_dir, env["HOME"])
154 154
155 155
@@ -159,14 +159,14 b' def test_get_home_dir_4():'
159 159
160 160 if 'HOME' in env: del env['HOME']
161 161 # this should still succeed, but we don't know what the answer should be
162 home = path.get_home_dir()
162 home = path.get_home_dir(True)
163 163 nt.assert_true(path._writable_dir(home))
164 164
165 165 @with_environment
166 166 def test_get_home_dir_5():
167 167 """raise HomeDirError if $HOME is specified, but not a writable dir"""
168 168 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
169 nt.assert_raises(path.HomeDirError, path.get_home_dir)
169 nt.assert_raises(path.HomeDirError, path.get_home_dir, True)
170 170
171 171 @with_environment
172 172 def test_get_ipython_dir_1():
General Comments 0
You need to be logged in to leave comments. Login now