feat: add util.get_home_path() function

This commit is contained in:
Lance Edgar 2024-11-23 11:56:30 -06:00
parent 26774bbcaf
commit d3bbc01e7a
2 changed files with 26 additions and 0 deletions

View file

@ -37,6 +37,23 @@ def exists(c, path):
return not c.run(f'test -e {path}', warn=True).failed 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): def is_symlink(c, path):
""" """
Check if the given path is a symlink. Check if the given path is a symlink.

View file

@ -15,6 +15,15 @@ class TestExists(TestCase):
c.run.assert_called_once_with('test -e /foo', warn=True) 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): class TestIsSymlink(TestCase):
def test_yes(self): def test_yes(self):