/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Andrew Bennetts
  • Date: 2011-04-08 03:31:54 UTC
  • mfrom: (5766 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5767.
  • Revision ID: andrew.bennetts@canonical.com-20110408033154-la08nghd4391sw5m
Merge latest lp:bzr, move our new release notes entries to the current release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
"""
64
64
 
65
65
import os
 
66
import string
66
67
import sys
67
68
 
68
69
from bzrlib import commands
69
70
from bzrlib.decorators import needs_write_lock
70
71
from bzrlib.lazy_import import lazy_import
71
72
lazy_import(globals(), """
72
 
import errno
73
73
import fnmatch
74
74
import re
75
75
from cStringIO import StringIO
279
279
        # We need to iterate until no more refs appear ({{foo}} will need two
280
280
        # iterations for example).
281
281
        while True:
282
 
            try:
283
 
                raw_chunks = self.option_ref_re.split(result)
284
 
            except TypeError:
285
 
                import pdb; pdb.set_trace()
 
282
            raw_chunks = self.option_ref_re.split(result)
286
283
            if len(raw_chunks) == 1:
287
284
                # Shorcut the trivial case: no refs
288
285
                return result
444
441
        the concrete policy type is checked, and finally
445
442
        $EMAIL is examined.
446
443
        If no username can be found, errors.NoWhoami exception is raised.
447
 
 
448
 
        TODO: Check it's reasonably well-formed.
449
444
        """
450
445
        v = os.environ.get('BZR_EMAIL')
451
446
        if v:
452
447
            return v.decode(osutils.get_user_encoding())
453
 
 
454
448
        v = self._get_user_id()
455
449
        if v:
456
450
            return v
457
 
 
458
451
        v = os.environ.get('EMAIL')
459
452
        if v:
460
453
            return v.decode(osutils.get_user_encoding())
461
 
 
 
454
        name, email = _auto_user_id()
 
455
        if name and email:
 
456
            return '%s <%s>' % (name, email)
 
457
        elif email:
 
458
            return email
462
459
        raise errors.NoWhoami()
463
460
 
464
461
    def ensure_username(self):
1410
1407
        return os.path.expanduser('~/.cache')
1411
1408
 
1412
1409
 
 
1410
def _get_default_mail_domain():
 
1411
    """If possible, return the assumed default email domain.
 
1412
 
 
1413
    :returns: string mail domain, or None.
 
1414
    """
 
1415
    if sys.platform == 'win32':
 
1416
        # No implementation yet; patches welcome
 
1417
        return None
 
1418
    try:
 
1419
        f = open('/etc/mailname')
 
1420
    except (IOError, OSError), e:
 
1421
        return None
 
1422
    try:
 
1423
        domain = f.read().strip()
 
1424
        return domain
 
1425
    finally:
 
1426
        f.close()
 
1427
 
 
1428
 
 
1429
def _auto_user_id():
 
1430
    """Calculate automatic user identification.
 
1431
 
 
1432
    :returns: (realname, email), either of which may be None if they can't be
 
1433
    determined.
 
1434
 
 
1435
    Only used when none is set in the environment or the id file.
 
1436
 
 
1437
    This only returns an email address if we can be fairly sure the 
 
1438
    address is reasonable, ie if /etc/mailname is set on unix.
 
1439
 
 
1440
    This doesn't use the FQDN as the default domain because that may be 
 
1441
    slow, and it doesn't use the hostname alone because that's not normally 
 
1442
    a reasonable address.
 
1443
    """
 
1444
    if sys.platform == 'win32':
 
1445
        # No implementation to reliably determine Windows default mail
 
1446
        # address; please add one.
 
1447
        return None, None
 
1448
 
 
1449
    default_mail_domain = _get_default_mail_domain()
 
1450
    if not default_mail_domain:
 
1451
        return None, None
 
1452
 
 
1453
    import pwd
 
1454
    uid = os.getuid()
 
1455
    try:
 
1456
        w = pwd.getpwuid(uid)
 
1457
    except KeyError:
 
1458
        mutter('no passwd entry for uid %d?' % uid)
 
1459
        return None, None
 
1460
 
 
1461
    # we try utf-8 first, because on many variants (like Linux),
 
1462
    # /etc/passwd "should" be in utf-8, and because it's unlikely to give
 
1463
    # false positives.  (many users will have their user encoding set to
 
1464
    # latin-1, which cannot raise UnicodeError.)
 
1465
    try:
 
1466
        gecos = w.pw_gecos.decode('utf-8')
 
1467
        encoding = 'utf-8'
 
1468
    except UnicodeError:
 
1469
        try:
 
1470
            encoding = osutils.get_user_encoding()
 
1471
            gecos = w.pw_gecos.decode(encoding)
 
1472
        except UnicodeError, e:
 
1473
            mutter("cannot decode passwd entry %s" % w)
 
1474
            return None, None
 
1475
    try:
 
1476
        username = w.pw_name.decode(encoding)
 
1477
    except UnicodeError, e:
 
1478
        mutter("cannot decode passwd entry %s" % w)
 
1479
        return None, None
 
1480
 
 
1481
    comma = gecos.find(',')
 
1482
    if comma == -1:
 
1483
        realname = gecos
 
1484
    else:
 
1485
        realname = gecos[:comma]
 
1486
 
 
1487
    return realname, (username + '@' + default_mail_domain)
 
1488
 
 
1489
 
1413
1490
def parse_username(username):
1414
1491
    """Parse e-mail username and return a (name, address) tuple."""
1415
1492
    match = re.match(r'(.*?)\s*<?([\w+.-]+@[\w+.-]+)>?', username)