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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
# 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.
Currently this configuration resides in ~/.bazaar/bazaar.conf
and ~/.bazaar/branches.conf, which is written to by bzr.
In bazaar.config the following options may be set:
[DEFAULT]
editor=name-of-program
email=Your Name <your@email.address>
check_signatures=require|ignore|check-available(default)
create_signatures=always|never|when-required(default)
gpg_signing_command=name-of-program
in branches.conf, you specify the url of a branch and options for it.
Wildcards may be used - * and ? as normal in shell completion. Options
set in both bazaar.conf and branches.conf are overriden by the branches.conf
setting.
[/home/robertc/source]
recurse=False|True(default)
email= as above
check_signatures= as abive
create_signatures= as above.
explanation of options
----------------------
editor - this option sets the pop up editor to use during commits.
email - this option sets the user id bzr will use when committing.
check_signatures - this option controls whether bzr will require good gpg
signatures, ignore them, or check them if they are
present.
create_signatures - this option controls whether bzr will always create
gpg signatures, never create them, or create them if the
branch is configured to require them.
NB: This option is planned, but not implemented yet.
"""
from ConfigParser import ConfigParser
import os
from fnmatch import fnmatch
import errno
import re
import bzrlib
import bzrlib.errors as errors
CHECK_IF_POSSIBLE=0
CHECK_ALWAYS=1
CHECK_NEVER=2
class Config(object):
"""A configuration policy - what username, editor, gpg needs etc."""
def get_editor(self):
"""Get the users pop up editor."""
raise NotImplementedError
def _get_signature_checking(self):
"""Template method to override signature checking policy."""
def gpg_signing_command(self):
"""What program should be used to sign signatures?"""
result = self._gpg_signing_command()
if result is None:
result = "gpg"
return result
def _gpg_signing_command(self):
"""See gpg_signing_command()."""
return None
def __init__(self):
super(Config, self).__init__()
def user_email(self):
"""Return just the email component of a username."""
e = self.username()
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)
def username(self):
"""Return email-style username.
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
$BZREMAIL can be set to override this, then
the concrete policy type is checked, and finally
$EMAIL is examinged.
but if none is found, a reasonable default is (hopefully)
created.
TODO: Check it's reasonably well-formed.
"""
v = os.environ.get('BZREMAIL')
if v:
return v.decode(bzrlib.user_encoding)
v = self._get_user_id()
if v:
return v
v = os.environ.get('EMAIL')
if v:
return v.decode(bzrlib.user_encoding)
name, email = _auto_user_id()
if name:
return '%s <%s>' % (name, email)
else:
return email
def signature_checking(self):
"""What is the current policy for signature checking?."""
policy = self._get_signature_checking()
if policy is not None:
return policy
return CHECK_IF_POSSIBLE
def signature_needed(self):
"""Is a signature needed when committing ?."""
policy = self._get_signature_checking()
if policy == CHECK_ALWAYS:
return True
return False
class IniBasedConfig(Config):
"""A configuration policy that draws from ini files."""
def _get_parser(self, file=None):
if self._parser is not None:
return self._parser
parser = ConfigParser()
if file is not None:
parser.readfp(file)
else:
parser.read([self._get_filename()])
self._parser = parser
return parser
def _get_section(self):
"""Override this to define the section used by the config."""
return "DEFAULT"
def _get_signature_checking(self):
"""See Config._get_signature_checking."""
section = self._get_section()
if section is None:
return None
if self._get_parser().has_option(section, 'check_signatures'):
return self._string_to_signature_policy(
self._get_parser().get(section, 'check_signatures'))
def _get_user_id(self):
"""Get the user id from the 'email' key in the current section."""
section = self._get_section()
if section is not None:
if self._get_parser().has_option(section, 'email'):
return self._get_parser().get(section, 'email')
def _gpg_signing_command(self):
"""See Config.gpg_signing_command."""
section = self._get_section()
if section is not None:
if self._get_parser().has_option(section, 'gpg_signing_command'):
return self._get_parser().get(section, 'gpg_signing_command')
def __init__(self, get_filename):
super(IniBasedConfig, self).__init__()
self._get_filename = get_filename
self._parser = None
def _string_to_signature_policy(self, signature_string):
"""Convert a string to a signing policy."""
if signature_string.lower() == 'check-available':
return CHECK_IF_POSSIBLE
if signature_string.lower() == 'ignore':
return CHECK_NEVER
if signature_string.lower() == 'require':
return CHECK_ALWAYS
raise errors.BzrError("Invalid signatures policy '%s'"
% signature_string)
class GlobalConfig(IniBasedConfig):
"""The configuration that should be used for a specific location."""
def get_editor(self):
if self._get_parser().has_option(self._get_section(), 'editor'):
return self._get_parser().get(self._get_section(), 'editor')
def __init__(self):
super(GlobalConfig, self).__init__(config_filename)
class LocationConfig(IniBasedConfig):
"""A configuration object that gives the policy for a location."""
def __init__(self, location):
super(LocationConfig, self).__init__(branches_config_filename)
self._global_config = None
self.location = location
def _get_global_config(self):
if self._global_config is None:
self._global_config = GlobalConfig()
return self._global_config
def _get_section(self):
"""Get the section we should look in for config items.
Returns None if none exists.
TODO: perhaps return a NullSection that thunks through to the
global config.
"""
sections = self._get_parser().sections()
location_names = self.location.split('/')
if self.location.endswith('/'):
del location_names[-1]
matches=[]
for section in sections:
section_names = section.split('/')
if section.endswith('/'):
del section_names[-1]
names = zip(location_names, section_names)
matched = True
for name in names:
if not fnmatch(name[0], name[1]):
matched = False
break
if not matched:
continue
# so, for the common prefix they matched.
# if section is longer, no match.
if len(section_names) > len(location_names):
continue
# if path is longer, and recurse is not true, no match
if len(section_names) < len(location_names):
if (self._get_parser().has_option(section, 'recurse')
and not self._get_parser().getboolean(section, 'recurse')):
continue
matches.append((len(section_names), section))
if not len(matches):
return None
matches.sort(reverse=True)
return matches[0][1]
def _gpg_signing_command(self):
"""See Config.gpg_signing_command."""
command = super(LocationConfig, self)._gpg_signing_command()
if command is not None:
return command
return self._get_global_config()._gpg_signing_command()
def _get_user_id(self):
user_id = super(LocationConfig, self)._get_user_id()
if user_id is not None:
return user_id
return self._get_global_config()._get_user_id()
def _get_signature_checking(self):
"""See Config._get_signature_checking."""
check = super(LocationConfig, self)._get_signature_checking()
if check is not None:
return check
return self._get_global_config()._get_signature_checking()
class BranchConfig(Config):
"""A configuration object giving the policy for a branch."""
def _get_location_config(self):
if self._location_config is None:
self._location_config = LocationConfig(self.branch.base)
return self._location_config
def _get_user_id(self):
"""Return the full user id for the branch.
e.g. "John Hacker <jhacker@foo.org>"
This is looked up in the email controlfile for the branch.
"""
try:
return (self.branch.controlfile("email", "r")
.read()
.decode(bzrlib.user_encoding)
.rstrip("\r\n"))
except errors.NoSuchFile, e:
pass
return self._get_location_config()._get_user_id()
def _get_signature_checking(self):
"""See Config._get_signature_checking."""
return self._get_location_config()._get_signature_checking()
def _gpg_signing_command(self):
"""See Config.gpg_signing_command."""
return self._get_location_config()._gpg_signing_command()
def __init__(self, branch):
super(BranchConfig, self).__init__()
self._location_config = None
self.branch = branch
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 branches_config_filename():
"""Return per-user configuration ini file filename."""
return os.path.join(config_dir(), 'branches.conf')
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())
|