18
18
"""Configuration that affects the behaviour of Bazaar."""
20
from ConfigParser import ConfigParser
23
29
"""Return per-user configuration directory.
25
By default this is ~/.bazaar.conf/
31
By default this is ~/.bazaar/
27
33
TODO: Global option --config-dir to override this.
29
return os.path.join(os.path.expanduser("~"), ".bazaar.conf")
35
return os.path.join(os.path.expanduser("~"), ".bazaar")
38
def config_filename():
39
"""Return per-user configuration ini file filename."""
40
return os.path.join(config_dir(), 'bazaar.conf')
43
def _get_config_parser(file=None):
44
parser = ConfigParser()
48
parser.read([config_filename()])
52
def _get_user_id(branch=None, parser = None):
53
"""Return the full user id from a file or environment variable.
55
e.g. "John Hacker <jhacker@foo.org>"
58
A branch to use for a per-branch configuration, or None.
60
The following are searched in order:
63
2. .bzr/email for this branch.
67
v = os.environ.get('BZREMAIL')
69
return v.decode(bzrlib.user_encoding)
73
return (branch.controlfile("email", "r")
75
.decode(bzrlib.user_encoding)
78
if e.errno != errno.ENOENT:
84
parser = _get_config_parser()
85
if parser.has_option('DEFAULT', 'email'):
86
email = parser.get('DEFAULT', 'email')
90
v = os.environ.get('EMAIL')
92
return v.decode(bzrlib.user_encoding)
98
"""Calculate automatic user identification.
100
Returns (realname, email).
102
Only used when none is set in the environment or the id file.
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
110
# XXX: Any good way to get real user name on win32?
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(',')
122
realname = gecos[:comma]
128
realname = username = getpass.getuser().decode(bzrlib.user_encoding)
130
return realname, (username + '@' + socket.gethostname())
133
def username(branch):
134
"""Return email-style username.
136
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
138
TODO: Check it's reasonably well-formed.
140
v = _get_user_id(branch)
144
name, email = _auto_user_id()
146
return '%s <%s>' % (name, email)
151
def user_email(branch):
152
"""Return just the email component of a username."""
153
e = _get_user_id(branch)
155
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
157
raise BzrError("%r doesn't seem to contain "
158
"a reasonable email address" % e)
161
return _auto_user_id()[1]