mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-05-30 22:34:24 +00:00
On Windows, the user's home directory may be either %USERPROFILE% or %HOMEDRIVE%%HOMEPATH% depending on the Windows version. fixes ticket:2417
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
This commit is contained in:
parent
f973657a6a
commit
4794666df6
@ -539,10 +539,11 @@ class FileUtil(ReallyEqualMixin, unittest.TestCase):
|
|||||||
_cleanup()
|
_cleanup()
|
||||||
self.failIf(os.path.exists(long_path))
|
self.failIf(os.path.exists(long_path))
|
||||||
|
|
||||||
def test_windows_expanduser(self):
|
def _test_windows_expanduser(self, userprofile=None, homedrive=None, homepath=None):
|
||||||
def call_windows_getenv(name):
|
def call_windows_getenv(name):
|
||||||
if name == u"HOMEDRIVE": return u"C:"
|
if name == u"USERPROFILE": return userprofile
|
||||||
if name == u"HOMEPATH": return u"\\Documents and Settings\\\u0100"
|
if name == u"HOMEDRIVE": return homedrive
|
||||||
|
if name == u"HOMEPATH": return homepath
|
||||||
self.fail("unexpected argument to call_windows_getenv")
|
self.fail("unexpected argument to call_windows_getenv")
|
||||||
self.patch(fileutil, 'windows_getenv', call_windows_getenv)
|
self.patch(fileutil, 'windows_getenv', call_windows_getenv)
|
||||||
|
|
||||||
@ -553,6 +554,12 @@ class FileUtil(ReallyEqualMixin, unittest.TestCase):
|
|||||||
self.failUnlessReallyEqual(fileutil.windows_expanduser(u"a~"), u"a~")
|
self.failUnlessReallyEqual(fileutil.windows_expanduser(u"a~"), u"a~")
|
||||||
self.failUnlessReallyEqual(fileutil.windows_expanduser(u"a\\~\\foo"), u"a\\~\\foo")
|
self.failUnlessReallyEqual(fileutil.windows_expanduser(u"a\\~\\foo"), u"a\\~\\foo")
|
||||||
|
|
||||||
|
def test_windows_expanduser_xp(self):
|
||||||
|
return self._test_windows_expanduser(homedrive=u"C:", homepath=u"\\Documents and Settings\\\u0100")
|
||||||
|
|
||||||
|
def test_windows_expanduser_win7(self):
|
||||||
|
return self._test_windows_expanduser(userprofile=os.path.join(u"C:", u"\\Documents and Settings\\\u0100"))
|
||||||
|
|
||||||
def test_disk_stats(self):
|
def test_disk_stats(self):
|
||||||
avail = fileutil.get_available_space('.', 2**14)
|
avail = fileutil.get_available_space('.', 2**14)
|
||||||
if avail == 0:
|
if avail == 0:
|
||||||
|
@ -396,18 +396,28 @@ def expanduser(path):
|
|||||||
def windows_expanduser(path):
|
def windows_expanduser(path):
|
||||||
if not path.startswith('~'):
|
if not path.startswith('~'):
|
||||||
return path
|
return path
|
||||||
home_drive = windows_getenv(u'HOMEDRIVE')
|
|
||||||
home_path = windows_getenv(u'HOMEPATH')
|
home_dir = windows_getenv(u'USERPROFILE')
|
||||||
|
if home_dir is None:
|
||||||
|
home_drive = windows_getenv(u'HOMEDRIVE')
|
||||||
|
home_path = windows_getenv(u'HOMEPATH')
|
||||||
|
if home_drive is None or home_path is None:
|
||||||
|
raise OSError("Could not find home directory: neither %USERPROFILE% nor (%HOMEDRIVE% and %HOMEPATH%) are set.")
|
||||||
|
home_dir = os.path.join(home_drive, home_path)
|
||||||
|
|
||||||
if path == '~':
|
if path == '~':
|
||||||
return os.path.join(home_drive, home_path)
|
return home_dir
|
||||||
elif path.startswith('~/') or path.startswith('~\\'):
|
elif path.startswith('~/') or path.startswith('~\\'):
|
||||||
return os.path.join(home_drive, home_path, path[2 :])
|
return os.path.join(home_dir, path[2 :])
|
||||||
else:
|
else:
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
# <https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx>
|
||||||
|
ERROR_ENVVAR_NOT_FOUND = 203
|
||||||
|
|
||||||
def windows_getenv(name):
|
def windows_getenv(name):
|
||||||
# Based on <http://stackoverflow.com/questions/2608200/problems-with-umlauts-in-python-appdata-environvent-variable/2608368#2608368>,
|
# Based on <http://stackoverflow.com/questions/2608200/problems-with-umlauts-in-python-appdata-environvent-variable/2608368#2608368>,
|
||||||
# with improved error handling.
|
# with improved error handling. Returns None if there is no enivronment variable of the given name.
|
||||||
if not isinstance(name, unicode):
|
if not isinstance(name, unicode):
|
||||||
raise AssertionError("name must be Unicode")
|
raise AssertionError("name must be Unicode")
|
||||||
|
|
||||||
@ -415,6 +425,8 @@ def windows_getenv(name):
|
|||||||
# GetEnvironmentVariableW returns DWORD, so n cannot be negative.
|
# GetEnvironmentVariableW returns DWORD, so n cannot be negative.
|
||||||
if n == 0:
|
if n == 0:
|
||||||
err = GetLastError()
|
err = GetLastError()
|
||||||
|
if err == ERROR_ENVVAR_NOT_FOUND:
|
||||||
|
return None
|
||||||
raise OSError("Windows error %d attempting to read size of environment variable %r"
|
raise OSError("Windows error %d attempting to read size of environment variable %r"
|
||||||
% (err, name))
|
% (err, name))
|
||||||
if n == 1:
|
if n == 1:
|
||||||
@ -426,6 +438,8 @@ def windows_getenv(name):
|
|||||||
retval = GetEnvironmentVariableW(name, buf, n)
|
retval = GetEnvironmentVariableW(name, buf, n)
|
||||||
if retval == 0:
|
if retval == 0:
|
||||||
err = GetLastError()
|
err = GetLastError()
|
||||||
|
if err == ERROR_ENVVAR_NOT_FOUND:
|
||||||
|
return None
|
||||||
raise OSError("Windows error %d attempting to read environment variable %r"
|
raise OSError("Windows error %d attempting to read environment variable %r"
|
||||||
% (err, name))
|
% (err, name))
|
||||||
if retval >= n:
|
if retval >= n:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user