From fb2d0939a421a107461aaa23fde4cc65a0fca9df Mon Sep 17 00:00:00 2001 From: George McCabe <23407799+georgemccabe@users.noreply.github.com> Date: Thu, 1 Sep 2022 09:40:12 -0600 Subject: [PATCH] Per dtcenter/METplus-Internal#20, fixed bug introduced from commit 458714cf to develop where error is thrown if username cannot be obtained, which occurs in environment running GHA tests --- metplus/util/met_util.py | 52 +++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/metplus/util/met_util.py b/metplus/util/met_util.py index c1c2060f35..6bf531dfc4 100644 --- a/metplus/util/met_util.py +++ b/metplus/util/met_util.py @@ -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')}") @@ -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' @@ -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