~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-03-22 09:57:11 UTC
  • mfrom: (5724.1.4 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20110322095711-9bggm9tnxnw9frow
(jameinel) Fix tar exporters to always write to binary streams. (John A
 Meinel)

Show diffs side-by-side

added added

removed removed

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