From 97fe7ad32bcdab65beb080843c9eb7bbeae9cb51 Mon Sep 17 00:00:00 2001 From: Greg Becker Date: Tue, 8 Nov 2022 10:36:10 -0800 Subject: use pwd for usernames on unix (#19980) --- lib/spack/spack/util/path.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/spack/spack/util/path.py b/lib/spack/spack/util/path.py index c378f1eb2d..d18d719c8f 100644 --- a/lib/spack/spack/util/path.py +++ b/lib/spack/spack/util/path.py @@ -39,6 +39,18 @@ def architecture(): return spack.spec.ArchSpec((str(host_platform), str(host_os), str(host_target))) +def get_user(): + # User pwd where available because it accounts for effective uids when using ksu and similar + try: + # user pwd for unix systems + import pwd + + return pwd.getpwuid(os.geteuid()).pw_name + except ImportError: + # fallback on getpass + return getpass.getuser() + + # Substitutions to perform def replacements(): # break circular import from spack.util.executable @@ -48,7 +60,7 @@ def replacements(): return { "spack": spack.paths.prefix, - "user": getpass.getuser(), + "user": get_user(), "tempdir": tempfile.gettempdir(), "user_cache_path": spack.paths.user_cache_path, "architecture": str(arch), -- cgit v1.2.3-60-g2f50