271
272
the concrete policy type is checked, and finally
272
273
$EMAIL is examined.
273
274
If no username can be found, errors.NoWhoami exception is raised.
275
TODO: Check it's reasonably well-formed.
277
276
v = os.environ.get('BZR_EMAIL')
279
278
return v.decode(osutils.get_user_encoding())
281
279
v = self._get_user_id()
285
282
v = os.environ.get('EMAIL')
287
284
return v.decode(osutils.get_user_encoding())
285
name, email = _auto_user_id()
287
return '%s <%s>' % (name, email)
289
290
raise errors.NoWhoami()
291
292
def ensure_username(self):
1213
1214
return os.path.expanduser('~/.cache')
1217
def _get_default_mail_domain():
1218
"""If possible, return the assumed default email domain.
1220
:returns: string mail domain, or None.
1222
if sys.platform == 'win32':
1223
# No implementation yet; patches welcome
1226
f = open('/etc/mailname')
1227
except (IOError, OSError), e:
1230
domain = f.read().strip()
1236
def _auto_user_id():
1237
"""Calculate automatic user identification.
1239
:returns: (realname, email), either of which may be None if they can't be
1242
Only used when none is set in the environment or the id file.
1244
This only returns an email address if we can be fairly sure the
1245
address is reasonable, ie if /etc/mailname is set on unix.
1247
This doesn't use the FQDN as the default domain because that may be
1248
slow, and it doesn't use the hostname alone because that's not normally
1249
a reasonable address.
1251
if sys.platform == 'win32':
1252
# No implementation to reliably determine Windows default mail
1253
# address; please add one.
1256
default_mail_domain = _get_default_mail_domain()
1257
if not default_mail_domain:
1263
w = pwd.getpwuid(uid)
1265
mutter('no passwd entry for uid %d?' % uid)
1268
# we try utf-8 first, because on many variants (like Linux),
1269
# /etc/passwd "should" be in utf-8, and because it's unlikely to give
1270
# false positives. (many users will have their user encoding set to
1271
# latin-1, which cannot raise UnicodeError.)
1273
gecos = w.pw_gecos.decode('utf-8')
1275
except UnicodeError:
1277
encoding = osutils.get_user_encoding()
1278
gecos = w.pw_gecos.decode(encoding)
1279
except UnicodeError, e:
1280
mutter("cannot decode passwd entry %s" % w)
1283
username = w.pw_name.decode(encoding)
1284
except UnicodeError, e:
1285
mutter("cannot decode passwd entry %s" % w)
1288
comma = gecos.find(',')
1292
realname = gecos[:comma]
1294
return realname, (username + '@' + default_mail_domain)
1216
1297
def parse_username(username):
1217
1298
"""Parse e-mail username and return a (name, address) tuple."""
1218
1299
match = re.match(r'(.*?)\s*<?([\w+.-]+@[\w+.-]+)>?', username)