~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Robert Collins
  • Date: 2005-10-11 07:00:25 UTC
  • mto: This revision was merged to the branch mainline in revision 1443.
  • Revision ID: robertc@robertcollins.net-20051011070025-bac6b53cb6186dfd
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Configuration that affects the behaviour of Bazaar."""
19
19
 
 
20
from ConfigParser import ConfigParser
20
21
import os
 
22
import errno
 
23
import re
 
24
 
 
25
import bzrlib
 
26
 
21
27
 
22
28
def config_dir():
23
29
    """Return per-user configuration directory.
24
30
 
25
 
    By default this is ~/.bazaar.conf/
 
31
    By default this is ~/.bazaar/
26
32
    
27
33
    TODO: Global option --config-dir to override this.
28
34
    """
29
 
    return os.path.join(os.path.expanduser("~"), ".bazaar.conf")
 
35
    return os.path.join(os.path.expanduser("~"), ".bazaar")
 
36
 
 
37
 
 
38
def config_filename():
 
39
    """Return per-user configuration ini file filename."""
 
40
    return os.path.join(config_dir(), 'bazaar.conf')
 
41
 
 
42
 
 
43
def _get_config_parser(file=None):
 
44
    parser = ConfigParser()
 
45
    if file is not None:
 
46
        parser.readfp(file)
 
47
    else:
 
48
        parser.read([config_filename()])
 
49
    return parser
 
50
 
 
51
 
 
52
def _get_user_id(branch=None, parser = None):
 
53
    """Return the full user id from a file or environment variable.
 
54
 
 
55
    e.g. "John Hacker <jhacker@foo.org>"
 
56
 
 
57
    branch
 
58
        A branch to use for a per-branch configuration, or None.
 
59
 
 
60
    The following are searched in order:
 
61
 
 
62
    1. $BZREMAIL
 
63
    2. .bzr/email for this branch.
 
64
    3. ~/.bzr.conf/email
 
65
    4. $EMAIL
 
66
    """
 
67
    v = os.environ.get('BZREMAIL')
 
68
    if v:
 
69
        return v.decode(bzrlib.user_encoding)
 
70
 
 
71
    if branch:
 
72
        try:
 
73
            return (branch.controlfile("email", "r") 
 
74
                    .read()
 
75
                    .decode(bzrlib.user_encoding)
 
76
                    .rstrip("\r\n"))
 
77
        except IOError, e:
 
78
            if e.errno != errno.ENOENT:
 
79
                raise
 
80
        except BzrError, e:
 
81
            pass
 
82
    
 
83
    if not parser:
 
84
        parser = _get_config_parser()
 
85
    if parser.has_option('DEFAULT', 'email'):
 
86
        email = parser.get('DEFAULT', 'email')
 
87
        if email is not None:
 
88
            return email
 
89
 
 
90
    v = os.environ.get('EMAIL')
 
91
    if v:
 
92
        return v.decode(bzrlib.user_encoding)
 
93
    else:    
 
94
        return None
 
95
 
 
96
 
 
97
def _auto_user_id():
 
98
    """Calculate automatic user identification.
 
99
 
 
100
    Returns (realname, email).
 
101
 
 
102
    Only used when none is set in the environment or the id file.
 
103
 
 
104
    This previously used the FQDN as the default domain, but that can
 
105
    be very slow on machines where DNS is broken.  So now we simply
 
106
    use the hostname.
 
107
    """
 
108
    import socket
 
109
 
 
110
    # XXX: Any good way to get real user name on win32?
 
111
 
 
112
    try:
 
113
        import pwd
 
114
        uid = os.getuid()
 
115
        w = pwd.getpwuid(uid)
 
116
        gecos = w.pw_gecos.decode(bzrlib.user_encoding)
 
117
        username = w.pw_name.decode(bzrlib.user_encoding)
 
118
        comma = gecos.find(',')
 
119
        if comma == -1:
 
120
            realname = gecos
 
121
        else:
 
122
            realname = gecos[:comma]
 
123
        if not realname:
 
124
            realname = username
 
125
 
 
126
    except ImportError:
 
127
        import getpass
 
128
        realname = username = getpass.getuser().decode(bzrlib.user_encoding)
 
129
 
 
130
    return realname, (username + '@' + socket.gethostname())
 
131
 
 
132
 
 
133
def username(branch):
 
134
    """Return email-style username.
 
135
 
 
136
    Something similar to 'Martin Pool <mbp@sourcefrog.net>'
 
137
 
 
138
    TODO: Check it's reasonably well-formed.
 
139
    """
 
140
    v = _get_user_id(branch)
 
141
    if v:
 
142
        return v
 
143
    
 
144
    name, email = _auto_user_id()
 
145
    if name:
 
146
        return '%s <%s>' % (name, email)
 
147
    else:
 
148
        return email
 
149
 
 
150
 
 
151
def user_email(branch):
 
152
    """Return just the email component of a username."""
 
153
    e = _get_user_id(branch)
 
154
    if e:
 
155
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
 
156
        if not m:
 
157
            raise BzrError("%r doesn't seem to contain "
 
158
                           "a reasonable email address" % e)
 
159
        return m.group(0)
 
160
 
 
161
    return _auto_user_id()[1]
 
162
 
 
163