On Sun, Apr 10, 2011 at 08:28:14PM +0200, Julien Muchembled wrote:
> +import pwd

Unfortunately the pwd module is Unix-only, and BE tries to be
platform-agnostic.

> @@ -40,20 +41,14 @@ import libbe.storage.util.config
>  def get_fallback_username():
>      """Return a username extracted from environmental variables.
>      """
> -    name = None
> -    for env in ["LOGNAME", "USERNAME"]:
> -        if os.environ.has_key(env):
> -            name = os.environ[env]
> -            break
> -    assert name != None
> -    return name
> +    pw_ent = pwd.getpwuid(os.getuid())
> +    return pw_ent[4].split(',', 1)[0] or pw_ent[0]

I approve of not depending on environmental variables when possible,
but it's good to be able to use them to override stuff, so I'm against
throwing out the environmental variables entirely.

>  def get_fallback_email():
>      """Return an email address extracted from environmental variables.
>      """
> -    hostname = gethostname()
> -    name = get_fallback_username()
> -    return "%s@%s" % (name, hostname)
> +    return os.getenv('EMAIL') or "%s@%s" % (pwd.getpwuid(os.getuid())[0],
> +                                            gethostname())

This looks good, but I think we should split out the
fullname-detection as its own function, and stick with
`get_fallback_username()` here rather than repeating some of its logic
with `pwd.getpwuid(os.getuid())[0]`.

I've pushed (and attached) my take on this:
  
http://www.physics.drexel.edu/~wking/code/git/gitweb.cgi?p=be.git;a=commitdiff;h=28b2fb89784f7a1f2dd61a4d157f6c511c5587fe

Out of curiosity, was there some sort of issue that inspired this patch?

-- 
This email may be signed or encrypted with GPG (http://www.gnupg.org).
The GPG signature (if present) will be attached as 'signature.asc'.
For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy

My public key is at http://www.physics.drexel.edu/~wking/pubkey.txt
From 28b2fb89784f7a1f2dd61a4d157f6c511c5587fe Mon Sep 17 00:00:00 2001
From: W. Trevor King <[email protected]>
Date: Thu, 14 Apr 2011 14:13:36 -0400
Subject: [PATCH] Add libbe.ui.util.user.get_fallback_fullname() and use pwd 
when possible.

This patch is based on Julien Muchembled's pwd suggestions.
---
 libbe/storage/vcs/base.py |    2 +-
 libbe/storage/vcs/git.py  |    2 +-
 libbe/ui/util/user.py     |   30 ++++++++++++++++++++++++++----
 3 files changed, 28 insertions(+), 6 deletions(-)

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
-- 
1.7.3.GIT

Attachment: pgpecNoKmqj8e.pgp
Description: PGP signature

_______________________________________________
Be-devel mailing list
[email protected]
http://void.printf.net/cgi-bin/mailman/listinfo/be-devel

Reply via email to