Skip to content

Commit ae10ae6

Browse files
author
efajardo
committed
Adding function to check ownership
1 parent a8bb279 commit ae10ae6

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

osgtest/library/core.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -634,19 +634,25 @@ def get_hostname():
634634
except socket.error:
635635
return None
636636

637+
def check_file_ownership(file_path, owner_name):
638+
"""Return True if at 'file_path' exists, is owned by
639+
'owner_name' and is a is a file
640+
"""
641+
owner_uid = pwd.getpwnam(owner_name)
642+
try:
643+
file_stat = os.stat(file_path)
644+
return (file_stat.st_uid == owner_uid and
645+
stat.S_ISREG(file_stat.st_mode))
646+
except OSError: # file does not exist
647+
return False
637648

638649
def check_file_and_perms(file_path, owner_name, permissions):
639-
"""Return True if the file at 'file_path' exists, is owned by
640-
'owner_name', is a file, and has the given permissions; False otherwise
641-
"""
642-
owner_uid = pwd.getpwnam(owner_name)
643-
try:
644-
file_stat = os.stat(file_path)
645-
return (file_stat.st_uid == owner_uid and
646-
file_stat.st_mode & 0o7777 == permissions and
647-
stat.S_ISREG(file_stat.st_mode))
648-
except OSError: # file does not exist
649-
return False
650+
"""Return True if the file at 'file_path' exists, is owned by
651+
'owner_name', is a file, and has the given permissions; False otherwise
652+
"""
653+
file_stat = os.stat(file_path)
654+
return check_file_ownership(file_path, owner_name) and
655+
file_stat.st_mode & 0o7777 == permissions
650656

651657
def parse_env_output(output):
652658
"""

0 commit comments

Comments
 (0)