Skip to content

Commit b998a18

Browse files
committed
🚀 add new utils to assert user owns file
1 parent 2a237cd commit b998a18

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

‎isabl_cli/utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import os
66
import sys
77
import tarfile
8+
from getpass import getuser
9+
from os import stat
10+
from pwd import getpwuid
811

912
import click
1013

@@ -208,3 +211,18 @@ def echo_title(title, color="cyan", blink=False):
208211
title = "\n" + title.strip().upper() + "\n"
209212
title += "".join("-" for i in title.strip()) + "\n"
210213
click.secho(title, fg=color, file=sys.stderr, blink=blink)
214+
215+
216+
def find_owner(filename):
217+
"""Find directory owner."""
218+
return getpwuid(stat(filename).st_uid).pw_name
219+
220+
221+
def assert_same_owner(path):
222+
"""Validate that a path is owned by the same user."""
223+
try:
224+
assert find_owner(path) == getuser(), f"{path} must be owned by {getuser()}"
225+
except AssertionError as error:
226+
raise click.UsageError(str(error))
227+
except FileNotFoundError:
228+
pass

‎tests/test_utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""isabl_cli utils tests."""
22

3+
from getpass import getuser
34
from os.path import join
45
import os
56
import tarfile
@@ -10,6 +11,11 @@
1011
from isabl_cli.settings import _DEFAULTS
1112

1213

14+
def test_find_user(tmpdir):
15+
assert getuser() == utils.find_owner(str(tmpdir))
16+
assert utils.assert_same_owner(str(tmpdir)) is None
17+
18+
1319
def test_force_symlink(tmpdir):
1420
src = join(str(tmpdir), "src")
1521
dst = join(str(tmpdir), "dst")

0 commit comments

Comments
 (0)