1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# Copyright (C) 2005 by Canonical Ltd
# Authors: Robert Collins <robert.collins@canonical.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Configuration that affects the behaviour of Bazaar."""
from ConfigParser import ConfigParser
import os
import errno
import re
import bzrlib
def config_dir():
"""Return per-user configuration directory.
By default this is ~/.bazaar/
TODO: Global option --config-dir to override this.
"""
return os.path.join(os.path.expanduser("~"), ".bazaar")
def config_filename():
"""Return per-user configuration ini file filename."""
return os.path.join(config_dir(), 'bazaar.conf')
def _get_config_parser(file=None):
parser = ConfigParser()
if file is not None:
parser.readfp(file)
else:
parser.read([config_filename()])
return parser
def get_editor(parser=None):
if parser is None:
parser = _get_config_parser()
if parser.has_option('DEFAULT', 'editor'):
return parser.get('DEFAULT', 'editor')
def _get_user_id(branch=None, parser = None):
"""Return the full user id from a file or environment variable.
e.g. "John Hacker <jhacker@foo.org>"
branch
A branch to use for a per-branch configuration, or None.
The following are searched in order:
1. $BZREMAIL
2. .bzr/email for this branch.
3. ~/.bzr.conf/email
4. $EMAIL
"""
v = os.environ.get('BZREMAIL')
if v:
return v.decode(bzrlib.user_encoding)
if branch:
try:
return (branch.controlfile("email", "r")
.read()
.decode(bzrlib.user_encoding)
.rstrip("\r\n"))
except IOError, e:
if e.errno != errno.ENOENT:
raise
except BzrError, e:
pass
if parser is None:
parser = _get_config_parser()
if parser.has_option('DEFAULT', 'email'):
email = parser.get('DEFAULT', 'email')
if email is not None:
return email
v = os.environ.get('EMAIL')
if v:
return v.decode(bzrlib.user_encoding)
else:
return None
def _auto_user_id():
"""Calculate automatic user identification.
Returns (realname, email).
Only used when none is set in the environment or the id file.
This previously used the FQDN as the default domain, but that can
be very slow on machines where DNS is broken. So now we simply
use the hostname.
"""
import socket
# XXX: Any good way to get real user name on win32?
try:
import pwd
uid = os.getuid()
w = pwd.getpwuid(uid)
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
username = w.pw_name.decode(bzrlib.user_encoding)
comma = gecos.find(',')
if comma == -1:
realname = gecos
else:
realname = gecos[:comma]
if not realname:
realname = username
except ImportError:
import getpass
realname = username = getpass.getuser().decode(bzrlib.user_encoding)
return realname, (username + '@' + socket.gethostname())
def username(branch):
"""Return email-style username.
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
TODO: Check it's reasonably well-formed.
"""
v = _get_user_id(branch)
if v:
return v
name, email = _auto_user_id()
if name:
return '%s <%s>' % (name, email)
else:
return email
def user_email(branch):
"""Return just the email component of a username."""
e = _get_user_id(branch)
if e:
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
if not m:
raise BzrError("%r doesn't seem to contain "
"a reasonable email address" % e)
return m.group(0)
return _auto_user_id()[1]
|