Merge lp:~gz/bzr/get_home_dir into lp:bzr

Proposed by Martin Packman
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merged at revision: 6459
Proposed branch: lp:~gz/bzr/get_home_dir
Merge into: lp:bzr
Diff against target: 103 lines (+68/-0)
2 files modified
bzrlib/osutils.py (+11/-0)
bzrlib/tests/test_osutils.py (+57/-0)
To merge this branch: bzr merge lp:~gz/bzr/get_home_dir
Reviewer Review Type Date Requested Status
Jelmer Vernooij (community) Approve
Review via email: mp+91073@code.launchpad.net

Commit message

Add osutils._get_home_dir function

Description of the change

Add a helper in osutils to get the decoded value of the user's home directory. On nix systems this will pretty much always be ascii anyway, but can avoid some platform detection at higher levels.

What the home dir should actually be used for is another question, see bug 240550 for windows and various XDG mutterings.

So, use of this function is mostly a sign of code doing things wrong, hence the underscore, but it's the shortest path to fixing some encoding issues without needing to worry about relocating things just yet.

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) wrote :

On Wed, Feb 01, 2012 at 01:36:36PM -0000, Martin Packman wrote:
> Martin Packman has proposed merging lp:~gz/bzr/get_home_dir into lp:bzr.
>
> Requested reviews:
> bzr-core (bzr-core)
>
> For more details, see:
> https://code.launchpad.net/~gz/bzr/get_home_dir/+merge/91073
>
> Add a helper in osutils to get the decoded value of the user's home directory. On nix systems this will pretty much always be ascii anyway, but can avoid some platform detection at higher levels.
>
> What the home dir should actually be used for is another question, see bug 240550 for windows and various XDG mutterings.
>
> So, use of this function is mostly a sign of code doing things wrong, hence the underscore, but it's the shortest path to fixing some encoding issues without needing to worry about relocating things just yet.

  review approve
  merge approved

Cheers,

Jelmer

review: Approve
Revision history for this message
Martin Packman (gz) wrote :

sent to pqm by email

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bzrlib/osutils.py'
2--- bzrlib/osutils.py 2012-01-05 10:44:12 +0000
3+++ bzrlib/osutils.py 2012-02-01 13:35:13 +0000
4@@ -342,6 +342,15 @@
5 raise errors.BadFilenameEncoding(val, _fs_enc)
6
7
8+def _posix_get_home_dir():
9+ """Get the home directory of the current user as a unicode path"""
10+ path = posixpath.expanduser("~")
11+ try:
12+ return path.decode(_fs_enc)
13+ except UnicodeDecodeError:
14+ raise errors.BadFilenameEncoding(path, _fs_enc)
15+
16+
17 def _posix_getuser_unicode():
18 """Get username from environment or password database as unicode"""
19 name = getpass.getuser()
20@@ -448,6 +457,7 @@
21 pathjoin = os.path.join
22 normpath = _posix_normpath
23 path_from_environ = _posix_path_from_environ
24+_get_home_dir = _posix_get_home_dir
25 getuser_unicode = _posix_getuser_unicode
26 getcwd = os.getcwdu
27 rename = os.rename
28@@ -511,6 +521,7 @@
29 if f is not None:
30 get_unicode_argv = f
31 path_from_environ = win32utils.get_environ_unicode
32+ _get_home_dir = win32utils.get_home_location
33 getuser_unicode = win32utils.get_user_name
34
35 elif sys.platform == 'darwin':
36
37=== modified file 'bzrlib/tests/test_osutils.py'
38--- bzrlib/tests/test_osutils.py 2012-01-04 11:35:56 +0000
39+++ bzrlib/tests/test_osutils.py 2012-02-01 13:35:13 +0000
40@@ -2095,6 +2095,63 @@
41 self.assertEquals(self.gid, s.st_gid)
42
43
44+class TestPathFromEnviron(tests.TestCase):
45+
46+ def test_is_unicode(self):
47+ self.overrideEnv('BZR_TEST_PATH', './anywhere at all/')
48+ path = osutils.path_from_environ('BZR_TEST_PATH')
49+ self.assertIsInstance(path, unicode)
50+ self.assertEqual(u'./anywhere at all/', path)
51+
52+ def test_posix_path_env_ascii(self):
53+ self.overrideEnv('BZR_TEST_PATH', '/tmp')
54+ home = osutils._posix_path_from_environ('BZR_TEST_PATH')
55+ self.assertIsInstance(home, unicode)
56+ self.assertEqual(u'/tmp', home)
57+
58+ def test_posix_path_env_unicode(self):
59+ self.requireFeature(features.ByteStringNamedFilesystem)
60+ self.overrideEnv('BZR_TEST_PATH', '/home/\xa7test')
61+ self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
62+ self.assertEqual(u'/home/\xa7test',
63+ osutils._posix_path_from_environ('BZR_TEST_PATH'))
64+ osutils._fs_enc = "iso8859-5"
65+ self.assertEqual(u'/home/\u0407test',
66+ osutils._posix_path_from_environ('BZR_TEST_PATH'))
67+ osutils._fs_enc = "utf-8"
68+ self.assertRaises(errors.BadFilenameEncoding,
69+ osutils._posix_path_from_environ, 'BZR_TEST_PATH')
70+
71+
72+class TestGetHomeDir(tests.TestCase):
73+
74+ def test_is_unicode(self):
75+ home = osutils._get_home_dir()
76+ self.assertIsInstance(home, unicode)
77+
78+ def test_posix_homeless(self):
79+ self.overrideEnv('HOME', None)
80+ home = osutils._get_home_dir()
81+ self.assertIsInstance(home, unicode)
82+
83+ def test_posix_home_ascii(self):
84+ self.overrideEnv('HOME', '/home/test')
85+ home = osutils._posix_get_home_dir()
86+ self.assertIsInstance(home, unicode)
87+ self.assertEqual(u'/home/test', home)
88+
89+ def test_posix_home_unicode(self):
90+ self.requireFeature(features.ByteStringNamedFilesystem)
91+ self.overrideEnv('HOME', '/home/\xa7test')
92+ self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
93+ self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
94+ osutils._fs_enc = "iso8859-5"
95+ self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
96+ osutils._fs_enc = "utf-8"
97+ self.assertRaises(errors.BadFilenameEncoding,
98+ osutils._posix_get_home_dir)
99+
100+
101 class TestGetuserUnicode(tests.TestCase):
102
103 def test_is_unicode(self):