Skip to content

Commit

Permalink
Per dtcenter/METplus-Internal#20, fixed bug introduced from commit 45…
Browse files Browse the repository at this point in the history
…8714c to develop where error is thrown if username cannot be obtained, which occurs in environment running GHA tests
  • Loading branch information
georgemccabe committed Sep 1, 2022
1 parent 458714c commit fb2d093
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions metplus/util/met_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,11 @@ def pre_run_setup(config_inputs):

logger = config.logger

try:
uid = f'as user {os.getlogin()}({os.getuid()})'
except AttributeError:
uid = f'as user {os.getlogin()}'

user_info = get_user_info()
user_string = f' as user {user_info} ' if user_info else ' '
config.set('config', 'METPLUS_VERSION', version_number)
logger.info('Running METplus v%s %s with command: %s',
version_number, uid, ' '.join(sys.argv))
logger.info('Running METplus v%s%swith command: %s',
version_number, user_string, ' '.join(sys.argv))

logger.info(f"Log file: {config.getstr('config', 'LOG_METPLUS')}")
logger.info(f"METplus Base: {config.getdir('METPLUS_BASE')}")
Expand Down Expand Up @@ -202,17 +199,15 @@ def post_run_cleanup(config, app_name, total_errors):
total_run_time = end_clock_time - start_clock_time
logger.debug(f"{app_name} took {total_run_time} to run.")

try:
uid = f'as user {os.getlogin()}({os.getuid()})'
except AttributeError:
uid = f'as user {os.getlogin()}'

user_info = get_user_info()
user_string = f' as user {user_info}' if user_info else ''
if not total_errors:
logger.info(log_message)
logger.info('%s has successfully finished running %s.', app_name, uid)
logger.info('%s has successfully finished running%s.',
app_name, user_string)
return

error_msg = (f'{app_name} has finished running {uid} '
error_msg = (f'{app_name} has finished running{user_string} '
f'but had {total_errors} error')
if total_errors > 1:
error_msg += 's'
Expand All @@ -221,6 +216,35 @@ def post_run_cleanup(config, app_name, total_errors):
logger.info(log_message)
sys.exit(1)

def get_user_info():
"""! Get user information from OS. Note that some OS cannot obtain user ID
and some cannot obtain username.
@returns username(uid) if both username and user ID can be read,
username if only username can be read, uid if only user ID can be read,
or an empty string if neither can be read.
"""
try:
username = os.getlogin()
except FileNotFoundError:
username = None

try:
uid = os.getuid()
except AttributeError:
uid = None

if username and uid:
return f'{username}({uid})'

if username:
return username

if uid:
return uid

return ''

def write_all_commands(all_commands, config):
"""! Write all commands that were run to a file in the log
directory. This includes the environment variables that
Expand Down

0 comments on commit fb2d093

Please sign in to comment.