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