diff --git a/src/wuttamess/util.py b/src/wuttamess/util.py index 619a34e..2fde609 100644 --- a/src/wuttamess/util.py +++ b/src/wuttamess/util.py @@ -37,6 +37,23 @@ def exists(c, path): return not c.run(f'test -e {path}', warn=True).failed +def get_home_path(c, user=None): + """ + Get the path to user's home folder on target machine. + + :param c: Fabric connection. + + :param user: Username whose home folder you want. If not + specified, the username for the current connection is assumed. + + :returns: Home folder path as string. + """ + user = user or c.user + home = c.run(f'getent passwd {user} | cut -d: -f6').stdout.strip() + home = home.rstrip('/') + return home + + def is_symlink(c, path): """ Check if the given path is a symlink. diff --git a/tests/test_util.py b/tests/test_util.py index c85b0de..3793f93 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -15,6 +15,15 @@ class TestExists(TestCase): c.run.assert_called_once_with('test -e /foo', warn=True) +class TestHomePath(TestCase): + + def test_basic(self): + c = MagicMock() + c.run.return_value.stdout = '/home/foo' + path = mod.get_home_path(c, user='foo') + self.assertEqual(path, '/home/foo') + + class TestIsSymlink(TestCase): def test_yes(self):