From: W. Trevor King Date: Thu, 14 Apr 2011 18:13:36 +0000 (-0400) Subject: Add libbe.ui.util.user.get_fallback_fullname() and use pwd when possible. X-Git-Tag: 1.1.0~231 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=28b2fb89784f7a1f2dd61a4d157f6c511c5587fe;p=be.git Add libbe.ui.util.user.get_fallback_fullname() and use pwd when possible. This patch is based on Julien Muchembled's pwd suggestions. --- diff --git a/libbe/storage/vcs/base.py b/libbe/storage/vcs/base.py index aba6159..3ff6e7e 100644 --- a/libbe/storage/vcs/base.py +++ b/libbe/storage/vcs/base.py @@ -537,7 +537,7 @@ class VCS (libbe.storage.base.VersionedStorage): self.user_id = self._vcs_get_user_id() if self.user_id == None: # guess missing info - name = libbe.ui.util.user.get_fallback_username() + name = libbe.ui.util.user.get_fallback_fullname() email = libbe.ui.util.user.get_fallback_email() self.user_id = libbe.ui.util.user.create_user_id(name, email) return self.user_id diff --git a/libbe/storage/vcs/git.py b/libbe/storage/vcs/git.py index 5c17303..6de1d56 100644 --- a/libbe/storage/vcs/git.py +++ b/libbe/storage/vcs/git.py @@ -74,7 +74,7 @@ class Git(base.VCS): if name != '' or email != '': # got something! # guess missing info, if necessary if name == '': - name = _user.get_fallback_username() + name = _user.get_fallback_fullname() if email == '': email = _user.get_fallback_email() return _user.create_user_id(name, email) 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