1
# Copyright (C) 2005 by Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Configuration that affects the behaviour of Bazaar."""
20
from ConfigParser import ConfigParser
29
"""Return per-user configuration directory.
31
By default this is ~/.bazaar/
33
TODO: Global option --config-dir to override this.
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_editor(parser=None):
54
parser = _get_config_parser()
55
if parser.has_option('DEFAULT', 'editor'):
56
return parser.get('DEFAULT', 'editor')
59
def _get_user_id(branch=None, parser = None):
60
"""Return the full user id from a file or environment variable.
62
e.g. "John Hacker <jhacker@foo.org>"
65
A branch to use for a per-branch configuration, or None.
67
The following are searched in order:
70
2. .bzr/email for this branch.
74
v = os.environ.get('BZREMAIL')
76
return v.decode(bzrlib.user_encoding)
80
return (branch.controlfile("email", "r")
82
.decode(bzrlib.user_encoding)
85
if e.errno != errno.ENOENT:
91
parser = _get_config_parser()
92
if parser.has_option('DEFAULT', 'email'):
93
email = parser.get('DEFAULT', 'email')
97
v = os.environ.get('EMAIL')
99
return v.decode(bzrlib.user_encoding)
105
"""Calculate automatic user identification.
107
Returns (realname, email).
109
Only used when none is set in the environment or the id file.
111
This previously used the FQDN as the default domain, but that can
112
be very slow on machines where DNS is broken. So now we simply
117
# XXX: Any good way to get real user name on win32?
122
w = pwd.getpwuid(uid)
123
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
124
username = w.pw_name.decode(bzrlib.user_encoding)
125
comma = gecos.find(',')
129
realname = gecos[:comma]
135
realname = username = getpass.getuser().decode(bzrlib.user_encoding)
137
return realname, (username + '@' + socket.gethostname())
140
def username(branch):
141
"""Return email-style username.
143
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
145
TODO: Check it's reasonably well-formed.
147
v = _get_user_id(branch)
151
name, email = _auto_user_id()
153
return '%s <%s>' % (name, email)
158
def user_email(branch):
159
"""Return just the email component of a username."""
160
e = _get_user_id(branch)
162
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
164
raise BzrError("%r doesn't seem to contain "
165
"a reasonable email address" % e)
168
return _auto_user_id()[1]