Skip to content

Commit a84c340

Browse files
committed
EAMxx: add checks on permissions for input files at buildnml time
1 parent 25120ff commit a84c340

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

components/eamxx/cime_config/eamxx_buildnml.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Used by buildnml. See buildnml for documetation.
55
"""
66

7-
import os, sys, re
7+
import os, sys, re, pwd, grp, stat, getpass
88
from collections import OrderedDict
99

1010
import xml.etree.ElementTree as ET
@@ -953,7 +953,37 @@ def create_input_data_list_file(case,caseroot):
953953
# Only add files whose full path starts with the CIME's input data location
954954
if file_path.startswith(din_loc_root):
955955
fd.write("scream_dl_input_{} = {}\n".format(idx, file_path))
956-
956+
if os.path.exists(file_path):
957+
if os.path.isdir(file_path):
958+
raise IsADirectoryError(f"Input file '{file_path}' is a directory, not a regular file.")
959+
if not os.path.isfile(file_path):
960+
raise OSError(f"Input file '{file_path}' exists but is not a regular file.")
961+
if not os.access(file_path,os.R_OK):
962+
try:
963+
file_stat = os.stat(file_path)
964+
965+
# Get owner and group names
966+
owner = pwd.getpwuid(file_stat.st_uid).pw_name
967+
group = grp.getgrgid(file_stat.st_gid).gr_name
968+
969+
# Get file permissions
970+
permissions = stat.filemode(file_stat.st_mode)
971+
972+
except Exception as e:
973+
raise RuntimeError(f"Error retrieving file info for '{file_path}': {e}")
974+
975+
curr_user = getpass.getuser()
976+
user_info = pwd.getpwnam(curr_user)
977+
group_ids = os.getgrouplist(curr_user, user_info.pw_gid)
978+
curr_groups = [grp.getgrgid(gid).gr_name for gid in group_ids]
979+
980+
raise PermissionError ("Input file exists but it is not readable for current user\n"
981+
f" - file name: {file_path}\n"
982+
f" - file owner: {owner}\n"
983+
f" - file group: {group}\n"
984+
f" - permissions: {permissions}\n"
985+
f" - current user: {curr_user}\n"
986+
f" - current user groups: {curr_groups}")
957987

958988
###############################################################################
959989
def do_cime_vars_on_yaml_output_files(case, caseroot):

0 commit comments

Comments
 (0)