[Pkg-bazaar-commits] ./bzr/unstable r246: - unicode decoding in getting email and userid strings

mbp at sourcefrog.net mbp at sourcefrog.net
Fri Apr 10 07:51:32 UTC 2009


------------------------------------------------------------
revno: 246
committer: mbp at sourcefrog.net
timestamp: Wed 2005-04-13 14:43:23 +1000
message:
  - unicode decoding in getting email and userid strings
modified:
  bzrlib/osutils.py
-------------- next part --------------
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2005-04-06 14:06:32 +0000
+++ b/bzrlib/osutils.py	2005-04-13 04:43:23 +0000
@@ -128,56 +128,77 @@
             'sha1': s.hexdigest()}
 
 
-
-def username():
-    """Return email-style username.
-
-    Something similar to 'Martin Pool <mbp at sourcefrog.net>'
-
-    :todo: Check it's reasonably well-formed.
-
-    :todo: Allow taking it from a dotfile to help people on windows
-           who can't easily set variables.
-
-    :todo: Cope without pwd module, which is only on unix. 
+def auto_user_id():
+    """Calculate automatic user identification.
+
+    Returns (realname, email).
+
+    Only used when none is set in the environment.
+
     """
-    e = os.environ.get('BZREMAIL') or os.environ.get('EMAIL')
-    if e: return e
-
-    import socket
+    import socket, locale
+
+    # XXX: Any good way to get real user name on win32?
+
+    # XXX: can the FQDN be non-ascii?
+
+    enc = locale.getpreferredencoding()
     
     try:
         import pwd
         uid = os.getuid()
         w = pwd.getpwuid(uid)
-        gecos = w.pw_gecos
+        gecos = w.pw_gecos.decode(enc)
+        username = w.pw_name.decode(enc)
         comma = gecos.find(',')
         if comma == -1:
             realname = gecos
         else:
             realname = gecos[:comma]
-        return '%s <%s@%s>' % (realname, w.pw_name, socket.getfqdn())
+
+        return realname, (username + '@' + socket.getfqdn())
+
     except ImportError:
-        pass
-
-    import getpass, socket
-    return '<%s@%s>' % (getpass.getuser(), socket.getfqdn())
+        import getpass
+        return '', (getpass.getuser().decode(enc) + '@' + socket.getfqdn())
+
+
+
+def username():
+    """Return email-style username.
+
+    Something similar to 'Martin Pool <mbp at sourcefrog.net>'
+
+    :todo: Check it's reasonably well-formed.
+
+    :todo: Allow taking it from a dotfile to help people on windows
+           who can't easily set variables.
+    """
+    import locale
+    e = os.environ.get('BZREMAIL') or os.environ.get('EMAIL')
+    if e:
+        return e.decode(locale.getpreferredencoding())
+
+    name, email = auto_user_id()
+    if name:
+        return '%s <%s>' % (name, email)
+    else:
+        return email
 
 
 _EMAIL_RE = re.compile(r'[\w+.-]+@[\w+.-]+')
 def user_email():
     """Return just the email component of a username."""
     e = os.environ.get('BZREMAIL') or os.environ.get('EMAIL')
+    import locale
+    e = e.decode(locale.getpreferredencoding())
     if e:
         m = _EMAIL_RE.search(e)
         if not m:
             bailout('%r is not a reasonable email address' % e)
         return m.group(0)
 
-
-    import getpass, socket
-    return '%s@%s' % (getpass.getuser(), socket.getfqdn())
-
+    return auto_user_id()[1]
     
 
 



More information about the Pkg-bazaar-commits mailing list