From 427a91cc50ebccbfa98c4b95c1a380dc6c6cab89 Mon Sep 17 00:00:00 2001 From: Gilles DAVID Date: Thu, 3 Jun 2021 16:26:15 +0200 Subject: [PATCH] getUserName now uses the standard getpass module --- bin/salome_utils.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/bin/salome_utils.py b/bin/salome_utils.py index cb0b454ac..61aa30203 100644 --- a/bin/salome_utils.py +++ b/bin/salome_utils.py @@ -125,18 +125,21 @@ def getPortFromORBcfg(): def getUserName(): """ Get user name: - 1. try USER environment variable (USERNAME on windows) - 2. if fails, try LOGNAME (un*x) - 3. if fails return 'unknown' as default user name - """ - import os, sys - if sys.platform == "win32": - return os.getenv("USERNAME", "unknown") - else: - user = os.getenv("USER") - if user: - return user - return os.getenv("LOGNAME", "unknown") + Uses the getpass standard module which test the + following variables (in that order): + 1. LOGNAME + 2. USER + 3. LNAME + 4. USERNAME + If none of these variable is set, try with the pwd module + if supported. + Finally raises an exception if nothing is found. + """ + import getpass + try: + return getpass.getuser() + except Exception: + return 'unknown' # --- def getHostName(): -- 2.39.2