##// END OF EJS Templates
defer to stdlib for path.get_home_dir()...
MinRK -
Show More
@@ -170,21 +170,14 b' class HomeDirError(Exception):'
170 def get_home_dir():
170 def get_home_dir():
171 """Return the closest possible equivalent to a 'home' directory.
171 """Return the closest possible equivalent to a 'home' directory.
172
172
173 * On POSIX, we try $HOME.
173 * First, check for frozen env in case of py2exe
174 * On Windows we try:
174 * Otherwise, defer to os.path.expanduser('~'), ensuring unicode
175 - %HOMESHARE%
175
176 - %HOMEDRIVE\%HOMEPATH%
176 See stdlib docs for how this is determined.
177 - %USERPROFILE%
177
178 - Registry hack for My Documents
178 $HOME is first priority on *ALL* platforms.
179 - %HOME%: rare, but some people with unix-like setups may have defined it
180 * On Dos C:\
181
182 Currently only Posix and NT are implemented, a HomeDirError exception is
183 raised for all other OSes.
184 """
179 """
185
180
186 env = os.environ
187
188 # first, check py2exe distribution root directory for _ipython.
181 # first, check py2exe distribution root directory for _ipython.
189 # This overrides all. Normally does not exist.
182 # This overrides all. Normally does not exist.
190
183
@@ -197,90 +190,12 b' def get_home_dir():'
197 if _writable_dir(os.path.join(root, '_ipython')):
190 if _writable_dir(os.path.join(root, '_ipython')):
198 os.environ["IPYKITROOT"] = root
191 os.environ["IPYKITROOT"] = root
199 return py3compat.cast_unicode(root, fs_encoding)
192 return py3compat.cast_unicode(root, fs_encoding)
200
193
201 if os.name == 'posix':
194 homedir = os.path.expanduser('~')
202 # Linux, Unix, AIX, OS X
195 if _writable_dir(homedir):
203 try:
196 return py3compat.cast_unicode(homedir, fs_encoding)
204 homedir = env['HOME']
205 except KeyError:
206 # Last-ditch attempt at finding a suitable $HOME, on systems where
207 # it may not be defined in the environment but the system shell
208 # still knows it - reported once as:
209 # https://github.com/ipython/ipython/issues/154
210 from subprocess import Popen, PIPE
211 homedir = Popen('echo $HOME', shell=True,
212 stdout=PIPE).communicate()[0].strip()
213 if homedir:
214 return py3compat.cast_unicode(homedir, fs_encoding)
215 else:
216 raise HomeDirError('Undefined $HOME, IPython cannot proceed.')
217 else:
218 return py3compat.cast_unicode(homedir, fs_encoding)
219 elif os.name == 'nt':
220 # Now for win9x, XP, Vista, 7?
221 # For some strange reason all of these return 'nt' for os.name.
222 # First look for a network home directory. This will return the UNC
223 # path (\\server\\Users\%username%) not the mapped path (Z:\). This
224 # is needed when running IPython on cluster where all paths have to
225 # be UNC.
226 try:
227 homedir = env['HOMESHARE']
228 except KeyError:
229 pass
230 else:
231 if _writable_dir(homedir):
232 return py3compat.cast_unicode(homedir, fs_encoding)
233
234 # Now look for a local home directory
235 try:
236 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
237 except KeyError:
238 pass
239 else:
240 if _writable_dir(homedir):
241 return py3compat.cast_unicode(homedir, fs_encoding)
242
243 # Now the users profile directory
244 try:
245 homedir = os.path.join(env['USERPROFILE'])
246 except KeyError:
247 pass
248 else:
249 if _writable_dir(homedir):
250 return py3compat.cast_unicode(homedir, fs_encoding)
251
252 # Use the registry to get the 'My Documents' folder.
253 try:
254 import _winreg as wreg
255 key = wreg.OpenKey(
256 wreg.HKEY_CURRENT_USER,
257 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
258 )
259 homedir = wreg.QueryValueEx(key,'Personal')[0]
260 key.Close()
261 except:
262 pass
263 else:
264 if _writable_dir(homedir):
265 return py3compat.cast_unicode(homedir, fs_encoding)
266
267 # A user with a lot of unix tools in win32 may have defined $HOME.
268 # Try this as a last ditch option.
269 try:
270 homedir = env['HOME']
271 except KeyError:
272 pass
273 else:
274 if _writable_dir(homedir):
275 return py3compat.cast_unicode(homedir, fs_encoding)
276
277 # If all else fails, raise HomeDirError
278 raise HomeDirError('No valid home directory could be found')
279 elif os.name == 'dos':
280 # Desperate, may do absurd things in classic MacOS. May work under DOS.
281 return u'C:\\'
282 else:
197 else:
283 raise HomeDirError('No valid home directory could be found for your OS')
198 raise HomeDirError('%s not a writable dir, set $HOME env to override' % homedir)
284
199
285 def get_xdg_dir():
200 def get_xdg_dir():
286 """Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
201 """Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
@@ -118,7 +118,6 b' def teardown_environment():'
118 # Build decorator that uses the setup_environment/setup_environment
118 # Build decorator that uses the setup_environment/setup_environment
119 with_environment = with_setup(setup_environment, teardown_environment)
119 with_environment = with_setup(setup_environment, teardown_environment)
120
120
121
122 @skip_if_not_win32
121 @skip_if_not_win32
123 @with_environment
122 @with_environment
124 def test_get_home_dir_1():
123 def test_get_home_dir_1():
@@ -147,96 +146,27 b' def test_get_home_dir_2():'
147
146
148
147
149 @with_environment
148 @with_environment
150 @skip_win32
151 def test_get_home_dir_3():
149 def test_get_home_dir_3():
152 """Testcase $HOME is set, then use its value as home directory."""
150 """get_home_dir() uses $HOME if set"""
153 env["HOME"] = HOME_TEST_DIR
151 env["HOME"] = HOME_TEST_DIR
154 home_dir = path.get_home_dir()
152 home_dir = path.get_home_dir()
155 nt.assert_equal(home_dir, env["HOME"])
153 nt.assert_equal(home_dir, env["HOME"])
156
154
157
155
158 @with_environment
156 @with_environment
159 @skip_win32
160 def test_get_home_dir_4():
157 def test_get_home_dir_4():
161 """Testcase $HOME is not set, os=='posix'.
158 """get_home_dir() still works if $HOME is not set"""
162 This should fail with HomeDirError"""
163
159
164 os.name = 'posix'
165 if 'HOME' in env: del env['HOME']
160 if 'HOME' in env: del env['HOME']
166 nt.assert_raises(path.HomeDirError, path.get_home_dir)
161 # this should still succeed, but we don't know what the answer should be
167
162 home = path.get_home_dir()
163 nt.assert_true(path._writable_dir(home))
168
164
169 @skip_if_not_win32
170 @with_environment
165 @with_environment
171 def test_get_home_dir_5():
166 def test_get_home_dir_5():
172 """Using HOMEDRIVE + HOMEPATH, os=='nt'.
167 """raise HomeDirError if $HOME is specified, but not a writable dir"""
173
168 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
174 HOMESHARE is missing.
169 nt.assert_raises(path.HomeDirError, path.get_home_dir)
175 """
176
177 os.name = 'nt'
178 env.pop('HOMESHARE', None)
179 env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR)
180 home_dir = path.get_home_dir()
181 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
182
183
184 @skip_if_not_win32
185 @with_environment
186 def test_get_home_dir_6():
187 """Using USERPROFILE, os=='nt'.
188
189 HOMESHARE, HOMEDRIVE, HOMEPATH are missing.
190 """
191
192 os.name = 'nt'
193 env.pop('HOMESHARE', None)
194 env.pop('HOMEDRIVE', None)
195 env.pop('HOMEPATH', None)
196 env["USERPROFILE"] = abspath(HOME_TEST_DIR)
197 home_dir = path.get_home_dir()
198 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
199
200
201 @skip_if_not_win32
202 @with_environment
203 def test_get_home_dir_7():
204 """Using HOMESHARE, os=='nt'."""
205
206 os.name = 'nt'
207 env["HOMESHARE"] = abspath(HOME_TEST_DIR)
208 home_dir = path.get_home_dir()
209 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
210
211
212 # Should we stub wreg fully so we can run the test on all platforms?
213 @skip_if_not_win32
214 @with_environment
215 def test_get_home_dir_8():
216 """Using registry hack for 'My Documents', os=='nt'
217
218 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
219 """
220 os.name = 'nt'
221 # Remove from stub environment all keys that may be set
222 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
223 env.pop(key, None)
224
225 #Stub windows registry functions
226 def OpenKey(x, y):
227 class key:
228 def Close(self):
229 pass
230 return key()
231 def QueryValueEx(x, y):
232 return [abspath(HOME_TEST_DIR)]
233
234 wreg.OpenKey = OpenKey
235 wreg.QueryValueEx = QueryValueEx
236
237 home_dir = path.get_home_dir()
238 nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
239
240
170
241 @with_environment
171 @with_environment
242 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