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
Currently this configuration resides in ~/.bazaar/bazaar.conf
21
and ~/.bazaar/branches.conf, which is written to by bzr.
23
In bazaar.conf the following options may be set:
25
editor=name-of-program
26
email=Your Name <your@email.address>
27
check_signatures=require|ignore|check-available(default)
28
create_signatures=always|never|when-required(default)
29
gpg_signing_command=name-of-program
31
in branches.conf, you specify the url of a branch and options for it.
32
Wildcards may be used - * and ? as normal in shell completion. Options
33
set in both bazaar.conf and branches.conf are overriden by the branches.conf
35
[/home/robertc/source]
36
recurse=False|True(default)
38
check_signatures= as abive
39
create_signatures= as above.
41
explanation of options
42
----------------------
43
editor - this option sets the pop up editor to use during commits.
44
email - this option sets the user id bzr will use when committing.
45
check_signatures - this option controls whether bzr will require good gpg
46
signatures, ignore them, or check them if they are
48
create_signatures - this option controls whether bzr will always create
49
gpg signatures, never create them, or create them if the
50
branch is configured to require them.
51
NB: This option is planned, but not implemented yet.
58
from fnmatch import fnmatch
63
import bzrlib.errors as errors
64
from bzrlib.osutils import pathjoin
65
from bzrlib.trace import mutter
66
import bzrlib.util.configobj.configobj as configobj
67
from StringIO import StringIO
74
class ConfigObj(configobj.ConfigObj):
76
def get_bool(self, section, key):
77
val = self[section][key].lower()
78
if val in ('1', 'yes', 'true', 'on'):
80
elif val in ('0', 'no', 'false', 'off'):
83
raise ValueError("Value %r is not boolean" % val)
85
def get_value(self, section, name):
86
# Try [] for the old DEFAULT section.
87
if section == "DEFAULT":
92
return self[section][name]
96
"""A configuration policy - what username, editor, gpg needs etc."""
99
"""Get the users pop up editor."""
100
raise NotImplementedError
102
def _get_signature_checking(self):
103
"""Template method to override signature checking policy."""
105
def _get_user_option(self, option_name):
106
"""Template method to provide a user option."""
109
def get_user_option(self, option_name):
110
"""Get a generic option - no special process, no default."""
111
return self._get_user_option(option_name)
113
def gpg_signing_command(self):
114
"""What program should be used to sign signatures?"""
115
result = self._gpg_signing_command()
120
def _gpg_signing_command(self):
121
"""See gpg_signing_command()."""
125
super(Config, self).__init__()
127
def post_commit(self):
128
"""An ordered list of python functions to call.
130
Each function takes branch, rev_id as parameters.
132
return self._post_commit()
134
def _post_commit(self):
135
"""See Config.post_commit."""
138
def user_email(self):
139
"""Return just the email component of a username."""
140
return extract_email_address(self.username())
143
"""Return email-style username.
145
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
147
$BZREMAIL can be set to override this, then
148
the concrete policy type is checked, and finally
150
If none is found, a reasonable default is (hopefully)
153
TODO: Check it's reasonably well-formed.
155
v = os.environ.get('BZREMAIL')
157
return v.decode(bzrlib.user_encoding)
159
v = self._get_user_id()
163
v = os.environ.get('EMAIL')
165
return v.decode(bzrlib.user_encoding)
167
name, email = _auto_user_id()
169
return '%s <%s>' % (name, email)
173
def signature_checking(self):
174
"""What is the current policy for signature checking?."""
175
policy = self._get_signature_checking()
176
if policy is not None:
178
return CHECK_IF_POSSIBLE
180
def signature_needed(self):
181
"""Is a signature needed when committing ?."""
182
policy = self._get_signature_checking()
183
if policy == CHECK_ALWAYS:
187
def get_command_defaults(self, command_name):
188
"""Return the default options"""
189
# All these template methods look like overengineering to me. Is it
190
# clear that we want to support a variety of configuration formats?
191
return self._get_command_defaults(command_name)
193
def _get_command_defaults(self, command_name):
197
class IniBasedConfig(Config):
198
"""A configuration policy that draws from ini files."""
200
def _get_parser(self, file=None):
201
if self._parser is not None:
204
input = self._get_filename()
208
self._parser = ConfigObj(input)
209
except configobj.ConfigObjError, e:
210
raise errors.ParseConfigError(e.errors, e.config.filename)
213
def _get_section(self):
214
"""Override this to define the section used by the config."""
217
def _get_signature_checking(self):
218
"""See Config._get_signature_checking."""
219
policy = self._get_user_option('check_signatures')
221
return self._string_to_signature_policy(policy)
223
def _get_user_id(self):
224
"""Get the user id from the 'email' key in the current section."""
225
return self._get_user_option('email')
227
def _get_user_option(self, option_name):
228
"""See Config._get_user_option."""
230
return self._get_parser().get_value(self._get_section(),
235
def _gpg_signing_command(self):
236
"""See Config.gpg_signing_command."""
237
return self._get_user_option('gpg_signing_command')
239
def __init__(self, get_filename):
240
super(IniBasedConfig, self).__init__()
241
self._get_filename = get_filename
244
def _post_commit(self):
245
"""See Config.post_commit."""
246
return self._get_user_option('post_commit')
248
def _string_to_signature_policy(self, signature_string):
249
"""Convert a string to a signing policy."""
250
if signature_string.lower() == 'check-available':
251
return CHECK_IF_POSSIBLE
252
if signature_string.lower() == 'ignore':
254
if signature_string.lower() == 'require':
256
raise errors.BzrError("Invalid signatures policy '%s'"
259
def _get_command_defaults(self, command_name):
261
defaults = self._get_parser().get_value("COMMAND_DEFAULTS",
266
return shlex.split(defaults)
267
except ValueError, e:
268
raise errors.CommandDefaultSyntax(command_name=command_name,
271
class GlobalConfig(IniBasedConfig):
272
"""The configuration that should be used for a specific location."""
274
def get_editor(self):
275
return self._get_user_option('editor')
278
super(GlobalConfig, self).__init__(config_filename)
281
class LocationConfig(IniBasedConfig):
282
"""A configuration object that gives the policy for a location."""
284
def __init__(self, location):
285
super(LocationConfig, self).__init__(branches_config_filename)
286
self._global_config = None
287
self.location = location
289
def _get_global_config(self):
290
if self._global_config is None:
291
self._global_config = GlobalConfig()
292
return self._global_config
294
def _get_section(self):
295
"""Get the section we should look in for config items.
297
Returns None if none exists.
298
TODO: perhaps return a NullSection that thunks through to the
301
sections = self._get_parser()
302
location_names = self.location.split('/')
303
if self.location.endswith('/'):
304
del location_names[-1]
306
for section in sections:
307
section_names = section.split('/')
308
if section.endswith('/'):
309
del section_names[-1]
310
names = zip(location_names, section_names)
313
if not fnmatch(name[0], name[1]):
318
# so, for the common prefix they matched.
319
# if section is longer, no match.
320
if len(section_names) > len(location_names):
322
# if path is longer, and recurse is not true, no match
323
if len(section_names) < len(location_names):
325
if not self._get_parser().get_bool(section, 'recurse'):
329
matches.append((len(section_names), section))
332
matches.sort(reverse=True)
335
def _gpg_signing_command(self):
336
"""See Config.gpg_signing_command."""
337
command = super(LocationConfig, self)._gpg_signing_command()
338
if command is not None:
340
return self._get_global_config()._gpg_signing_command()
342
def _get_user_id(self):
343
user_id = super(LocationConfig, self)._get_user_id()
344
if user_id is not None:
346
return self._get_global_config()._get_user_id()
348
def _get_user_option(self, option_name):
349
"""See Config._get_user_option."""
350
option_value = super(LocationConfig,
351
self)._get_user_option(option_name)
352
if option_value is not None:
354
return self._get_global_config()._get_user_option(option_name)
356
def _get_signature_checking(self):
357
"""See Config._get_signature_checking."""
358
check = super(LocationConfig, self)._get_signature_checking()
359
if check is not None:
361
return self._get_global_config()._get_signature_checking()
363
def _post_commit(self):
364
"""See Config.post_commit."""
365
hook = self._get_user_option('post_commit')
368
return self._get_global_config()._post_commit()
370
def set_user_option(self, option, value):
371
"""Save option and its value in the configuration."""
372
# FIXME: RBC 20051029 This should refresh the parser and also take a
373
# file lock on branches.conf.
374
conf_dir = os.path.dirname(self._get_filename())
375
ensure_config_dir_exists(conf_dir)
376
location = self.location
377
if location.endswith('/'):
378
location = location[:-1]
379
if (not location in self._get_parser() and
380
not location + '/' in self._get_parser()):
381
self._get_parser()[location]={}
382
elif location + '/' in self._get_parser():
383
location = location + '/'
384
self._get_parser()[location][option]=value
385
self._get_parser().write()
388
class BranchConfig(Config):
389
"""A configuration object giving the policy for a branch."""
391
def _get_location_config(self):
392
if self._location_config is None:
393
self._location_config = LocationConfig(self.branch.base)
394
return self._location_config
396
def _get_user_id(self):
397
"""Return the full user id for the branch.
399
e.g. "John Hacker <jhacker@foo.org>"
400
This is looked up in the email controlfile for the branch.
403
return (self.branch.control_files.get_utf8("email")
405
.decode(bzrlib.user_encoding)
407
except errors.NoSuchFile, e:
410
return self._get_location_config()._get_user_id()
412
def _get_signature_checking(self):
413
"""See Config._get_signature_checking."""
414
return self._get_location_config()._get_signature_checking()
416
def _get_user_option(self, option_name):
417
"""See Config._get_user_option."""
418
return self._get_location_config()._get_user_option(option_name)
420
def _gpg_signing_command(self):
421
"""See Config.gpg_signing_command."""
422
return self._get_location_config()._gpg_signing_command()
424
def __init__(self, branch):
425
super(BranchConfig, self).__init__()
426
self._location_config = None
429
def _post_commit(self):
430
"""See Config.post_commit."""
431
return self._get_location_config()._post_commit()
434
def ensure_config_dir_exists(path=None):
435
"""Make sure a configuration directory exists.
436
This makes sure that the directory exists.
437
On windows, since configuration directories are 2 levels deep,
438
it makes sure both the directory and the parent directory exists.
442
if not os.path.isdir(path):
443
if sys.platform == 'win32':
444
parent_dir = os.path.dirname(path)
445
if not os.path.isdir(parent_dir):
446
mutter('creating config parent directory: %r', parent_dir)
448
mutter('creating config directory: %r', path)
453
"""Return per-user configuration directory.
455
By default this is ~/.bazaar/
457
TODO: Global option --config-dir to override this.
459
base = os.environ.get('BZR_HOME', None)
460
if sys.platform == 'win32':
462
base = os.environ.get('APPDATA', None)
464
base = os.environ.get('HOME', None)
466
raise BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
467
return pathjoin(base, 'bazaar', '2.0')
469
# cygwin, linux, and darwin all have a $HOME directory
471
base = os.path.expanduser("~")
472
return pathjoin(base, ".bazaar")
475
def config_filename():
476
"""Return per-user configuration ini file filename."""
477
return pathjoin(config_dir(), 'bazaar.conf')
480
def branches_config_filename():
481
"""Return per-user configuration ini file filename."""
482
return pathjoin(config_dir(), 'branches.conf')
486
"""Calculate automatic user identification.
488
Returns (realname, email).
490
Only used when none is set in the environment or the id file.
492
This previously used the FQDN as the default domain, but that can
493
be very slow on machines where DNS is broken. So now we simply
498
# XXX: Any good way to get real user name on win32?
503
w = pwd.getpwuid(uid)
504
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
505
username = w.pw_name.decode(bzrlib.user_encoding)
506
comma = gecos.find(',')
510
realname = gecos[:comma]
516
realname = username = getpass.getuser().decode(bzrlib.user_encoding)
518
return realname, (username + '@' + socket.gethostname())
521
def extract_email_address(e):
522
"""Return just the address part of an email string.
524
That is just the user@domain part, nothing else.
525
This part is required to contain only ascii characters.
526
If it can't be extracted, raises an error.
528
>>> extract_email_address('Jane Tester <jane@test.com>')
531
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
533
raise errors.BzrError("%r doesn't seem to contain "
534
"a reasonable email address" % e)
537
class TreeConfig(object):
538
"""Branch configuration data associated with its contents, not location"""
539
def __init__(self, branch):
542
def _get_config(self):
544
obj = ConfigObj(self.branch.control_files.get('branch.conf'
547
except errors.NoSuchFile:
551
def get_option(self, name, section=None, default=None):
552
self.branch.lock_read()
554
obj = self._get_config()
556
if section is not None:
565
def set_option(self, value, name, section=None):
566
"""Set a per-branch configuration option"""
567
self.branch.lock_write()
569
cfg_obj = self._get_config()
574
obj = cfg_obj[section]
576
cfg_obj[section] = {}
577
obj = cfg_obj[section]
579
cfg_obj.encode('UTF-8')
580
out_file = StringIO(''.join([l+'\n' for l in cfg_obj.write()]))
582
self.branch.control_files.put('branch.conf', out_file)