From 28b2fb89784f7a1f2dd61a4d157f6c511c5587fe Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 14 Apr 2011 14:13:36 -0400 Subject: Add libbe.ui.util.user.get_fallback_fullname() and use pwd when possible. This patch is based on Julien Muchembled's pwd suggestions. --- libbe/ui/util/user.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'libbe/ui/util/user.py') diff --git a/libbe/ui/util/user.py b/libbe/ui/util/user.py index 35665e4..f2c7511 100644 --- a/libbe/ui/util/user.py +++ b/libbe/ui/util/user.py @@ -28,24 +28,46 @@ are human-readable tags refering to objects. try: from email.utils import formataddr, parseaddr -except ImportErrror: # adjust to old python < 2.5 +except ImportErrror: # adjust to old python < 2.5 from email.Utils import formataddr, parseaddr import os +try: + import pwd +except ImportError: # handle non-Unix systems + pwd = None import re from socket import gethostname import libbe import libbe.storage.util.config + def get_fallback_username(): """Return a username extracted from environmental variables. """ name = None - for env in ["LOGNAME", "USERNAME"]: + for env in ['LOGNAME', 'USERNAME']: + if os.environ.has_key(env): + name = os.environ[env] + break + if name is None and pwd: + pw_ent = pwd.getpwuid(os.getuid()) + name = pw_ent.pw_name + assert name is not None + return name + +def get_fallback_fullname(): + """Return a full name extracted from environmental variables. + """ + name = None + for env in ['FULLNAME']: if os.environ.has_key(env): name = os.environ[env] break - assert name != None + if name is None and pwd: + pw_ent = pwd.getpwuid(os.getuid()) + name = pw_ent.pw_gecos.split(',', 1)[0] + assert name is not None return name def get_fallback_email(): @@ -122,7 +144,7 @@ def get_user_id(storage=None): user = storage.get_user_id() if user != None: return user - name = get_fallback_username() + name = get_fallback_fullname() email = get_fallback_email() user = create_user_id(name, email) return user -- cgit