15
14
# You should have received a copy of the GNU General Public License
16
15
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
"""Configuration that affects the behaviour of Breezy.
21
Currently this configuration resides in ~/.config/breezy/breezy.conf
22
and ~/.config/breezy/locations.conf, which is written to by brz.
24
If the first location doesn't exist, then brz falls back to reading
25
Bazaar configuration files in ~/.bazaar or ~/.config/bazaar.
27
In breezy.conf the following options may be set:
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:
29
25
editor=name-of-program
30
26
email=Your Name <your@email.address>
31
27
check_signatures=require|ignore|check-available(default)
32
28
create_signatures=always|never|when-required(default)
33
log_format=name-of-format
34
validate_signatures_in_log=true|false(default)
35
acceptable_keys=pattern1,pattern2
36
gpg_signing_key=amy@example.com
29
gpg_signing_command=name-of-program
38
in locations.conf, you specify the url of a branch and options for it.
31
in branches.conf, you specify the url of a branch and options for it.
39
32
Wildcards may be used - * and ? as normal in shell completion. Options
40
set in both breezy.conf and locations.conf are overridden by the locations.conf
33
set in both bazaar.conf and branches.conf are overriden by the branches.conf
42
35
[/home/robertc/source]
43
36
recurse=False|True(default)
45
check_signatures= as above
38
check_signatures= as abive
46
39
create_signatures= as above.
47
validate_signatures_in_log=as above
48
acceptable_keys=as above
50
41
explanation of options
51
42
----------------------
52
43
editor - this option sets the pop up editor to use during commits.
53
email - this option sets the user id brz will use when committing.
54
check_signatures - this option will control whether brz will require good gpg
55
signatures, ignore them, or check them if they are
56
present. Currently it is unused except that
57
check_signatures turns on create_signatures.
58
create_signatures - this option controls whether brz will always create
59
gpg signatures or not on commits. There is an unused
60
option which in future is expected to work if
61
branch settings require signatures.
62
log_format - this option sets the default log format. Possible values are
63
long, short, line, or a plugin can register new formats.
64
validate_signatures_in_log - show GPG signature validity in log output
65
acceptable_keys - comma separated list of key patterns acceptable for
66
verify-signatures command
68
In breezy.conf you can also define aliases in the ALIASES sections, example
71
lastlog=log --line -r-10..-1
72
ll=log --line -r-10..-1
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.
77
from __future__ import absolute_import
54
from ConfigParser import ConfigParser
84
from .lazy_import import lazy_import
85
lazy_import(globals(), """
56
from fnmatch import fnmatch
108
from breezy.i18n import gettext
118
from .sixish import (
127
CHECK_IF_POSSIBLE = 0
132
SIGN_WHEN_REQUIRED = 0
139
POLICY_APPENDPATH = 2
143
POLICY_NORECURSE: 'norecurse',
144
POLICY_APPENDPATH: 'appendpath',
149
'norecurse': POLICY_NORECURSE,
150
'appendpath': POLICY_APPENDPATH,
154
STORE_LOCATION = POLICY_NONE
155
STORE_LOCATION_NORECURSE = POLICY_NORECURSE
156
STORE_LOCATION_APPENDPATH = POLICY_APPENDPATH
161
class OptionExpansionLoop(errors.BzrError):
163
_fmt = 'Loop involving %(refs)r while expanding "%(string)s".'
165
def __init__(self, string, refs):
167
self.refs = '->'.join(refs)
170
class ExpandingUnknownOption(errors.BzrError):
172
_fmt = 'Option "%(name)s" is not defined while expanding "%(string)s".'
174
def __init__(self, name, string):
179
class IllegalOptionName(errors.BzrError):
181
_fmt = 'Option "%(name)s" is not allowed.'
183
def __init__(self, name):
187
class ConfigContentError(errors.BzrError):
189
_fmt = "Config file %(filename)s is not UTF-8 encoded\n"
191
def __init__(self, filename):
192
self.filename = filename
195
class ParseConfigError(errors.BzrError):
197
_fmt = "Error(s) parsing config file %(filename)s:\n%(errors)s"
199
def __init__(self, errors, filename):
200
self.filename = filename
201
self.errors = '\n'.join(e.msg for e in errors)
204
class ConfigOptionValueError(errors.BzrError):
206
_fmt = ('Bad value "%(value)s" for option "%(name)s".\n'
207
'See ``brz help %(name)s``')
209
def __init__(self, name, value):
210
errors.BzrError.__init__(self, name=name, value=value)
213
class NoEmailInUsername(errors.BzrError):
215
_fmt = "%(username)r does not seem to contain a reasonable email address"
217
def __init__(self, username):
218
self.username = username
221
class NoSuchConfig(errors.BzrError):
223
_fmt = ('The "%(config_id)s" configuration does not exist.')
225
def __init__(self, config_id):
226
errors.BzrError.__init__(self, config_id=config_id)
229
class NoSuchConfigOption(errors.BzrError):
231
_fmt = ('The "%(option_name)s" configuration option does not exist.')
233
def __init__(self, option_name):
234
errors.BzrError.__init__(self, option_name=option_name)
237
def signature_policy_from_unicode(signature_string):
238
"""Convert a string to a signing policy."""
239
if signature_string.lower() == 'check-available':
240
return CHECK_IF_POSSIBLE
241
if signature_string.lower() == 'ignore':
243
if signature_string.lower() == 'require':
245
raise ValueError("Invalid signatures policy '%s'"
249
def signing_policy_from_unicode(signature_string):
250
"""Convert a string to a signing policy."""
251
if signature_string.lower() == 'when-required':
252
return SIGN_WHEN_REQUIRED
253
if signature_string.lower() == 'never':
255
if signature_string.lower() == 'always':
257
raise ValueError("Invalid signing policy '%s'"
261
def _has_decode_bug():
262
"""True if configobj will fail to decode to unicode on Python 2."""
265
conf = configobj.ConfigObj()
266
decode = getattr(conf, "_decode", None)
268
result = decode(b"\xc2\xa7", "utf-8")
269
if isinstance(result[0], str):
274
def _has_triplequote_bug():
275
"""True if triple quote logic is reversed, see lp:710410."""
276
conf = configobj.ConfigObj()
277
quote = getattr(conf, "_get_triple_quote", None)
278
if quote and quote('"""') != "'''":
283
class ConfigObj(configobj.ConfigObj):
285
def __init__(self, infile=None, **kwargs):
286
# We define our own interpolation mechanism calling it option expansion
287
super(ConfigObj, self).__init__(infile=infile,
291
if _has_decode_bug():
292
def _decode(self, infile, encoding):
293
if isinstance(infile, str) and encoding:
294
return infile.decode(encoding).splitlines(True)
295
return super(ConfigObj, self)._decode(infile, encoding)
297
if _has_triplequote_bug():
298
def _get_triple_quote(self, value):
299
quot = super(ConfigObj, self)._get_triple_quote(value)
300
if quot == configobj.tdquot:
301
return configobj.tsquot
302
return configobj.tdquot
304
def get_bool(self, section, key):
305
return self[section].as_bool(key)
307
def get_value(self, section, name):
308
# Try [] for the old DEFAULT section.
309
if section == "DEFAULT":
314
return self[section][name]
61
import bzrlib.errors as errors
317
69
class Config(object):
318
70
"""A configuration policy - what username, editor, gpg needs etc."""
321
super(Config, self).__init__()
324
"""Returns a unique ID for the config."""
325
raise NotImplementedError(self.config_id)
327
def get_change_editor(self, old_tree, new_tree):
328
from breezy import diff
329
cmd = self._get_change_editor()
332
cmd = cmd.replace('@old_path', '{old_path}')
333
cmd = cmd.replace('@new_path', '{new_path}')
334
cmd = cmdline.split(cmd)
335
if '{old_path}' not in cmd:
336
cmd.extend(['{old_path}', '{new_path}'])
337
return diff.DiffFromTool.from_string(cmd, old_tree, new_tree,
73
"""Get the users pop up editor."""
74
raise NotImplementedError
340
76
def _get_signature_checking(self):
341
77
"""Template method to override signature checking policy."""
343
def _get_signing_policy(self):
344
"""Template method to override signature creation policy."""
348
def expand_options(self, string, env=None):
349
"""Expand option references in the string in the configuration context.
351
:param string: The string containing option to expand.
353
:param env: An option dict defining additional configuration options or
354
overriding existing ones.
356
:returns: The expanded string.
358
return self._expand_options_in_string(string, env)
360
def _expand_options_in_list(self, slist, env=None, _ref_stack=None):
361
"""Expand options in a list of strings in the configuration context.
363
:param slist: A list of strings.
365
:param env: An option dict defining additional configuration options or
366
overriding existing ones.
368
:param _ref_stack: Private list containing the options being
369
expanded to detect loops.
371
:returns: The flatten list of expanded strings.
373
# expand options in each value separately flattening lists
376
value = self._expand_options_in_string(s, env, _ref_stack)
377
if isinstance(value, list):
383
def _expand_options_in_string(self, string, env=None, _ref_stack=None):
384
"""Expand options in the string in the configuration context.
386
:param string: The string to be expanded.
388
:param env: An option dict defining additional configuration options or
389
overriding existing ones.
391
:param _ref_stack: Private list containing the options being
392
expanded to detect loops.
394
:returns: The expanded string.
397
# Not much to expand there
399
if _ref_stack is None:
400
# What references are currently resolved (to detect loops)
402
if self.option_ref_re is None:
403
# We want to match the most embedded reference first (i.e. for
404
# '{{foo}}' we will get '{foo}',
405
# for '{bar{baz}}' we will get '{baz}'
406
self.option_ref_re = re.compile('({[^{}]+})')
408
# We need to iterate until no more refs appear ({{foo}} will need two
409
# iterations for example).
411
raw_chunks = self.option_ref_re.split(result)
412
if len(raw_chunks) == 1:
413
# Shorcut the trivial case: no refs
417
# Split will isolate refs so that every other chunk is a ref
419
for chunk in raw_chunks:
422
# Keep only non-empty strings (or we get bogus empty
423
# slots when a list value is involved).
428
if name in _ref_stack:
429
raise OptionExpansionLoop(string, _ref_stack)
430
_ref_stack.append(name)
431
value = self._expand_option(name, env, _ref_stack)
433
raise ExpandingUnknownOption(name, string)
434
if isinstance(value, list):
442
# Once a list appears as the result of an expansion, all
443
# callers will get a list result. This allows a consistent
444
# behavior even when some options in the expansion chain
445
# defined as strings (no comma in their value) but their
446
# expanded value is a list.
447
return self._expand_options_in_list(chunks, env, _ref_stack)
449
result = ''.join(chunks)
452
def _expand_option(self, name, env, _ref_stack):
453
if env is not None and name in env:
454
# Special case, values provided in env takes precedence over
458
# FIXME: This is a limited implementation, what we really need is a
459
# way to query the brz config for the value of an option,
460
# respecting the scope rules (That is, once we implement fallback
461
# configs, getting the option value should restart from the top
462
# config, not the current one) -- vila 20101222
463
value = self.get_user_option(name, expand=False)
464
if isinstance(value, list):
465
value = self._expand_options_in_list(value, env, _ref_stack)
467
value = self._expand_options_in_string(value, env, _ref_stack)
470
79
def _get_user_option(self, option_name):
471
80
"""Template method to provide a user option."""
474
def get_user_option(self, option_name, expand=True):
475
"""Get a generic option - no special process, no default.
477
:param option_name: The queried option.
479
:param expand: Whether options references should be expanded.
481
:returns: The value of the option.
483
value = self._get_user_option(option_name)
485
if isinstance(value, list):
486
value = self._expand_options_in_list(value)
487
elif isinstance(value, dict):
488
trace.warning('Cannot expand "%s":'
489
' Dicts do not support option expansion'
492
value = self._expand_options_in_string(value)
493
for hook in OldConfigHooks['get']:
494
hook(self, option_name, value)
497
def get_user_option_as_bool(self, option_name, expand=None, default=None):
498
"""Get a generic option as a boolean.
500
:param expand: Allow expanding references to other config values.
501
:param default: Default value if nothing is configured
502
:return None if the option doesn't exist or its value can't be
503
interpreted as a boolean. Returns True or False otherwise.
505
s = self.get_user_option(option_name, expand=expand)
507
# The option doesn't exist
509
val = ui.bool_from_string(s)
511
# The value can't be interpreted as a boolean
512
trace.warning('Value "%s" is not a boolean for "%s"',
516
def get_user_option_as_list(self, option_name, expand=None):
517
"""Get a generic option as a list - no special process, no default.
519
:return None if the option doesn't exist. Returns the value as a list
522
l = self.get_user_option(option_name, expand=expand)
523
if isinstance(l, string_types):
524
# A single value, most probably the user forgot (or didn't care to
529
def _log_format(self):
530
"""See log_format()."""
533
def validate_signatures_in_log(self):
534
"""Show GPG signature validity in log"""
535
result = self._validate_signatures_in_log()
83
def get_user_option(self, option_name):
84
"""Get a generic option - no special process, no default."""
85
return self._get_user_option(option_name)
87
def gpg_signing_command(self):
88
"""What program should be used to sign signatures?"""
89
result = self._gpg_signing_command()
542
def _validate_signatures_in_log(self):
543
"""See validate_signatures_in_log()."""
94
def _gpg_signing_command(self):
95
"""See gpg_signing_command()."""
546
def _post_commit(self):
547
"""See Config.post_commit."""
99
super(Config, self).__init__()
550
101
def user_email(self):
551
102
"""Return just the email component of a username."""
552
return extract_email_address(self.username())
104
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
106
raise BzrError("%r doesn't seem to contain "
107
"a reasonable email address" % e)
554
110
def username(self):
555
111
"""Return email-style username.
557
113
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
559
$BRZ_EMAIL or $BZR_EMAIL can be set to override this, then
115
$BZREMAIL can be set to override this, then
560
116
the concrete policy type is checked, and finally
562
If no username can be found, NoWhoami exception is raised.
118
but if none is found, a reasonable default is (hopefully)
121
TODO: Check it's reasonably well-formed.
564
v = os.environ.get('BRZ_EMAIL') or os.environ.get('BZR_EMAIL')
123
v = os.environ.get('BZREMAIL')
567
v = v.decode(osutils.get_user_encoding())
125
return v.decode(bzrlib.user_encoding)
569
127
v = self._get_user_id()
572
return bedding.default_email()
574
def get_alias(self, value):
575
return self._get_alias(value)
577
def _get_alias(self, value):
580
def get_nickname(self):
581
return self._get_nickname()
583
def _get_nickname(self):
586
def get_bzr_remote_path(self):
588
return os.environ['BZR_REMOTE_PATH']
590
path = self.get_user_option("bzr_remote_path")
595
def suppress_warning(self, warning):
596
"""Should the warning be suppressed or emitted.
598
:param warning: The name of the warning being tested.
600
:returns: True if the warning should be suppressed, False otherwise.
602
warnings = self.get_user_option_as_list('suppress_warnings')
603
if warnings is None or warning not in warnings:
131
v = os.environ.get('EMAIL')
133
return v.decode(bzrlib.user_encoding)
135
name, email = _auto_user_id()
137
return '%s <%s>' % (name, email)
141
def signature_checking(self):
142
"""What is the current policy for signature checking?."""
143
policy = self._get_signature_checking()
144
if policy is not None:
146
return CHECK_IF_POSSIBLE
148
def signature_needed(self):
149
"""Is a signature needed when committing ?."""
150
policy = self._get_signature_checking()
151
if policy == CHECK_ALWAYS:
608
def get_merge_tools(self):
610
for (oname, value, section, conf_id, parser) in self._get_options():
611
if oname.startswith('bzr.mergetool.'):
612
tool_name = oname[len('bzr.mergetool.'):]
613
tools[tool_name] = self.get_user_option(oname, False)
614
trace.mutter('loaded merge tools: %r' % tools)
617
def find_merge_tool(self, name):
618
# We fake a defaults mechanism here by checking if the given name can
619
# be found in the known_merge_tools if it's not found in the config.
620
# This should be done through the proposed config defaults mechanism
621
# when it becomes available in the future.
622
command_line = (self.get_user_option('bzr.mergetool.%s' % name,
624
mergetools.known_merge_tools.get(name, None))
628
class _ConfigHooks(hooks.Hooks):
629
"""A dict mapping hook names and a list of callables for configs.
633
"""Create the default hooks.
635
These are all empty initially, because by default nothing should get
638
super(_ConfigHooks, self).__init__('breezy.config', 'ConfigHooks')
639
self.add_hook('load',
640
'Invoked when a config store is loaded.'
641
' The signature is (store).',
643
self.add_hook('save',
644
'Invoked when a config store is saved.'
645
' The signature is (store).',
647
# The hooks for config options
649
'Invoked when a config option is read.'
650
' The signature is (stack, name, value).',
653
'Invoked when a config option is set.'
654
' The signature is (stack, name, value).',
656
self.add_hook('remove',
657
'Invoked when a config option is removed.'
658
' The signature is (stack, name).',
662
ConfigHooks = _ConfigHooks()
665
class _OldConfigHooks(hooks.Hooks):
666
"""A dict mapping hook names and a list of callables for configs.
670
"""Create the default hooks.
672
These are all empty initially, because by default nothing should get
675
super(_OldConfigHooks, self).__init__(
676
'breezy.config', 'OldConfigHooks')
677
self.add_hook('load',
678
'Invoked when a config store is loaded.'
679
' The signature is (config).',
681
self.add_hook('save',
682
'Invoked when a config store is saved.'
683
' The signature is (config).',
685
# The hooks for config options
687
'Invoked when a config option is read.'
688
' The signature is (config, name, value).',
691
'Invoked when a config option is set.'
692
' The signature is (config, name, value).',
694
self.add_hook('remove',
695
'Invoked when a config option is removed.'
696
' The signature is (config, name).',
700
OldConfigHooks = _OldConfigHooks()
703
156
class IniBasedConfig(Config):
704
157
"""A configuration policy that draws from ini files."""
706
def __init__(self, file_name=None):
707
"""Base class for configuration files using an ini-like syntax.
709
:param file_name: The configuration file path.
711
super(IniBasedConfig, self).__init__()
712
self.file_name = file_name
713
self.file_name = file_name
718
def from_string(cls, str_or_unicode, file_name=None, save=False):
719
"""Create a config object from a string.
721
:param str_or_unicode: A string representing the file content. This
722
will be utf-8 encoded.
724
:param file_name: The configuration file path.
726
:param _save: Whether the file should be saved upon creation.
728
conf = cls(file_name=file_name)
729
conf._create_from_string(str_or_unicode, save)
732
def _create_from_string(self, str_or_unicode, save):
733
if isinstance(str_or_unicode, text_type):
734
str_or_unicode = str_or_unicode.encode('utf-8')
735
self._content = BytesIO(str_or_unicode)
736
# Some tests use in-memory configs, some other always need the config
737
# file to exist on disk.
739
self._write_config_file()
741
def _get_parser(self):
159
def _get_parser(self, file=None):
742
160
if self._parser is not None:
743
161
return self._parser
744
if self._content is not None:
745
co_input = self._content
746
elif self.file_name is None:
747
raise AssertionError('We have no content to create the config')
749
co_input = self.file_name
751
self._parser = ConfigObj(co_input, encoding='utf-8')
752
except configobj.ConfigObjError as e:
753
raise ParseConfigError(e.errors, e.config.filename)
754
except UnicodeDecodeError:
755
raise ConfigContentError(self.file_name)
756
# Make sure self.reload() will use the right file name
757
self._parser.filename = self.file_name
758
for hook in OldConfigHooks['load']:
763
"""Reload the config file from disk."""
764
if self.file_name is None:
765
raise AssertionError('We need a file name to reload the config')
766
if self._parser is not None:
767
self._parser.reload()
768
for hook in ConfigHooks['load']:
771
def _get_matching_sections(self):
772
"""Return an ordered list of (section_name, extra_path) pairs.
774
If the section contains inherited configuration, extra_path is
775
a string containing the additional path components.
777
section = self._get_section()
778
if section is not None:
779
return [(section, '')]
162
parser = ConfigParser()
166
parser.read([self._get_filename()])
167
self._parser = parser
783
170
def _get_section(self):
784
171
"""Override this to define the section used by the config."""
787
def _get_sections(self, name=None):
788
"""Returns an iterator of the sections specified by ``name``.
790
:param name: The section name. If None is supplied, the default
791
configurations are yielded.
793
:return: A tuple (name, section, config_id) for all sections that will
794
be walked by user_get_option() in the 'right' order. The first one
795
is where set_user_option() will update the value.
797
parser = self._get_parser()
799
yield (name, parser[name], self.config_id())
801
# No section name has been given so we fallback to the configobj
802
# itself which holds the variables defined outside of any section.
803
yield (None, parser, self.config_id())
805
def _get_options(self, sections=None):
806
"""Return an ordered list of (name, value, section, config_id) tuples.
808
All options are returned with their associated value and the section
809
they appeared in. ``config_id`` is a unique identifier for the
810
configuration file the option is defined in.
812
:param sections: Default to ``_get_matching_sections`` if not
813
specified. This gives a better control to daughter classes about
814
which sections should be searched. This is a list of (name,
818
parser = self._get_parser()
820
for (section_name, _) in self._get_matching_sections():
822
section = parser[section_name]
824
# This could happen for an empty file for which we define a
825
# DEFAULT section. FIXME: Force callers to provide sections
826
# instead ? -- vila 20100930
828
sections.append((section_name, section))
829
config_id = self.config_id()
830
for (section_name, section) in sections:
831
for (name, value) in section.iteritems():
832
yield (name, parser._quote(value), section_name,
835
def _get_option_policy(self, section, option_name):
836
"""Return the policy for the given (section, option_name) pair."""
839
def _get_change_editor(self):
840
return self.get_user_option('change_editor', expand=False)
842
174
def _get_signature_checking(self):
843
175
"""See Config._get_signature_checking."""
844
policy = self._get_user_option('check_signatures')
846
return signature_policy_from_unicode(policy)
848
def _get_signing_policy(self):
849
"""See Config._get_signing_policy"""
850
policy = self._get_user_option('create_signatures')
852
return signing_policy_from_unicode(policy)
176
section = self._get_section()
179
if self._get_parser().has_option(section, 'check_signatures'):
180
return self._string_to_signature_policy(
181
self._get_parser().get(section, 'check_signatures'))
854
183
def _get_user_id(self):
855
184
"""Get the user id from the 'email' key in the current section."""
856
return self._get_user_option('email')
185
section = self._get_section()
186
if section is not None:
187
if self._get_parser().has_option(section, 'email'):
188
return self._get_parser().get(section, 'email')
858
190
def _get_user_option(self, option_name):
859
191
"""See Config._get_user_option."""
860
for (section, extra_path) in self._get_matching_sections():
862
value = self._get_parser().get_value(section, option_name)
865
policy = self._get_option_policy(section, option_name)
866
if policy == POLICY_NONE:
868
elif policy == POLICY_NORECURSE:
869
# norecurse items only apply to the exact path
874
elif policy == POLICY_APPENDPATH:
876
value = urlutils.join(value, extra_path)
879
raise AssertionError('Unexpected config policy %r' % policy)
883
def _log_format(self):
884
"""See Config.log_format."""
885
return self._get_user_option('log_format')
887
def _validate_signatures_in_log(self):
888
"""See Config.validate_signatures_in_log."""
889
return self._get_user_option('validate_signatures_in_log')
891
def _acceptable_keys(self):
892
"""See Config.acceptable_keys."""
893
return self._get_user_option('acceptable_keys')
895
def _post_commit(self):
896
"""See Config.post_commit."""
897
return self._get_user_option('post_commit')
899
def _get_alias(self, value):
901
return self._get_parser().get_value("ALIASES",
906
def _get_nickname(self):
907
return self.get_user_option('nickname')
909
def remove_user_option(self, option_name, section_name=None):
910
"""Remove a user option and save the configuration file.
912
:param option_name: The option to be removed.
914
:param section_name: The section the option is defined in, default to
918
parser = self._get_parser()
919
if section_name is None:
922
section = parser[section_name]
924
del section[option_name]
926
raise NoSuchConfigOption(option_name)
927
self._write_config_file()
928
for hook in OldConfigHooks['remove']:
929
hook(self, option_name)
931
def _write_config_file(self):
932
if self.file_name is None:
933
raise AssertionError('We cannot save, self.file_name is None')
934
conf_dir = os.path.dirname(self.file_name)
935
bedding.ensure_config_dir_exists(conf_dir)
936
with atomicfile.AtomicFile(self.file_name) as atomic_file:
937
self._get_parser().write(atomic_file)
938
osutils.copy_ownership_from_path(self.file_name)
939
for hook in OldConfigHooks['save']:
943
class LockableConfig(IniBasedConfig):
944
"""A configuration needing explicit locking for access.
946
If several processes try to write the config file, the accesses need to be
949
Daughter classes should use the self.lock_write() decorator method when
950
they upate a config (they call, directly or indirectly, the
951
``_write_config_file()`` method. These methods (typically ``set_option()``
952
and variants must reload the config file from disk before calling
953
``_write_config_file()``), this can be achieved by calling the
954
``self.reload()`` method. Note that the lock scope should cover both the
955
reading and the writing of the config file which is why the decorator can't
956
be applied to ``_write_config_file()`` only.
958
This should be enough to implement the following logic:
959
- lock for exclusive write access,
960
- reload the config file from disk,
964
This logic guarantees that a writer can update a value without erasing an
965
update made by another writer.
970
def __init__(self, file_name):
971
super(LockableConfig, self).__init__(file_name=file_name)
972
self.dir = osutils.dirname(osutils.safe_unicode(self.file_name))
973
# FIXME: It doesn't matter that we don't provide possible_transports
974
# below since this is currently used only for local config files ;
975
# local transports are not shared. But if/when we start using
976
# LockableConfig for other kind of transports, we will need to reuse
977
# whatever connection is already established -- vila 20100929
978
self.transport = transport.get_transport_from_path(self.dir)
979
self._lock = lockdir.LockDir(self.transport, self.lock_name)
981
def _create_from_string(self, unicode_bytes, save):
982
super(LockableConfig, self)._create_from_string(unicode_bytes, False)
984
# We need to handle the saving here (as opposed to IniBasedConfig)
987
self._write_config_file()
990
def lock_write(self, token=None):
991
"""Takes a write lock in the directory containing the config file.
993
If the directory doesn't exist it is created.
995
bedding.ensure_config_dir_exists(self.dir)
996
token = self._lock.lock_write(token)
997
return lock.LogicalLockResult(self.unlock, token)
1002
def break_lock(self):
1003
self._lock.break_lock()
1005
def remove_user_option(self, option_name, section_name=None):
1006
with self.lock_write():
1007
super(LockableConfig, self).remove_user_option(
1008
option_name, section_name)
1010
def _write_config_file(self):
1011
if self._lock is None or not self._lock.is_held:
1012
# NB: if the following exception is raised it probably means a
1013
# missing call to lock_write() by one of the callers.
1014
raise errors.ObjectNotLocked(self)
1015
super(LockableConfig, self)._write_config_file()
1018
class GlobalConfig(LockableConfig):
192
section = self._get_section()
193
if section is not None:
194
if self._get_parser().has_option(section, option_name):
195
return self._get_parser().get(section, option_name)
197
def _gpg_signing_command(self):
198
"""See Config.gpg_signing_command."""
199
section = self._get_section()
200
if section is not None:
201
if self._get_parser().has_option(section, 'gpg_signing_command'):
202
return self._get_parser().get(section, 'gpg_signing_command')
204
def __init__(self, get_filename):
205
super(IniBasedConfig, self).__init__()
206
self._get_filename = get_filename
209
def _string_to_signature_policy(self, signature_string):
210
"""Convert a string to a signing policy."""
211
if signature_string.lower() == 'check-available':
212
return CHECK_IF_POSSIBLE
213
if signature_string.lower() == 'ignore':
215
if signature_string.lower() == 'require':
217
raise errors.BzrError("Invalid signatures policy '%s'"
221
class GlobalConfig(IniBasedConfig):
1019
222
"""The configuration that should be used for a specific location."""
224
def get_editor(self):
225
if self._get_parser().has_option(self._get_section(), 'editor'):
226
return self._get_parser().get(self._get_section(), 'editor')
1021
228
def __init__(self):
1022
super(GlobalConfig, self).__init__(file_name=bedding.config_path())
1024
def config_id(self):
1028
def from_string(cls, str_or_unicode, save=False):
1029
"""Create a config object from a string.
1031
:param str_or_unicode: A string representing the file content. This
1032
will be utf-8 encoded.
1034
:param save: Whether the file should be saved upon creation.
229
super(GlobalConfig, self).__init__(config_filename)
232
class LocationConfig(IniBasedConfig):
233
"""A configuration object that gives the policy for a location."""
235
def __init__(self, location):
236
super(LocationConfig, self).__init__(branches_config_filename)
237
self._global_config = None
238
self.location = location
240
def _get_global_config(self):
241
if self._global_config is None:
242
self._global_config = GlobalConfig()
243
return self._global_config
245
def _get_section(self):
246
"""Get the section we should look in for config items.
248
Returns None if none exists.
249
TODO: perhaps return a NullSection that thunks through to the
1037
conf._create_from_string(str_or_unicode, save)
1040
def set_user_option(self, option, value):
1041
"""Save option and its value in the configuration."""
1042
with self.lock_write():
1043
self._set_option(option, value, 'DEFAULT')
1045
def get_aliases(self):
1046
"""Return the aliases section."""
1047
if 'ALIASES' in self._get_parser():
1048
return self._get_parser()['ALIASES']
1052
def set_alias(self, alias_name, alias_command):
1053
"""Save the alias in the configuration."""
1054
with self.lock_write():
1055
self._set_option(alias_name, alias_command, 'ALIASES')
1057
def unset_alias(self, alias_name):
1058
"""Unset an existing alias."""
1059
with self.lock_write():
1061
aliases = self._get_parser().get('ALIASES')
1062
if not aliases or alias_name not in aliases:
1063
raise errors.NoSuchAlias(alias_name)
1064
del aliases[alias_name]
1065
self._write_config_file()
1067
def _set_option(self, option, value, section):
1069
self._get_parser().setdefault(section, {})[option] = value
1070
self._write_config_file()
1071
for hook in OldConfigHooks['set']:
1072
hook(self, option, value)
1074
def _get_sections(self, name=None):
1075
"""See IniBasedConfig._get_sections()."""
1076
parser = self._get_parser()
1077
# We don't give access to options defined outside of any section, we
1078
# used the DEFAULT section by... default.
1079
if name in (None, 'DEFAULT'):
1080
# This could happen for an empty file where the DEFAULT section
1081
# doesn't exist yet. So we force DEFAULT when yielding
1083
if 'DEFAULT' not in parser:
1084
parser['DEFAULT'] = {}
1085
yield (name, parser[name], self.config_id())
1087
def remove_user_option(self, option_name, section_name=None):
1088
if section_name is None:
1089
# We need to force the default section.
1090
section_name = 'DEFAULT'
1091
with self.lock_write():
1092
# We need to avoid the LockableConfig implementation or we'll lock
1094
super(LockableConfig, self).remove_user_option(
1095
option_name, section_name)
1098
def _iter_for_location_by_parts(sections, location):
1099
"""Keep only the sessions matching the specified location.
1101
:param sections: An iterable of section names.
1103
:param location: An url or a local path to match against.
1105
:returns: An iterator of (section, extra_path, nb_parts) where nb is the
1106
number of path components in the section name, section is the section
1107
name and extra_path is the difference between location and the section
1110
``location`` will always be a local path and never a 'file://' url but the
1111
section names themselves can be in either form.
1113
location_parts = location.rstrip('/').split('/')
1115
for section in sections:
1116
# location is a local path if possible, so we need to convert 'file://'
1117
# urls in section names to local paths if necessary.
1119
# This also avoids having file:///path be a more exact
1120
# match than '/path'.
1122
# FIXME: This still raises an issue if a user defines both file:///path
1123
# *and* /path. Should we raise an error in this case -- vila 20110505
1125
if section.startswith('file://'):
1126
section_path = urlutils.local_path_from_url(section)
1128
section_path = section
1129
section_parts = section_path.rstrip('/').split('/')
1132
if len(section_parts) > len(location_parts):
1133
# More path components in the section, they can't match
1136
# Rely on zip truncating in length to the length of the shortest
1137
# argument sequence.
1138
for name in zip(location_parts, section_parts):
1139
if not fnmatch.fnmatch(name[0], name[1]):
252
sections = self._get_parser().sections()
253
location_names = self.location.split('/')
254
if self.location.endswith('/'):
255
del location_names[-1]
257
for section in sections:
258
section_names = section.split('/')
259
if section.endswith('/'):
260
del section_names[-1]
261
names = zip(location_names, section_names)
264
if not fnmatch(name[0], name[1]):
1144
# build the path difference between the section and the location
1145
extra_path = '/'.join(location_parts[len(section_parts):])
1146
yield section, extra_path, len(section_parts)
1149
class LocationConfig(LockableConfig):
1150
"""A configuration object that gives the policy for a location."""
1152
def __init__(self, location):
1153
super(LocationConfig, self).__init__(
1154
file_name=bedding.locations_config_path())
1155
# local file locations are looked up by local path, rather than
1156
# by file url. This is because the config file is a user
1157
# file, and we would rather not expose the user to file urls.
1158
if location.startswith('file://'):
1159
location = urlutils.local_path_from_url(location)
1160
self.location = location
1162
def config_id(self):
1166
def from_string(cls, str_or_unicode, location, save=False):
1167
"""Create a config object from a string.
1169
:param str_or_unicode: A string representing the file content. This will
1172
:param location: The location url to filter the configuration.
1174
:param save: Whether the file should be saved upon creation.
1176
conf = cls(location)
1177
conf._create_from_string(str_or_unicode, save)
1180
def _get_matching_sections(self):
1181
"""Return an ordered list of section names matching this location."""
1182
# put the longest (aka more specific) locations first
1184
_iter_for_location_by_parts(self._get_parser(), self.location),
1185
key=lambda match: (match[2], match[0]),
1187
for (section, extra_path, length) in matches:
1188
yield section, extra_path
1189
# should we stop looking for parent configs here?
1191
if self._get_parser()[section].as_bool('ignore_parents'):
1196
def _get_sections(self, name=None):
1197
"""See IniBasedConfig._get_sections()."""
1198
# We ignore the name here as the only sections handled are named with
1199
# the location path and we don't expose embedded sections either.
1200
parser = self._get_parser()
1201
for name, extra_path in self._get_matching_sections():
1202
yield (name, parser[name], self.config_id())
1204
def _get_option_policy(self, section, option_name):
1205
"""Return the policy for the given (section, option_name) pair."""
1206
# check for the old 'recurse=False' flag
1208
recurse = self._get_parser()[section].as_bool('recurse')
1212
return POLICY_NORECURSE
1214
policy_key = option_name + ':policy'
1216
policy_name = self._get_parser()[section][policy_key]
1220
return _policy_value[policy_name]
1222
def _set_option_policy(self, section, option_name, option_policy):
1223
"""Set the policy for the given option name in the given section."""
1224
policy_key = option_name + ':policy'
1225
policy_name = _policy_name[option_policy]
1226
if policy_name is not None:
1227
self._get_parser()[section][policy_key] = policy_name
1229
if policy_key in self._get_parser()[section]:
1230
del self._get_parser()[section][policy_key]
1232
def set_user_option(self, option, value, store=STORE_LOCATION):
1233
"""Save option and its value in the configuration."""
1234
if store not in [STORE_LOCATION,
1235
STORE_LOCATION_NORECURSE,
1236
STORE_LOCATION_APPENDPATH]:
1237
raise ValueError('bad storage policy %r for %r' %
1239
with self.lock_write():
1241
location = self.location
1242
if location.endswith('/'):
1243
location = location[:-1]
1244
parser = self._get_parser()
1245
if location not in parser and not location + '/' in parser:
1246
parser[location] = {}
1247
elif location + '/' in parser:
1248
location = location + '/'
1249
parser[location][option] = value
1250
# the allowed values of store match the config policies
1251
self._set_option_policy(location, option, store)
1252
self._write_config_file()
1253
for hook in OldConfigHooks['set']:
1254
hook(self, option, value)
269
# so, for the common prefix they matched.
270
# if section is longer, no match.
271
if len(section_names) > len(location_names):
273
# if path is longer, and recurse is not true, no match
274
if len(section_names) < len(location_names):
275
if (self._get_parser().has_option(section, 'recurse')
276
and not self._get_parser().getboolean(section, 'recurse')):
278
matches.append((len(section_names), section))
281
matches.sort(reverse=True)
284
def _gpg_signing_command(self):
285
"""See Config.gpg_signing_command."""
286
command = super(LocationConfig, self)._gpg_signing_command()
287
if command is not None:
289
return self._get_global_config()._gpg_signing_command()
291
def _get_user_id(self):
292
user_id = super(LocationConfig, self)._get_user_id()
293
if user_id is not None:
295
return self._get_global_config()._get_user_id()
297
def _get_user_option(self, option_name):
298
"""See Config._get_user_option."""
299
option_value = super(LocationConfig,
300
self)._get_user_option(option_name)
301
if option_value is not None:
303
return self._get_global_config()._get_user_option(option_name)
305
def _get_signature_checking(self):
306
"""See Config._get_signature_checking."""
307
check = super(LocationConfig, self)._get_signature_checking()
308
if check is not None:
310
return self._get_global_config()._get_signature_checking()
1257
313
class BranchConfig(Config):
1258
314
"""A configuration object giving the policy for a branch."""
1260
def __init__(self, branch):
1261
super(BranchConfig, self).__init__()
1262
self._location_config = None
1263
self._branch_data_config = None
1264
self._global_config = None
1265
self.branch = branch
1266
self.option_sources = (self._get_location_config,
1267
self._get_branch_data_config,
1268
self._get_global_config)
1270
def config_id(self):
1273
def _get_branch_data_config(self):
1274
if self._branch_data_config is None:
1275
self._branch_data_config = TreeConfig(self.branch)
1276
self._branch_data_config.config_id = self.config_id
1277
return self._branch_data_config
1279
316
def _get_location_config(self):
1280
317
if self._location_config is None:
1281
318
self._location_config = LocationConfig(self.branch.base)
1282
319
return self._location_config
1284
def _get_global_config(self):
1285
if self._global_config is None:
1286
self._global_config = GlobalConfig()
1287
return self._global_config
1289
def _get_best_value(self, option_name):
1290
"""This returns a user option from local, tree or global config.
1292
They are tried in that order. Use get_safe_value if trusted values
1295
for source in self.option_sources:
1296
value = getattr(source(), option_name)()
1297
if value is not None:
1301
def _get_safe_value(self, option_name):
1302
"""This variant of get_best_value never returns untrusted values.
1304
It does not return values from the branch data, because the branch may
1305
not be controlled by the user.
1307
We may wish to allow locations.conf to control whether branches are
1308
trusted in the future.
1310
for source in (self._get_location_config, self._get_global_config):
1311
value = getattr(source(), option_name)()
1312
if value is not None:
1316
321
def _get_user_id(self):
1317
322
"""Return the full user id for the branch.
1319
e.g. "John Hacker <jhacker@example.com>"
324
e.g. "John Hacker <jhacker@foo.org>"
1320
325
This is looked up in the email controlfile for the branch.
1322
return self._get_best_value('_get_user_id')
1324
def _get_change_editor(self):
1325
return self._get_best_value('_get_change_editor')
328
return (self.branch.controlfile("email", "r")
330
.decode(bzrlib.user_encoding)
332
except errors.NoSuchFile, e:
335
return self._get_location_config()._get_user_id()
1327
337
def _get_signature_checking(self):
1328
338
"""See Config._get_signature_checking."""
1329
return self._get_best_value('_get_signature_checking')
1331
def _get_signing_policy(self):
1332
"""See Config._get_signing_policy."""
1333
return self._get_best_value('_get_signing_policy')
339
return self._get_location_config()._get_signature_checking()
1335
341
def _get_user_option(self, option_name):
1336
342
"""See Config._get_user_option."""
1337
for source in self.option_sources:
1338
value = source()._get_user_option(option_name)
1339
if value is not None:
1343
def _get_sections(self, name=None):
1344
"""See IniBasedConfig.get_sections()."""
1345
for source in self.option_sources:
1346
for section in source()._get_sections(name):
1349
def _get_options(self, sections=None):
1350
# First the locations options
1351
for option in self._get_location_config()._get_options():
1353
# Then the branch options
1354
branch_config = self._get_branch_data_config()
1355
if sections is None:
1356
sections = [('DEFAULT', branch_config._get_parser())]
1357
# FIXME: We shouldn't have to duplicate the code in IniBasedConfig but
1358
# Config itself has no notion of sections :( -- vila 20101001
1359
config_id = self.config_id()
1360
for (section_name, section) in sections:
1361
for (name, value) in section.iteritems():
1362
yield (name, value, section_name,
1363
config_id, branch_config._get_parser())
1364
# Then the global options
1365
for option in self._get_global_config()._get_options():
1368
def set_user_option(self, name, value, store=STORE_BRANCH,
1370
if store == STORE_BRANCH:
1371
self._get_branch_data_config().set_option(value, name)
1372
elif store == STORE_GLOBAL:
1373
self._get_global_config().set_user_option(name, value)
343
return self._get_location_config()._get_user_option(option_name)
345
def _gpg_signing_command(self):
346
"""See Config.gpg_signing_command."""
347
return self._get_location_config()._gpg_signing_command()
349
def __init__(self, branch):
350
super(BranchConfig, self).__init__()
351
self._location_config = None
356
"""Return per-user configuration directory.
358
By default this is ~/.bazaar/
360
TODO: Global option --config-dir to override this.
362
return os.path.join(os.path.expanduser("~"), ".bazaar")
365
def config_filename():
366
"""Return per-user configuration ini file filename."""
367
return os.path.join(config_dir(), 'bazaar.conf')
370
def branches_config_filename():
371
"""Return per-user configuration ini file filename."""
372
return os.path.join(config_dir(), 'branches.conf')
376
"""Calculate automatic user identification.
378
Returns (realname, email).
380
Only used when none is set in the environment or the id file.
382
This previously used the FQDN as the default domain, but that can
383
be very slow on machines where DNS is broken. So now we simply
388
# XXX: Any good way to get real user name on win32?
393
w = pwd.getpwuid(uid)
394
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
395
username = w.pw_name.decode(bzrlib.user_encoding)
396
comma = gecos.find(',')
1375
self._get_location_config().set_user_option(name, value, store)
1378
if store in (STORE_GLOBAL, STORE_BRANCH):
1379
mask_value = self._get_location_config().get_user_option(name)
1380
if mask_value is not None:
1381
trace.warning('Value "%s" is masked by "%s" from'
1382
' locations.conf', value, mask_value)
1384
if store == STORE_GLOBAL:
1385
branch_config = self._get_branch_data_config()
1386
mask_value = branch_config.get_user_option(name)
1387
if mask_value is not None:
1388
trace.warning('Value "%s" is masked by "%s" from'
1389
' branch.conf', value, mask_value)
1391
def remove_user_option(self, option_name, section_name=None):
1392
self._get_branch_data_config().remove_option(option_name, section_name)
1394
def _post_commit(self):
1395
"""See Config.post_commit."""
1396
return self._get_safe_value('_post_commit')
1398
def _get_nickname(self):
1399
value = self._get_explicit_nickname()
1400
if value is not None:
1402
if self.branch.name:
1403
return self.branch.name
1404
return urlutils.unescape(self.branch.base.split('/')[-2])
1406
def has_explicit_nickname(self):
1407
"""Return true if a nickname has been explicitly assigned."""
1408
return self._get_explicit_nickname() is not None
1410
def _get_explicit_nickname(self):
1411
return self._get_best_value('_get_nickname')
1413
def _log_format(self):
1414
"""See Config.log_format."""
1415
return self._get_best_value('_log_format')
1417
def _validate_signatures_in_log(self):
1418
"""See Config.validate_signatures_in_log."""
1419
return self._get_best_value('_validate_signatures_in_log')
1421
def _acceptable_keys(self):
1422
"""See Config.acceptable_keys."""
1423
return self._get_best_value('_acceptable_keys')
1426
def parse_username(username):
1427
"""Parse e-mail username and return a (name, address) tuple."""
1428
match = re.match(r'(.*?)\s*<?([\w+.-]+@[\w+.-]+)>?', username)
1430
return (username, '')
1431
return (match.group(1), match.group(2))
400
realname = gecos[:comma]
406
realname = username = getpass.getuser().decode(bzrlib.user_encoding)
408
return realname, (username + '@' + socket.gethostname())
1434
411
def extract_email_address(e):
1435
412
"""Return just the address part of an email string.
1437
That is just the user@domain part, nothing else.
414
That is just the user@domain part, nothing else.
1438
415
This part is required to contain only ascii characters.
1439
416
If it can't be extracted, raises an error.
1441
418
>>> extract_email_address('Jane Tester <jane@test.com>')
1444
name, email = parse_username(e)
1446
raise NoEmailInUsername(e)
1450
class TreeConfig(IniBasedConfig):
1451
"""Branch configuration data associated with its contents, not location"""
1453
# XXX: Really needs a better name, as this is not part of the tree!
1456
def __init__(self, branch):
1457
self._config = branch._get_config()
1458
self.branch = branch
1460
def _get_parser(self, file=None):
1461
if file is not None:
1462
return IniBasedConfig._get_parser(file)
1463
return self._config._get_configobj()
1465
def get_option(self, name, section=None, default=None):
1466
with self.branch.lock_read():
1467
return self._config.get_option(name, section, default)
1469
def set_option(self, value, name, section=None):
1470
"""Set a per-branch configuration option"""
1471
# FIXME: We shouldn't need to lock explicitly here but rather rely on
1472
# higher levels providing the right lock -- vila 20101004
1473
with self.branch.lock_write():
1474
self._config.set_option(value, name, section)
1476
def remove_option(self, option_name, section_name=None):
1477
# FIXME: We shouldn't need to lock explicitly here but rather rely on
1478
# higher levels providing the right lock -- vila 20101004
1479
with self.branch.lock_write():
1480
self._config.remove_option(option_name, section_name)
1483
_authentication_config_permission_errors = set()
1486
class AuthenticationConfig(object):
1487
"""The authentication configuration file based on a ini file.
1489
Implements the authentication.conf file described in
1490
doc/developers/authentication-ring.txt.
1493
def __init__(self, _file=None):
1494
self._config = None # The ConfigObj
1496
self._input = self._filename = bedding.authentication_config_path()
1497
self._check_permissions()
1499
# Tests can provide a string as _file
1500
self._filename = None
1503
def _get_config(self):
1504
if self._config is not None:
1507
# FIXME: Should we validate something here ? Includes: empty
1508
# sections are useless, at least one of
1509
# user/password/password_encoding should be defined, etc.
1511
# Note: the encoding below declares that the file itself is utf-8
1512
# encoded, but the values in the ConfigObj are always Unicode.
1513
self._config = ConfigObj(self._input, encoding='utf-8')
1514
except configobj.ConfigObjError as e:
1515
raise ParseConfigError(e.errors, e.config.filename)
1516
except UnicodeError:
1517
raise ConfigContentError(self._filename)
1520
def _check_permissions(self):
1521
"""Check permission of auth file are user read/write able only."""
1523
st = os.stat(self._filename)
1524
except OSError as e:
1525
if e.errno != errno.ENOENT:
1526
trace.mutter('Unable to stat %r: %r', self._filename, e)
1528
mode = stat.S_IMODE(st.st_mode)
1529
if ((stat.S_IXOTH | stat.S_IWOTH | stat.S_IROTH | stat.S_IXGRP
1530
| stat.S_IWGRP | stat.S_IRGRP) & mode):
1532
if (self._filename not in _authentication_config_permission_errors and
1533
not GlobalConfig().suppress_warning(
1534
'insecure_permissions')):
1535
trace.warning("The file '%s' has insecure "
1536
"file permissions. Saved passwords may be accessible "
1537
"by other users.", self._filename)
1538
_authentication_config_permission_errors.add(self._filename)
1541
"""Save the config file, only tests should use it for now."""
1542
conf_dir = os.path.dirname(self._filename)
1543
bedding.ensure_config_dir_exists(conf_dir)
1544
fd = os.open(self._filename, os.O_RDWR | os.O_CREAT, 0o600)
1546
f = os.fdopen(fd, 'wb')
1547
self._get_config().write(f)
1551
def _set_option(self, section_name, option_name, value):
1552
"""Set an authentication configuration option"""
1553
conf = self._get_config()
1554
section = conf.get(section_name)
1557
section = conf[section]
1558
section[option_name] = value
1561
def get_credentials(self, scheme, host, port=None, user=None, path=None,
1563
"""Returns the matching credentials from authentication.conf file.
1565
:param scheme: protocol
1567
:param host: the server address
1569
:param port: the associated port (optional)
1571
:param user: login (optional)
1573
:param path: the absolute path on the server (optional)
1575
:param realm: the http authentication realm (optional)
1577
:return: A dict containing the matching credentials or None.
1579
- name: the section name of the credentials in the
1580
authentication.conf file,
1581
- user: can't be different from the provided user if any,
1582
- scheme: the server protocol,
1583
- host: the server address,
1584
- port: the server port (can be None),
1585
- path: the absolute server path (can be None),
1586
- realm: the http specific authentication realm (can be None),
1587
- password: the decoded password, could be None if the credential
1588
defines only the user
1589
- verify_certificates: https specific, True if the server
1590
certificate should be verified, False otherwise.
1593
for auth_def_name, auth_def in self._get_config().iteritems():
1594
if not isinstance(auth_def, configobj.Section):
1595
raise ValueError("%s defined outside a section" %
1598
a_scheme, a_host, a_user, a_path = map(
1599
auth_def.get, ['scheme', 'host', 'user', 'path'])
1602
a_port = auth_def.as_int('port')
1606
raise ValueError("'port' not numeric in %s" % auth_def_name)
1608
a_verify_certificates = auth_def.as_bool('verify_certificates')
1610
a_verify_certificates = True
1613
"'verify_certificates' not boolean in %s" % auth_def_name)
1616
if a_scheme is not None and scheme != a_scheme:
1618
if a_host is not None:
1619
if not (host == a_host or
1620
(a_host.startswith('.') and host.endswith(a_host))):
1622
if a_port is not None and port != a_port:
1624
if (a_path is not None and path is not None and
1625
not path.startswith(a_path)):
1627
if (a_user is not None and user is not None and
1629
# Never contradict the caller about the user to be used
1634
# Prepare a credentials dictionary with additional keys
1635
# for the credential providers
1636
credentials = dict(name=auth_def_name,
1643
password=auth_def.get('password', None),
1644
verify_certificates=a_verify_certificates)
1645
# Decode the password in the credentials (or get one)
1646
self.decode_password(credentials,
1647
auth_def.get('password_encoding', None))
1648
if 'auth' in debug.debug_flags:
1649
trace.mutter("Using authentication section: %r", auth_def_name)
1652
if credentials is None:
1653
# No credentials were found in authentication.conf, try the fallback
1654
# credentials stores.
1655
credentials = credential_store_registry.get_fallback_credentials(
1656
scheme, host, port, user, path, realm)
1660
def set_credentials(self, name, host, user, scheme=None, password=None,
1661
port=None, path=None, verify_certificates=None,
1663
"""Set authentication credentials for a host.
1665
Any existing credentials with matching scheme, host, port and path
1666
will be deleted, regardless of name.
1668
:param name: An arbitrary name to describe this set of credentials.
1669
:param host: Name of the host that accepts these credentials.
1670
:param user: The username portion of these credentials.
1671
:param scheme: The URL scheme (e.g. ssh, http) the credentials apply
1673
:param password: Password portion of these credentials.
1674
:param port: The IP port on the host that these credentials apply to.
1675
:param path: A filesystem path on the host that these credentials
1677
:param verify_certificates: On https, verify server certificates if
1679
:param realm: The http authentication realm (optional).
1681
values = {'host': host, 'user': user}
1682
if password is not None:
1683
values['password'] = password
1684
if scheme is not None:
1685
values['scheme'] = scheme
1686
if port is not None:
1687
values['port'] = '%d' % port
1688
if path is not None:
1689
values['path'] = path
1690
if verify_certificates is not None:
1691
values['verify_certificates'] = str(verify_certificates)
1692
if realm is not None:
1693
values['realm'] = realm
1694
config = self._get_config()
1695
for section, existing_values in config.iteritems():
1696
for key in ('scheme', 'host', 'port', 'path', 'realm'):
1697
if existing_values.get(key) != values.get(key):
1701
config.update({name: values})
1704
def get_user(self, scheme, host, port=None, realm=None, path=None,
1705
prompt=None, ask=False, default=None):
1706
"""Get a user from authentication file.
1708
:param scheme: protocol
1710
:param host: the server address
1712
:param port: the associated port (optional)
1714
:param realm: the realm sent by the server (optional)
1716
:param path: the absolute path on the server (optional)
1718
:param ask: Ask the user if there is no explicitly configured username
1721
:param default: The username returned if none is defined (optional).
1723
:return: The found user.
1725
credentials = self.get_credentials(scheme, host, port, user=None,
1726
path=path, realm=realm)
1727
if credentials is not None:
1728
user = credentials['user']
1734
# Create a default prompt suitable for most cases
1735
prompt = u'%s' % (scheme.upper(),) + u' %(host)s username'
1736
# Special handling for optional fields in the prompt
1737
if port is not None:
1738
prompt_host = '%s:%d' % (host, port)
1741
user = ui.ui_factory.get_username(prompt, host=prompt_host)
1746
def get_password(self, scheme, host, user, port=None,
1747
realm=None, path=None, prompt=None):
1748
"""Get a password from authentication file or prompt the user for one.
1750
:param scheme: protocol
1752
:param host: the server address
1754
:param port: the associated port (optional)
1758
:param realm: the realm sent by the server (optional)
1760
:param path: the absolute path on the server (optional)
1762
:return: The found password or the one entered by the user.
1764
credentials = self.get_credentials(scheme, host, port, user, path,
1766
if credentials is not None:
1767
password = credentials['password']
1768
if password is not None and scheme == 'ssh':
1769
trace.warning('password ignored in section [%s],'
1770
' use an ssh agent instead'
1771
% credentials['name'])
1775
# Prompt user only if we could't find a password
1776
if password is None:
1778
# Create a default prompt suitable for most cases
1780
scheme.upper() + u' %(user)s@%(host)s password')
1781
# Special handling for optional fields in the prompt
1782
if port is not None:
1783
prompt_host = '%s:%d' % (host, port)
1786
password = ui.ui_factory.get_password(prompt,
1787
host=prompt_host, user=user)
1790
def decode_password(self, credentials, encoding):
1792
cs = credential_store_registry.get_credential_store(encoding)
1794
raise ValueError('%r is not a known password_encoding' % encoding)
1795
credentials['password'] = cs.decode_password(credentials)
1799
class CredentialStoreRegistry(registry.Registry):
1800
"""A class that registers credential stores.
1802
A credential store provides access to credentials via the password_encoding
1803
field in authentication.conf sections.
1805
Except for stores provided by brz itself, most stores are expected to be
1806
provided by plugins that will therefore use
1807
register_lazy(password_encoding, module_name, member_name, help=help,
1808
fallback=fallback) to install themselves.
1810
A fallback credential store is one that is queried if no credentials can be
1811
found via authentication.conf.
1814
def get_credential_store(self, encoding=None):
1815
cs = self.get(encoding)
1820
def is_fallback(self, name):
1821
"""Check if the named credentials store should be used as fallback."""
1822
return self.get_info(name)
1824
def get_fallback_credentials(self, scheme, host, port=None, user=None,
1825
path=None, realm=None):
1826
"""Request credentials from all fallback credentials stores.
1828
The first credentials store that can provide credentials wins.
1831
for name in self.keys():
1832
if not self.is_fallback(name):
1834
cs = self.get_credential_store(name)
1835
credentials = cs.get_credentials(scheme, host, port, user,
1837
if credentials is not None:
1838
# We found some credentials
1842
def register(self, key, obj, help=None, override_existing=False,
1844
"""Register a new object to a name.
1846
:param key: This is the key to use to request the object later.
1847
:param obj: The object to register.
1848
:param help: Help text for this entry. This may be a string or
1849
a callable. If it is a callable, it should take two
1850
parameters (registry, key): this registry and the key that
1851
the help was registered under.
1852
:param override_existing: Raise KeyErorr if False and something has
1853
already been registered for that key. If True, ignore if there
1854
is an existing key (always register the new value).
1855
:param fallback: Whether this credential store should be
1858
return super(CredentialStoreRegistry,
1859
self).register(key, obj, help, info=fallback,
1860
override_existing=override_existing)
1862
def register_lazy(self, key, module_name, member_name,
1863
help=None, override_existing=False,
1865
"""Register a new credential store to be loaded on request.
1867
:param module_name: The python path to the module. Such as 'os.path'.
1868
:param member_name: The member of the module to return. If empty or
1869
None, get() will return the module itself.
1870
:param help: Help text for this entry. This may be a string or
1872
:param override_existing: If True, replace the existing object
1873
with the new one. If False, if there is already something
1874
registered with the same key, raise a KeyError
1875
:param fallback: Whether this credential store should be
1878
return super(CredentialStoreRegistry, self).register_lazy(
1879
key, module_name, member_name, help,
1880
info=fallback, override_existing=override_existing)
1883
credential_store_registry = CredentialStoreRegistry()
1886
class CredentialStore(object):
1887
"""An abstract class to implement storage for credentials"""
1889
def decode_password(self, credentials):
1890
"""Returns a clear text password for the provided credentials."""
1891
raise NotImplementedError(self.decode_password)
1893
def get_credentials(self, scheme, host, port=None, user=None, path=None,
1895
"""Return the matching credentials from this credential store.
1897
This method is only called on fallback credential stores.
1899
raise NotImplementedError(self.get_credentials)
1902
class PlainTextCredentialStore(CredentialStore):
1903
__doc__ = """Plain text credential store for the authentication.conf file"""
1905
def decode_password(self, credentials):
1906
"""See CredentialStore.decode_password."""
1907
return credentials['password']
1910
credential_store_registry.register('plain', PlainTextCredentialStore,
1911
help=PlainTextCredentialStore.__doc__)
1912
credential_store_registry.default_key = 'plain'
1915
class Base64CredentialStore(CredentialStore):
1916
__doc__ = """Base64 credential store for the authentication.conf file"""
1918
def decode_password(self, credentials):
1919
"""See CredentialStore.decode_password."""
1920
# GZ 2012-07-28: Will raise binascii.Error if password is not base64,
1921
# should probably propogate as something more useful.
1922
return base64.standard_b64decode(credentials['password'])
1925
credential_store_registry.register('base64', Base64CredentialStore,
1926
help=Base64CredentialStore.__doc__)
1929
class BzrDirConfig(object):
1931
def __init__(self, bzrdir):
1932
self._bzrdir = bzrdir
1933
self._config = bzrdir._get_config()
1935
def set_default_stack_on(self, value):
1936
"""Set the default stacking location.
1938
It may be set to a location, or None.
1940
This policy affects all branches contained by this control dir, except
1941
for those under repositories.
1943
if self._config is None:
1944
raise errors.BzrError("Cannot set configuration in %s"
1947
self._config.set_option('', 'default_stack_on')
1949
self._config.set_option(value, 'default_stack_on')
1951
def get_default_stack_on(self):
1952
"""Return the default stacking location.
1954
This will either be a location, or None.
1956
This policy affects all branches contained by this control dir, except
1957
for those under repositories.
1959
if self._config is None:
1961
value = self._config.get_option('default_stack_on')
1967
class TransportConfig(object):
1968
"""A Config that reads/writes a config file on a Transport.
1970
It is a low-level object that considers config data to be name/value pairs
1971
that may be associated with a section. Assigning meaning to these values
1972
is done at higher levels like TreeConfig.
1975
def __init__(self, transport, filename):
1976
self._transport = transport
1977
self._filename = filename
1979
def get_option(self, name, section=None, default=None):
1980
"""Return the value associated with a named option.
1982
:param name: The name of the value
1983
:param section: The section the option is in (if any)
1984
:param default: The value to return if the value is not set
1985
:return: The value or default value
1987
configobj = self._get_configobj()
1989
section_obj = configobj
1992
section_obj = configobj[section]
1995
value = section_obj.get(name, default)
1996
for hook in OldConfigHooks['get']:
1997
hook(self, name, value)
2000
def set_option(self, value, name, section=None):
2001
"""Set the value associated with a named option.
2003
:param value: The value to set
2004
:param name: The name of the value to set
2005
:param section: The section the option is in (if any)
2007
configobj = self._get_configobj()
2009
configobj[name] = value
2011
configobj.setdefault(section, {})[name] = value
2012
for hook in OldConfigHooks['set']:
2013
hook(self, name, value)
2014
self._set_configobj(configobj)
2016
def remove_option(self, option_name, section_name=None):
2017
configobj = self._get_configobj()
2018
if section_name is None:
2019
del configobj[option_name]
2021
del configobj[section_name][option_name]
2022
for hook in OldConfigHooks['remove']:
2023
hook(self, option_name)
2024
self._set_configobj(configobj)
2026
def _get_config_file(self):
2028
f = BytesIO(self._transport.get_bytes(self._filename))
2029
for hook in OldConfigHooks['load']:
2032
except errors.NoSuchFile:
2034
except errors.PermissionDenied:
2036
"Permission denied while trying to open "
2037
"configuration file %s.",
2038
urlutils.unescape_for_display(
2039
urlutils.join(self._transport.base, self._filename),
2043
def _external_url(self):
2044
return urlutils.join(self._transport.external_url(), self._filename)
2046
def _get_configobj(self):
2047
f = self._get_config_file()
2050
conf = ConfigObj(f, encoding='utf-8')
2051
except configobj.ConfigObjError as e:
2052
raise ParseConfigError(e.errors, self._external_url())
2053
except UnicodeDecodeError:
2054
raise ConfigContentError(self._external_url())
2059
def _set_configobj(self, configobj):
2060
out_file = BytesIO()
2061
configobj.write(out_file)
2063
self._transport.put_file(self._filename, out_file)
2064
for hook in OldConfigHooks['save']:
2068
class Option(object):
2069
"""An option definition.
2071
The option *values* are stored in config files and found in sections.
2073
Here we define various properties about the option itself, its default
2074
value, how to convert it from stores, what to do when invalid values are
2075
encoutered, in which config files it can be stored.
2078
def __init__(self, name, override_from_env=None,
2079
default=None, default_from_env=None,
2080
help=None, from_unicode=None, invalid=None, unquote=True):
2081
"""Build an option definition.
2083
:param name: the name used to refer to the option.
2085
:param override_from_env: A list of environment variables which can
2086
provide override any configuration setting.
2088
:param default: the default value to use when none exist in the config
2089
stores. This is either a string that ``from_unicode`` will convert
2090
into the proper type, a callable returning a unicode string so that
2091
``from_unicode`` can be used on the return value, or a python
2092
object that can be stringified (so only the empty list is supported
2095
:param default_from_env: A list of environment variables which can
2096
provide a default value. 'default' will be used only if none of the
2097
variables specified here are set in the environment.
2099
:param help: a doc string to explain the option to the user.
2101
:param from_unicode: a callable to convert the unicode string
2102
representing the option value in a store or its default value.
2104
:param invalid: the action to be taken when an invalid value is
2105
encountered in a store. This is called only when from_unicode is
2106
invoked to convert a string and returns None or raise ValueError or
2107
TypeError. Accepted values are: None (ignore invalid values),
2108
'warning' (emit a warning), 'error' (emit an error message and
2111
:param unquote: should the unicode value be unquoted before conversion.
2112
This should be used only when the store providing the values cannot
2113
safely unquote them (see http://pad.lv/906897). It is provided so
2114
daughter classes can handle the quoting themselves.
2116
if override_from_env is None:
2117
override_from_env = []
2118
if default_from_env is None:
2119
default_from_env = []
2121
self.override_from_env = override_from_env
2122
# Convert the default value to a unicode string so all values are
2123
# strings internally before conversion (via from_unicode) is attempted.
2126
elif isinstance(default, list):
2127
# Only the empty list is supported
2129
raise AssertionError(
2130
'Only empty lists are supported as default values')
2132
elif isinstance(default, (binary_type, text_type, bool, int, float)):
2133
# Rely on python to convert strings, booleans and integers
2134
self.default = u'%s' % (default,)
2135
elif callable(default):
2136
self.default = default
2138
# other python objects are not expected
2139
raise AssertionError('%r is not supported as a default value'
2141
self.default_from_env = default_from_env
2143
self.from_unicode = from_unicode
2144
self.unquote = unquote
2145
if invalid and invalid not in ('warning', 'error'):
2146
raise AssertionError("%s not supported for 'invalid'" % (invalid,))
2147
self.invalid = invalid
2153
def convert_from_unicode(self, store, unicode_value):
2154
if self.unquote and store is not None and unicode_value is not None:
2155
unicode_value = store.unquote(unicode_value)
2156
if self.from_unicode is None or unicode_value is None:
2157
# Don't convert or nothing to convert
2158
return unicode_value
2160
converted = self.from_unicode(unicode_value)
2161
except (ValueError, TypeError):
2162
# Invalid values are ignored
2164
if converted is None and self.invalid is not None:
2165
# The conversion failed
2166
if self.invalid == 'warning':
2167
trace.warning('Value "%s" is not valid for "%s"',
2168
unicode_value, self.name)
2169
elif self.invalid == 'error':
2170
raise ConfigOptionValueError(self.name, unicode_value)
2173
def get_override(self):
2175
for var in self.override_from_env:
2177
# If the env variable is defined, its value takes precedence
2178
value = os.environ[var]
2180
value = value.decode(osutils.get_user_encoding())
2186
def get_default(self):
2188
for var in self.default_from_env:
2190
# If the env variable is defined, its value is the default one
2191
value = os.environ[var]
2193
value = value.decode(osutils.get_user_encoding())
2198
# Otherwise, fallback to the value defined at registration
2199
if callable(self.default):
2200
value = self.default()
2201
if not isinstance(value, text_type):
2202
raise AssertionError(
2203
"Callable default value for '%s' should be unicode"
2206
value = self.default
2209
def get_help_topic(self):
2212
def get_help_text(self, additional_see_also=None, plain=True):
2214
from breezy import help_topics
2215
result += help_topics._format_see_also(additional_see_also)
2217
result = help_topics.help_as_plain_text(result)
2221
# Predefined converters to get proper values from store
2223
def bool_from_store(unicode_str):
2224
return ui.bool_from_string(unicode_str)
2227
def int_from_store(unicode_str):
2228
return int(unicode_str)
2231
_unit_suffixes = dict(K=10**3, M=10**6, G=10**9)
2234
def int_SI_from_store(unicode_str):
2235
"""Convert a human readable size in SI units, e.g 10MB into an integer.
2237
Accepted suffixes are K,M,G. It is case-insensitive and may be followed
2238
by a trailing b (i.e. Kb, MB). This is intended to be practical and not
2241
:return Integer, expanded to its base-10 value if a proper SI unit is
2242
found, None otherwise.
2244
regexp = "^(\\d+)(([" + ''.join(_unit_suffixes) + "])b?)?$"
2245
p = re.compile(regexp, re.IGNORECASE)
2246
m = p.match(unicode_str)
2249
val, _, unit = m.groups()
2253
coeff = _unit_suffixes[unit.upper()]
2256
gettext('{0} is not an SI unit.').format(unit))
2261
def float_from_store(unicode_str):
2262
return float(unicode_str)
2265
# Use an empty dict to initialize an empty configobj avoiding all parsing and
2267
_list_converter_config = configobj.ConfigObj(
2268
{}, encoding='utf-8', list_values=True, interpolation=False)
2271
class ListOption(Option):
2273
def __init__(self, name, default=None, default_from_env=None,
2274
help=None, invalid=None):
2275
"""A list Option definition.
2277
This overrides the base class so the conversion from a unicode string
2278
can take quoting into account.
2280
super(ListOption, self).__init__(
2281
name, default=default, default_from_env=default_from_env,
2282
from_unicode=self.from_unicode, help=help,
2283
invalid=invalid, unquote=False)
2285
def from_unicode(self, unicode_str):
2286
if not isinstance(unicode_str, string_types):
2288
# Now inject our string directly as unicode. All callers got their
2289
# value from configobj, so values that need to be quoted are already
2291
_list_converter_config.reset()
2292
_list_converter_config._parse([u"list=%s" % (unicode_str,)])
2293
maybe_list = _list_converter_config['list']
2294
if isinstance(maybe_list, string_types):
2296
# A single value, most probably the user forgot (or didn't care
2297
# to add) the final ','
2300
# The empty string, convert to empty list
2303
# We rely on ConfigObj providing us with a list already
2308
class RegistryOption(Option):
2309
"""Option for a choice from a registry."""
2311
def __init__(self, name, registry, default_from_env=None,
2312
help=None, invalid=None):
2313
"""A registry based Option definition.
2315
This overrides the base class so the conversion from a unicode string
2316
can take quoting into account.
2318
super(RegistryOption, self).__init__(
2319
name, default=lambda: registry.default_key,
2320
default_from_env=default_from_env,
2321
from_unicode=self.from_unicode, help=help,
2322
invalid=invalid, unquote=False)
2323
self.registry = registry
2325
def from_unicode(self, unicode_str):
2326
if not isinstance(unicode_str, string_types):
2329
return self.registry.get(unicode_str)
2332
"Invalid value %s for %s."
2333
"See help for a list of possible values." % (unicode_str,
2338
ret = [self._help, "\n\nThe following values are supported:\n"]
2339
for key in self.registry.keys():
2340
ret.append(" %s - %s\n" % (key, self.registry.get_help(key)))
2344
_option_ref_re = lazy_regex.lazy_compile('({[^\\d\\W](?:\\.\\w|-\\w|\\w)*})')
2345
"""Describes an expandable option reference.
2347
We want to match the most embedded reference first.
2349
I.e. for '{{foo}}' we will get '{foo}',
2350
for '{bar{baz}}' we will get '{baz}'
2354
def iter_option_refs(string):
2355
# Split isolate refs so every other chunk is a ref
2357
for chunk in _option_ref_re.split(string):
2362
class OptionRegistry(registry.Registry):
2363
"""Register config options by their name.
2365
This overrides ``registry.Registry`` to simplify registration by acquiring
2366
some information from the option object itself.
2369
def _check_option_name(self, option_name):
2370
"""Ensures an option name is valid.
2372
:param option_name: The name to validate.
2374
if _option_ref_re.match('{%s}' % option_name) is None:
2375
raise IllegalOptionName(option_name)
2377
def register(self, option):
2378
"""Register a new option to its name.
2380
:param option: The option to register. Its name is used as the key.
2382
self._check_option_name(option.name)
2383
super(OptionRegistry, self).register(option.name, option,
2386
def register_lazy(self, key, module_name, member_name):
2387
"""Register a new option to be loaded on request.
2389
:param key: the key to request the option later. Since the registration
2390
is lazy, it should be provided and match the option name.
2392
:param module_name: the python path to the module. Such as 'os.path'.
2394
:param member_name: the member of the module to return. If empty or
2395
None, get() will return the module itself.
2397
self._check_option_name(key)
2398
super(OptionRegistry, self).register_lazy(key,
2399
module_name, member_name)
2401
def get_help(self, key=None):
2402
"""Get the help text associated with the given key"""
2403
option = self.get(key)
2404
the_help = option.help
2405
if callable(the_help):
2406
return the_help(self, key)
2410
option_registry = OptionRegistry()
2413
# Registered options in lexicographical order
2415
option_registry.register(
2416
Option('append_revisions_only',
2417
default=None, from_unicode=bool_from_store, invalid='warning',
2419
Whether to only append revisions to the mainline.
2421
If this is set to true, then it is not possible to change the
2422
existing mainline of the branch.
2424
option_registry.register(
2425
ListOption('acceptable_keys',
2428
List of GPG key patterns which are acceptable for verification.
2430
option_registry.register(
2431
Option('add.maximum_file_size',
2432
default=u'20MB', from_unicode=int_SI_from_store,
2434
Size above which files should be added manually.
2436
Files below this size are added automatically when using ``bzr add`` without
2439
A negative value means disable the size check.
2441
option_registry.register(
2443
default=None, from_unicode=bool_from_store,
2445
Is the branch bound to ``bound_location``.
2447
If set to "True", the branch should act as a checkout, and push each commit to
2448
the bound_location. This option is normally set by ``bind``/``unbind``.
2450
See also: bound_location.
2452
option_registry.register(
2453
Option('bound_location',
2456
The location that commits should go to when acting as a checkout.
2458
This option is normally set by ``bind``.
2462
option_registry.register(
2463
Option('branch.fetch_tags', default=False, from_unicode=bool_from_store,
2465
Whether revisions associated with tags should be fetched.
2467
option_registry.register_lazy(
2468
'transform.orphan_policy', 'breezy.transform', 'opt_transform_orphan')
2469
option_registry.register(
2470
Option('bzr.workingtree.worth_saving_limit', default=10,
2471
from_unicode=int_from_store, invalid='warning',
2473
How many changes before saving the dirstate.
2475
-1 means that we will never rewrite the dirstate file for only
2476
stat-cache changes. Regardless of this setting, we will always rewrite
2477
the dirstate file if a file is added/removed/renamed/etc. This flag only
2478
affects the behavior of updating the dirstate file after we notice that
2479
a file has been touched.
2481
option_registry.register(
2482
Option('bugtracker', default=None,
2484
Default bug tracker to use.
2486
This bug tracker will be used for example when marking bugs
2487
as fixed using ``bzr commit --fixes``, if no explicit
2488
bug tracker was specified.
2490
option_registry.register(
2491
Option('calculate_revnos', default=True,
2492
from_unicode=bool_from_store,
2494
Calculate revision numbers if they are not known.
2496
Always show revision numbers, even for branch formats that don't store them
2497
natively (such as Git). Calculating the revision number requires traversing
2498
the left hand ancestry of the branch and can be slow on very large branches.
2500
option_registry.register(
2501
Option('check_signatures', default=CHECK_IF_POSSIBLE,
2502
from_unicode=signature_policy_from_unicode,
2504
GPG checking policy.
2506
Possible values: require, ignore, check-available (default)
2508
this option will control whether bzr will require good gpg
2509
signatures, ignore them, or check them if they are
2512
option_registry.register(
2513
Option('child_submit_format',
2514
help='''The preferred format of submissions to this branch.'''))
2515
option_registry.register(
2516
Option('child_submit_to',
2517
help='''Where submissions to this branch are mailed to.'''))
2518
option_registry.register(
2519
Option('create_signatures', default=SIGN_WHEN_REQUIRED,
2520
from_unicode=signing_policy_from_unicode,
2524
Possible values: always, never, when-required (default)
2526
This option controls whether bzr will always create
2527
gpg signatures or not on commits.
2529
option_registry.register(
2530
Option('dirstate.fdatasync', default=True,
2531
from_unicode=bool_from_store,
2533
Flush dirstate changes onto physical disk?
2535
If true (default), working tree metadata changes are flushed through the
2536
OS buffers to physical disk. This is somewhat slower, but means data
2537
should not be lost if the machine crashes. See also repository.fdatasync.
2539
option_registry.register(
2540
ListOption('debug_flags', default=[],
2541
help='Debug flags to activate.'))
2542
option_registry.register(
2543
Option('default_format', default='2a',
2544
help='Format used when creating branches.'))
2545
option_registry.register(
2547
help='The command called to launch an editor to enter a message.'))
2548
option_registry.register(
2549
Option('email', override_from_env=['BRZ_EMAIL', 'BZR_EMAIL'],
2550
default=bedding.default_email, help='The users identity'))
2551
option_registry.register(
2552
Option('gpg_signing_key',
2555
GPG key to use for signing.
2557
This defaults to the first key associated with the users email.
2559
option_registry.register(
2561
help='Language to translate messages into.'))
2562
option_registry.register(
2563
Option('locks.steal_dead', default=True, from_unicode=bool_from_store,
2565
Steal locks that appears to be dead.
2567
If set to True, bzr will check if a lock is supposed to be held by an
2568
active process from the same user on the same machine. If the user and
2569
machine match, but no process with the given PID is active, then bzr
2570
will automatically break the stale lock, and create a new lock for
2572
Otherwise, bzr will prompt as normal to break the lock.
2574
option_registry.register(
2575
Option('log_format', default='long',
2577
Log format to use when displaying revisions.
2579
Standard log formats are ``long``, ``short`` and ``line``. Additional formats
2580
may be provided by plugins.
2582
option_registry.register_lazy('mail_client', 'breezy.mail_client',
2584
option_registry.register(
2585
Option('output_encoding',
2586
help='Unicode encoding for output'
2587
' (terminal encoding if not specified).'))
2588
option_registry.register(
2589
Option('parent_location',
2592
The location of the default branch for pull or merge.
2594
This option is normally set when creating a branch, the first ``pull`` or by
2595
``pull --remember``.
2597
option_registry.register(
2598
Option('post_commit', default=None,
2600
Post commit functions.
2602
An ordered list of python functions to call, separated by spaces.
2604
Each function takes branch, rev_id as parameters.
2606
option_registry.register_lazy('progress_bar', 'breezy.ui.text',
2608
option_registry.register(
2609
Option('public_branch',
2612
A publically-accessible version of this branch.
2614
This implies that the branch setting this option is not publically-accessible.
2615
Used and set by ``bzr send``.
2617
option_registry.register(
2618
Option('push_location',
2621
The location of the default branch for push.
2623
This option is normally set by the first ``push`` or ``push --remember``.
2625
option_registry.register(
2626
Option('push_strict', default=None,
2627
from_unicode=bool_from_store,
2629
The default value for ``push --strict``.
2631
If present, defines the ``--strict`` option default value for checking
2632
uncommitted changes before sending a merge directive.
2634
option_registry.register(
2635
Option('repository.fdatasync', default=True,
2636
from_unicode=bool_from_store,
2638
Flush repository changes onto physical disk?
2640
If true (default), repository changes are flushed through the OS buffers
2641
to physical disk. This is somewhat slower, but means data should not be
2642
lost if the machine crashes. See also dirstate.fdatasync.
2644
option_registry.register_lazy('smtp_server',
2645
'breezy.smtp_connection', 'smtp_server')
2646
option_registry.register_lazy('smtp_password',
2647
'breezy.smtp_connection', 'smtp_password')
2648
option_registry.register_lazy('smtp_username',
2649
'breezy.smtp_connection', 'smtp_username')
2650
option_registry.register(
2651
Option('selftest.timeout',
2653
from_unicode=int_from_store,
2654
help='Abort selftest if one test takes longer than this many seconds',
2657
option_registry.register(
2658
Option('send_strict', default=None,
2659
from_unicode=bool_from_store,
2661
The default value for ``send --strict``.
2663
If present, defines the ``--strict`` option default value for checking
2664
uncommitted changes before sending a bundle.
2667
option_registry.register(
2668
Option('serve.client_timeout',
2669
default=300.0, from_unicode=float_from_store,
2670
help="If we wait for a new request from a client for more than"
2671
" X seconds, consider the client idle, and hangup."))
2672
option_registry.register(
2674
default=None, override_from_env=['BRZ_SSH'],
2675
help='SSH vendor to use.'))
2676
option_registry.register(
2677
Option('stacked_on_location',
2679
help="""The location where this branch is stacked on."""))
2680
option_registry.register(
2681
Option('submit_branch',
2684
The branch you intend to submit your current work to.
2686
This is automatically set by ``bzr send`` and ``bzr merge``, and is also used
2687
by the ``submit:`` revision spec.
2689
option_registry.register(
2691
help='''Where submissions from this branch are mailed to.'''))
2692
option_registry.register(
2693
ListOption('suppress_warnings',
2695
help="List of warning classes to suppress."))
2696
option_registry.register(
2697
Option('validate_signatures_in_log', default=False,
2698
from_unicode=bool_from_store, invalid='warning',
2699
help='''Whether to validate signatures in brz log.'''))
2700
option_registry.register_lazy('ssl.ca_certs',
2701
'breezy.transport.http', 'opt_ssl_ca_certs')
2703
option_registry.register_lazy('ssl.cert_reqs',
2704
'breezy.transport.http', 'opt_ssl_cert_reqs')
2707
class Section(object):
2708
"""A section defines a dict of option name => value.
2710
This is merely a read-only dict which can add some knowledge about the
2711
options. It is *not* a python dict object though and doesn't try to mimic
2715
def __init__(self, section_id, options):
2716
self.id = section_id
2717
# We re-use the dict-like object received
2718
self.options = options
2720
def get(self, name, default=None, expand=True):
2721
return self.options.get(name, default)
2723
def iter_option_names(self):
2724
for k in self.options.keys():
2728
# Mostly for debugging use
2729
return "<config.%s id=%s>" % (self.__class__.__name__, self.id)
2732
_NewlyCreatedOption = object()
2733
"""Was the option created during the MutableSection lifetime"""
2734
_DeletedOption = object()
2735
"""Was the option deleted during the MutableSection lifetime"""
2738
class MutableSection(Section):
2739
"""A section allowing changes and keeping track of the original values."""
2741
def __init__(self, section_id, options):
2742
super(MutableSection, self).__init__(section_id, options)
2743
self.reset_changes()
2745
def set(self, name, value):
2746
if name not in self.options:
2747
# This is a new option
2748
self.orig[name] = _NewlyCreatedOption
2749
elif name not in self.orig:
2750
self.orig[name] = self.get(name, None)
2751
self.options[name] = value
2753
def remove(self, name):
2754
if name not in self.orig and name in self.options:
2755
self.orig[name] = self.get(name, None)
2756
del self.options[name]
2758
def reset_changes(self):
2761
def apply_changes(self, dirty, store):
2762
"""Apply option value changes.
2764
``self`` has been reloaded from the persistent storage. ``dirty``
2765
contains the changes made since the previous loading.
2767
:param dirty: the mutable section containing the changes.
2769
:param store: the store containing the section
2771
for k, expected in dirty.orig.items():
2772
actual = dirty.get(k, _DeletedOption)
2773
reloaded = self.get(k, _NewlyCreatedOption)
2774
if actual is _DeletedOption:
2775
if k in self.options:
2779
# Report concurrent updates in an ad-hoc way. This should only
2780
# occurs when different processes try to update the same option
2781
# which is not supported (as in: the config framework is not meant
2782
# to be used as a sharing mechanism).
2783
if expected != reloaded:
2784
if actual is _DeletedOption:
2785
actual = '<DELETED>'
2786
if reloaded is _NewlyCreatedOption:
2787
reloaded = '<CREATED>'
2788
if expected is _NewlyCreatedOption:
2789
expected = '<CREATED>'
2790
# Someone changed the value since we get it from the persistent
2792
trace.warning(gettext(
2793
"Option {0} in section {1} of {2} was changed"
2794
" from {3} to {4}. The {5} value will be saved.".format(
2795
k, self.id, store.external_url(), expected,
2797
# No need to keep track of these changes
2798
self.reset_changes()
2801
class Store(object):
2802
"""Abstract interface to persistent storage for configuration options."""
2804
readonly_section_class = Section
2805
mutable_section_class = MutableSection
2808
# Which sections need to be saved (by section id). We use a dict here
2809
# so the dirty sections can be shared by multiple callers.
2810
self.dirty_sections = {}
2812
def is_loaded(self):
2813
"""Returns True if the Store has been loaded.
2815
This is used to implement lazy loading and ensure the persistent
2816
storage is queried only when needed.
2818
raise NotImplementedError(self.is_loaded)
2821
"""Loads the Store from persistent storage."""
2822
raise NotImplementedError(self.load)
2824
def _load_from_string(self, bytes):
2825
"""Create a store from a string in configobj syntax.
2827
:param bytes: A string representing the file content.
2829
raise NotImplementedError(self._load_from_string)
2832
"""Unloads the Store.
2834
This should make is_loaded() return False. This is used when the caller
2835
knows that the persistent storage has changed or may have change since
2838
raise NotImplementedError(self.unload)
2840
def quote(self, value):
2841
"""Quote a configuration option value for storing purposes.
2843
This allows Stacks to present values as they will be stored.
2847
def unquote(self, value):
2848
"""Unquote a configuration option value into unicode.
2850
The received value is quoted as stored.
2855
"""Saves the Store to persistent storage."""
2856
raise NotImplementedError(self.save)
2858
def _need_saving(self):
2859
for s in self.dirty_sections.values():
2861
# At least one dirty section contains a modification
2865
def apply_changes(self, dirty_sections):
2866
"""Apply changes from dirty sections while checking for coherency.
2868
The Store content is discarded and reloaded from persistent storage to
2869
acquire up-to-date values.
2871
Dirty sections are MutableSection which kept track of the value they
2872
are expected to update.
2874
# We need an up-to-date version from the persistent storage, unload the
2875
# store. The reload will occur when needed (triggered by the first
2876
# get_mutable_section() call below.
2878
# Apply the changes from the preserved dirty sections
2879
for section_id, dirty in dirty_sections.items():
2880
clean = self.get_mutable_section(section_id)
2881
clean.apply_changes(dirty, self)
2882
# Everything is clean now
2883
self.dirty_sections = {}
2885
def save_changes(self):
2886
"""Saves the Store to persistent storage if changes occurred.
2888
Apply the changes recorded in the mutable sections to a store content
2889
refreshed from persistent storage.
2891
raise NotImplementedError(self.save_changes)
2893
def external_url(self):
2894
raise NotImplementedError(self.external_url)
2896
def get_sections(self):
2897
"""Returns an ordered iterable of existing sections.
2899
:returns: An iterable of (store, section).
2901
raise NotImplementedError(self.get_sections)
2903
def get_mutable_section(self, section_id=None):
2904
"""Returns the specified mutable section.
2906
:param section_id: The section identifier
2908
raise NotImplementedError(self.get_mutable_section)
2911
# Mostly for debugging use
2912
return "<config.%s(%s)>" % (self.__class__.__name__,
2913
self.external_url())
2916
class CommandLineStore(Store):
2917
"A store to carry command line overrides for the config options."""
2919
def __init__(self, opts=None):
2920
super(CommandLineStore, self).__init__()
2927
# The dict should be cleared but not replaced so it can be shared.
2928
self.options.clear()
2930
def _from_cmdline(self, overrides):
2931
# Reset before accepting new definitions
2933
for over in overrides:
2935
name, value = over.split('=', 1)
2937
raise errors.BzrCommandError(
2938
gettext("Invalid '%s', should be of the form 'name=value'")
2940
self.options[name] = value
2942
def external_url(self):
2943
# Not an url but it makes debugging easier and is never needed
2947
def get_sections(self):
2948
yield self, self.readonly_section_class(None, self.options)
2951
class IniFileStore(Store):
2952
"""A config Store using ConfigObj for storage.
2954
:ivar _config_obj: Private member to hold the ConfigObj instance used to
2955
serialize/deserialize the config file.
2959
"""A config Store using ConfigObj for storage.
2961
super(IniFileStore, self).__init__()
2962
self._config_obj = None
2964
def is_loaded(self):
2965
return self._config_obj is not None
2968
self._config_obj = None
2969
self.dirty_sections = {}
2971
def _load_content(self):
2972
"""Load the config file bytes.
2974
This should be provided by subclasses
2976
:return: Byte string
2978
raise NotImplementedError(self._load_content)
2980
def _save_content(self, content):
2981
"""Save the config file bytes.
2983
This should be provided by subclasses
2985
:param content: Config file bytes to write
2987
raise NotImplementedError(self._save_content)
2990
"""Load the store from the associated file."""
2991
if self.is_loaded():
2993
content = self._load_content()
2994
self._load_from_string(content)
2995
for hook in ConfigHooks['load']:
2998
def _load_from_string(self, bytes):
2999
"""Create a config store from a string.
3001
:param bytes: A string representing the file content.
3003
if self.is_loaded():
3004
raise AssertionError('Already loaded: %r' % (self._config_obj,))
3005
co_input = BytesIO(bytes)
3007
# The config files are always stored utf8-encoded
3008
self._config_obj = ConfigObj(co_input, encoding='utf-8',
3010
except configobj.ConfigObjError as e:
3011
self._config_obj = None
3012
raise ParseConfigError(e.errors, self.external_url())
3013
except UnicodeDecodeError:
3014
raise ConfigContentError(self.external_url())
3016
def save_changes(self):
3017
if not self.is_loaded():
3020
if not self._need_saving():
3022
# Preserve the current version
3023
dirty_sections = self.dirty_sections.copy()
3024
self.apply_changes(dirty_sections)
3025
# Save to the persistent storage
3029
if not self.is_loaded():
3033
self._config_obj.write(out)
3034
self._save_content(out.getvalue())
3035
for hook in ConfigHooks['save']:
3038
def get_sections(self):
3039
"""Get the configobj section in the file order.
3041
:returns: An iterable of (store, section).
3043
# We need a loaded store
3046
except (errors.NoSuchFile, errors.PermissionDenied):
3047
# If the file can't be read, there is no sections
3049
cobj = self._config_obj
3051
yield self, self.readonly_section_class(None, cobj)
3052
for section_name in cobj.sections:
3054
self.readonly_section_class(section_name,
3055
cobj[section_name]))
3057
def get_mutable_section(self, section_id=None):
3058
# We need a loaded store
3061
except errors.NoSuchFile:
3062
# The file doesn't exist, let's pretend it was empty
3063
self._load_from_string(b'')
3064
if section_id in self.dirty_sections:
3065
# We already created a mutable section for this id
3066
return self.dirty_sections[section_id]
3067
if section_id is None:
3068
section = self._config_obj
3070
section = self._config_obj.setdefault(section_id, {})
3071
mutable_section = self.mutable_section_class(section_id, section)
3072
# All mutable sections can become dirty
3073
self.dirty_sections[section_id] = mutable_section
3074
return mutable_section
3076
def quote(self, value):
3078
# configobj conflates automagical list values and quoting
3079
self._config_obj.list_values = True
3080
return self._config_obj._quote(value)
3082
self._config_obj.list_values = False
3084
def unquote(self, value):
3085
if value and isinstance(value, string_types):
3086
# _unquote doesn't handle None nor empty strings nor anything that
3087
# is not a string, really.
3088
value = self._config_obj._unquote(value)
3091
def external_url(self):
3092
# Since an IniFileStore can be used without a file (at least in tests),
3093
# it's better to provide something than raising a NotImplementedError.
3094
# All daughter classes are supposed to provide an implementation
3096
return 'In-Process Store, no URL'
3099
class TransportIniFileStore(IniFileStore):
3100
"""IniFileStore that loads files from a transport.
3102
:ivar transport: The transport object where the config file is located.
3104
:ivar file_name: The config file basename in the transport directory.
3107
def __init__(self, transport, file_name):
3108
"""A Store using a ini file on a Transport
3110
:param transport: The transport object where the config file is located.
3111
:param file_name: The config file basename in the transport directory.
3113
super(TransportIniFileStore, self).__init__()
3114
self.transport = transport
3115
self.file_name = file_name
3117
def _load_content(self):
3119
return self.transport.get_bytes(self.file_name)
3120
except errors.PermissionDenied:
3121
trace.warning("Permission denied while trying to load "
3122
"configuration store %s.", self.external_url())
3125
def _save_content(self, content):
3126
self.transport.put_bytes(self.file_name, content)
3128
def external_url(self):
3129
# FIXME: external_url should really accepts an optional relpath
3130
# parameter (bug #750169) :-/ -- vila 2011-04-04
3131
# The following will do in the interim but maybe we don't want to
3132
# expose a path here but rather a config ID and its associated
3133
# object </hand wawe>.
3134
return urlutils.join(
3135
self.transport.external_url(), urlutils.escape(self.file_name))
3138
# Note that LockableConfigObjStore inherits from ConfigObjStore because we need
3139
# unlockable stores for use with objects that can already ensure the locking
3140
# (think branches). If different stores (not based on ConfigObj) are created,
3141
# they may face the same issue.
3144
class LockableIniFileStore(TransportIniFileStore):
3145
"""A ConfigObjStore using locks on save to ensure store integrity."""
3147
def __init__(self, transport, file_name, lock_dir_name=None):
3148
"""A config Store using ConfigObj for storage.
3150
:param transport: The transport object where the config file is located.
3152
:param file_name: The config file basename in the transport directory.
3154
if lock_dir_name is None:
3155
lock_dir_name = 'lock'
3156
self.lock_dir_name = lock_dir_name
3157
super(LockableIniFileStore, self).__init__(transport, file_name)
3158
self._lock = lockdir.LockDir(self.transport, self.lock_dir_name)
3160
def lock_write(self, token=None):
3161
"""Takes a write lock in the directory containing the config file.
3163
If the directory doesn't exist it is created.
3165
# FIXME: This doesn't check the ownership of the created directories as
3166
# ensure_config_dir_exists does. It should if the transport is local
3167
# -- vila 2011-04-06
3168
self.transport.create_prefix()
3169
token = self._lock.lock_write(token)
3170
return lock.LogicalLockResult(self.unlock, token)
3175
def break_lock(self):
3176
self._lock.break_lock()
3179
with self.lock_write():
3180
# We need to be able to override the undecorated implementation
3181
self.save_without_locking()
3183
def save_without_locking(self):
3184
super(LockableIniFileStore, self).save()
3187
# FIXME: global, breezy, shouldn't that be 'user' instead or even
3188
# 'user_defaults' as opposed to 'user_overrides', 'system_defaults'
3189
# (/etc/bzr/bazaar.conf) and 'system_overrides' ? -- vila 2011-04-05
3191
# FIXME: Moreover, we shouldn't need classes for these stores either, factory
3192
# functions or a registry will make it easier and clearer for tests, focusing
3193
# on the relevant parts of the API that needs testing -- vila 20110503 (based
3194
# on a poolie's remark)
3195
class GlobalStore(LockableIniFileStore):
3196
"""A config store for global options.
3198
There is a single GlobalStore for a given process.
3201
def __init__(self, possible_transports=None):
3202
path, kind = bedding._config_dir()
3203
t = transport.get_transport_from_path(
3204
path, possible_transports=possible_transports)
3205
super(GlobalStore, self).__init__(t, kind + '.conf')
3209
class LocationStore(LockableIniFileStore):
3210
"""A config store for options specific to a location.
3212
There is a single LocationStore for a given process.
3215
def __init__(self, possible_transports=None):
3216
t = transport.get_transport_from_path(
3217
bedding.config_dir(), possible_transports=possible_transports)
3218
super(LocationStore, self).__init__(t, 'locations.conf')
3219
self.id = 'locations'
3222
class BranchStore(TransportIniFileStore):
3223
"""A config store for branch options.
3225
There is a single BranchStore for a given branch.
3228
def __init__(self, branch):
3229
super(BranchStore, self).__init__(branch.control_transport,
3231
self.branch = branch
3235
class ControlStore(LockableIniFileStore):
3237
def __init__(self, bzrdir):
3238
super(ControlStore, self).__init__(bzrdir.transport,
3240
lock_dir_name='branch_lock')
3244
class SectionMatcher(object):
3245
"""Select sections into a given Store.
3247
This is intended to be used to postpone getting an iterable of sections
3251
def __init__(self, store):
3254
def get_sections(self):
3255
# This is where we require loading the store so we can see all defined
3257
sections = self.store.get_sections()
3258
# Walk the revisions in the order provided
3259
for store, s in sections:
3263
def match(self, section):
3264
"""Does the proposed section match.
3266
:param section: A Section object.
3268
:returns: True if the section matches, False otherwise.
3270
raise NotImplementedError(self.match)
3273
class NameMatcher(SectionMatcher):
3275
def __init__(self, store, section_id):
3276
super(NameMatcher, self).__init__(store)
3277
self.section_id = section_id
3279
def match(self, section):
3280
return section.id == self.section_id
3283
class LocationSection(Section):
3285
def __init__(self, section, extra_path, branch_name=None):
3286
super(LocationSection, self).__init__(section.id, section.options)
3287
self.extra_path = extra_path
3288
if branch_name is None:
3290
self.locals = {'relpath': extra_path,
3291
'basename': urlutils.basename(extra_path),
3292
'branchname': branch_name}
3294
def get(self, name, default=None, expand=True):
3295
value = super(LocationSection, self).get(name, default)
3296
if value is not None and expand:
3297
policy_name = self.get(name + ':policy', None)
3298
policy = _policy_value.get(policy_name, POLICY_NONE)
3299
if policy == POLICY_APPENDPATH:
3300
value = urlutils.join(value, self.extra_path)
3301
# expand section local options right now (since POLICY_APPENDPATH
3302
# will never add options references, it's ok to expand after it).
3304
for is_ref, chunk in iter_option_refs(value):
3306
chunks.append(chunk)
3309
if ref in self.locals:
3310
chunks.append(self.locals[ref])
3312
chunks.append(chunk)
3313
value = ''.join(chunks)
3317
class StartingPathMatcher(SectionMatcher):
3318
"""Select sections for a given location respecting the Store order."""
3320
# FIXME: Both local paths and urls can be used for section names as well as
3321
# ``location`` to stay consistent with ``LocationMatcher`` which itself
3322
# inherited the fuzziness from the previous ``LocationConfig``
3323
# implementation. We probably need to revisit which encoding is allowed for
3324
# both ``location`` and section names and how we normalize
3325
# them. http://pad.lv/85479, http://pad.lv/437009 and http://359320 are
3326
# related too. -- vila 2012-01-04
3328
def __init__(self, store, location):
3329
super(StartingPathMatcher, self).__init__(store)
3330
if location.startswith('file://'):
3331
location = urlutils.local_path_from_url(location)
3332
self.location = location
3334
def get_sections(self):
3335
"""Get all sections matching ``location`` in the store.
3337
The most generic sections are described first in the store, then more
3338
specific ones can be provided for reduced scopes.
3340
The returned section are therefore returned in the reversed order so
3341
the most specific ones can be found first.
3343
location_parts = self.location.rstrip('/').split('/')
3345
# Later sections are more specific, they should be returned first
3346
for _, section in reversed(list(store.get_sections())):
3347
if section.id is None:
3348
# The no-name section is always included if present
3349
yield store, LocationSection(section, self.location)
3351
section_path = section.id
3352
if section_path.startswith('file://'):
3353
# the location is already a local path or URL, convert the
3354
# section id to the same format
3355
section_path = urlutils.local_path_from_url(section_path)
3356
if (self.location.startswith(section_path) or
3357
fnmatch.fnmatch(self.location, section_path)):
3358
section_parts = section_path.rstrip('/').split('/')
3359
extra_path = '/'.join(location_parts[len(section_parts):])
3360
yield store, LocationSection(section, extra_path)
3363
class LocationMatcher(SectionMatcher):
3365
def __init__(self, store, location):
3366
super(LocationMatcher, self).__init__(store)
3367
url, params = urlutils.split_segment_parameters(location)
3368
if location.startswith('file://'):
3369
location = urlutils.local_path_from_url(location)
3370
self.location = location
3371
branch_name = params.get('branch')
3372
if branch_name is None:
3373
self.branch_name = urlutils.basename(self.location)
3375
self.branch_name = urlutils.unescape(branch_name)
3377
def _get_matching_sections(self):
3378
"""Get all sections matching ``location``."""
3379
# We slightly diverge from LocalConfig here by allowing the no-name
3380
# section as the most generic one and the lower priority.
3381
no_name_section = None
3383
# Filter out the no_name_section so _iter_for_location_by_parts can be
3384
# used (it assumes all sections have a name).
3385
for _, section in self.store.get_sections():
3386
if section.id is None:
3387
no_name_section = section
3389
all_sections.append(section)
3390
# Unfortunately _iter_for_location_by_parts deals with section names so
3391
# we have to resync.
3392
filtered_sections = _iter_for_location_by_parts(
3393
[s.id for s in all_sections], self.location)
3394
iter_all_sections = iter(all_sections)
3395
matching_sections = []
3396
if no_name_section is not None:
3397
matching_sections.append(
3398
(0, LocationSection(no_name_section, self.location)))
3399
for section_id, extra_path, length in filtered_sections:
3400
# a section id is unique for a given store so it's safe to take the
3401
# first matching section while iterating. Also, all filtered
3402
# sections are part of 'all_sections' and will always be found
3405
section = next(iter_all_sections)
3406
if section_id == section.id:
3407
section = LocationSection(section, extra_path,
3409
matching_sections.append((length, section))
3411
return matching_sections
3413
def get_sections(self):
3414
# Override the default implementation as we want to change the order
3415
# We want the longest (aka more specific) locations first
3416
sections = sorted(self._get_matching_sections(),
3417
key=lambda match: (match[0], match[1].id),
3419
# Sections mentioning 'ignore_parents' restrict the selection
3420
for _, section in sections:
3421
# FIXME: We really want to use as_bool below -- vila 2011-04-07
3422
ignore = section.get('ignore_parents', None)
3423
if ignore is not None:
3424
ignore = ui.bool_from_string(ignore)
3427
# Finally, we have a valid section
3428
yield self.store, section
3431
# FIXME: _shared_stores should be an attribute of a library state once a
3432
# library_state object is always available.
3434
_shared_stores_at_exit_installed = False
3437
class Stack(object):
3438
"""A stack of configurations where an option can be defined"""
3440
def __init__(self, sections_def, store=None, mutable_section_id=None):
3441
"""Creates a stack of sections with an optional store for changes.
3443
:param sections_def: A list of Section or callables that returns an
3444
iterable of Section. This defines the Sections for the Stack and
3445
can be called repeatedly if needed.
3447
:param store: The optional Store where modifications will be
3448
recorded. If none is specified, no modifications can be done.
3450
:param mutable_section_id: The id of the MutableSection where changes
3451
are recorded. This requires the ``store`` parameter to be
3454
self.sections_def = sections_def
3456
self.mutable_section_id = mutable_section_id
3458
def iter_sections(self):
3459
"""Iterate all the defined sections."""
3460
# Ensuring lazy loading is achieved by delaying section matching (which
3461
# implies querying the persistent storage) until it can't be avoided
3462
# anymore by using callables to describe (possibly empty) section
3464
for sections in self.sections_def:
3465
for store, section in sections():
3466
yield store, section
3468
def get(self, name, expand=True, convert=True):
3469
"""Return the *first* option value found in the sections.
3471
This is where we guarantee that sections coming from Store are loaded
3472
lazily: the loading is delayed until we need to either check that an
3473
option exists or get its value, which in turn may require to discover
3474
in which sections it can be defined. Both of these (section and option
3475
existence) require loading the store (even partially).
3477
:param name: The queried option.
3479
:param expand: Whether options references should be expanded.
3481
:param convert: Whether the option value should be converted from
3482
unicode (do nothing for non-registered options).
3484
:returns: The value of the option.
3486
# FIXME: No caching of options nor sections yet -- vila 20110503
3488
found_store = None # Where the option value has been found
3489
# If the option is registered, it may provide additional info about
3492
opt = option_registry.get(name)
3497
def expand_and_convert(val):
3498
# This may need to be called in different contexts if the value is
3499
# None or ends up being None during expansion or conversion.
3502
if isinstance(val, string_types):
3503
val = self._expand_options_in_string(val)
3505
trace.warning('Cannot expand "%s":'
3506
' %s does not support option expansion'
3507
% (name, type(val)))
3509
val = found_store.unquote(val)
3511
val = opt.convert_from_unicode(found_store, val)
3514
# First of all, check if the environment can override the configuration
3516
if opt is not None and opt.override_from_env:
3517
value = opt.get_override()
3518
value = expand_and_convert(value)
3520
for store, section in self.iter_sections():
3521
value = section.get(name)
3522
if value is not None:
3525
value = expand_and_convert(value)
3526
if opt is not None and value is None:
3527
# If the option is registered, it may provide a default value
3528
value = opt.get_default()
3529
value = expand_and_convert(value)
3530
for hook in ConfigHooks['get']:
3531
hook(self, name, value)
3534
def expand_options(self, string, env=None):
3535
"""Expand option references in the string in the configuration context.
3537
:param string: The string containing option(s) to expand.
3539
:param env: An option dict defining additional configuration options or
3540
overriding existing ones.
3542
:returns: The expanded string.
3544
return self._expand_options_in_string(string, env)
3546
def _expand_options_in_string(self, string, env=None, _refs=None):
3547
"""Expand options in the string in the configuration context.
3549
:param string: The string to be expanded.
3551
:param env: An option dict defining additional configuration options or
3552
overriding existing ones.
3554
:param _refs: Private list (FIFO) containing the options being expanded
3557
:returns: The expanded string.
3560
# Not much to expand there
3563
# What references are currently resolved (to detect loops)
3566
# We need to iterate until no more refs appear ({{foo}} will need two
3567
# iterations for example).
3572
for is_ref, chunk in iter_option_refs(result):
3574
chunks.append(chunk)
3579
raise OptionExpansionLoop(string, _refs)
3581
value = self._expand_option(name, env, _refs)
3583
raise ExpandingUnknownOption(name, string)
3584
chunks.append(value)
3586
result = ''.join(chunks)
3589
def _expand_option(self, name, env, _refs):
3590
if env is not None and name in env:
3591
# Special case, values provided in env takes precedence over
3595
value = self.get(name, expand=False, convert=False)
3596
value = self._expand_options_in_string(value, env, _refs)
3599
def _get_mutable_section(self):
3600
"""Get the MutableSection for the Stack.
3602
This is where we guarantee that the mutable section is lazily loaded:
3603
this means we won't load the corresponding store before setting a value
3604
or deleting an option. In practice the store will often be loaded but
3605
this helps catching some programming errors.
3608
section = store.get_mutable_section(self.mutable_section_id)
3609
return store, section
3611
def set(self, name, value):
3612
"""Set a new value for the option."""
3613
store, section = self._get_mutable_section()
3614
section.set(name, store.quote(value))
3615
for hook in ConfigHooks['set']:
3616
hook(self, name, value)
3618
def remove(self, name):
3619
"""Remove an existing option."""
3620
_, section = self._get_mutable_section()
3621
section.remove(name)
3622
for hook in ConfigHooks['remove']:
3626
# Mostly for debugging use
3627
return "<config.%s(%s)>" % (self.__class__.__name__, id(self))
3629
def _get_overrides(self):
3630
if breezy._global_state is not None:
3631
# TODO(jelmer): Urgh, this is circular so we can't call breezy.get_global_state()
3632
return breezy._global_state.cmdline_overrides.get_sections()
3635
def get_shared_store(self, store, state=None):
3636
"""Get a known shared store.
3638
Store urls uniquely identify them and are used to ensure a single copy
3639
is shared across all users.
3641
:param store: The store known to the caller.
3643
:param state: The library state where the known stores are kept.
3645
:returns: The store received if it's not a known one, an already known
3649
# TODO(jelmer): Urgh, this is circular so we can't call breezy.get_global_state()
3650
state = breezy._global_state
3652
global _shared_stores_at_exit_installed
3653
stores = _shared_stores
3655
def save_config_changes():
3656
for k, store in stores.items():
3657
store.save_changes()
3658
if not _shared_stores_at_exit_installed:
3659
# FIXME: Ugly hack waiting for library_state to always be
3660
# available. -- vila 20120731
3662
atexit.register(save_config_changes)
3663
_shared_stores_at_exit_installed = True
3665
stores = state.config_stores
3666
url = store.external_url()
3674
class MemoryStack(Stack):
3675
"""A configuration stack defined from a string.
3677
This is mainly intended for tests and requires no disk resources.
3680
def __init__(self, content=None):
3681
"""Create an in-memory stack from a given content.
3683
It uses a single store based on configobj and support reading and
3686
:param content: The initial content of the store. If None, the store is
3687
not loaded and ``_load_from_string`` can and should be used if
3690
store = IniFileStore()
3691
if content is not None:
3692
store._load_from_string(content)
3693
super(MemoryStack, self).__init__(
3694
[store.get_sections], store)
3697
class _CompatibleStack(Stack):
3698
"""Place holder for compatibility with previous design.
3700
This is intended to ease the transition from the Config-based design to the
3701
Stack-based design and should not be used nor relied upon by plugins.
3703
One assumption made here is that the daughter classes will all use Stores
3704
derived from LockableIniFileStore).
3706
It implements set() and remove () by re-loading the store before applying
3707
the modification and saving it.
3709
The long term plan being to implement a single write by store to save
3710
all modifications, this class should not be used in the interim.
3713
def set(self, name, value):
3716
super(_CompatibleStack, self).set(name, value)
3717
# Force a write to persistent storage
3720
def remove(self, name):
3723
super(_CompatibleStack, self).remove(name)
3724
# Force a write to persistent storage
3728
class GlobalStack(Stack):
3729
"""Global options only stack.
3731
The following sections are queried:
3733
* command-line overrides,
3735
* the 'DEFAULT' section in bazaar.conf
3737
This stack will use the ``DEFAULT`` section in bazaar.conf as its
3742
gstore = self.get_shared_store(GlobalStore())
3743
super(GlobalStack, self).__init__(
3744
[self._get_overrides,
3745
NameMatcher(gstore, 'DEFAULT').get_sections],
3746
gstore, mutable_section_id='DEFAULT')
3749
class LocationStack(Stack):
3750
"""Per-location options falling back to global options stack.
3753
The following sections are queried:
3755
* command-line overrides,
3757
* the sections matching ``location`` in ``locations.conf``, the order being
3758
defined by the number of path components in the section glob, higher
3759
numbers first (from most specific section to most generic).
3761
* the 'DEFAULT' section in bazaar.conf
3763
This stack will use the ``location`` section in locations.conf as its
3767
def __init__(self, location):
3768
"""Make a new stack for a location and global configuration.
3770
:param location: A URL prefix to """
3771
lstore = self.get_shared_store(LocationStore())
3772
if location.startswith('file://'):
3773
location = urlutils.local_path_from_url(location)
3774
gstore = self.get_shared_store(GlobalStore())
3775
super(LocationStack, self).__init__(
3776
[self._get_overrides,
3777
LocationMatcher(lstore, location).get_sections,
3778
NameMatcher(gstore, 'DEFAULT').get_sections],
3779
lstore, mutable_section_id=location)
3782
class BranchStack(Stack):
3783
"""Per-location options falling back to branch then global options stack.
3785
The following sections are queried:
3787
* command-line overrides,
3789
* the sections matching ``location`` in ``locations.conf``, the order being
3790
defined by the number of path components in the section glob, higher
3791
numbers first (from most specific section to most generic),
3793
* the no-name section in branch.conf,
3795
* the ``DEFAULT`` section in ``bazaar.conf``.
3797
This stack will use the no-name section in ``branch.conf`` as its
3801
def __init__(self, branch):
3802
lstore = self.get_shared_store(LocationStore())
3803
bstore = branch._get_config_store()
3804
gstore = self.get_shared_store(GlobalStore())
3805
super(BranchStack, self).__init__(
3806
[self._get_overrides,
3807
LocationMatcher(lstore, branch.base).get_sections,
3808
NameMatcher(bstore, None).get_sections,
3809
NameMatcher(gstore, 'DEFAULT').get_sections],
3811
self.branch = branch
3813
def lock_write(self, token=None):
3814
return self.branch.lock_write(token)
3817
return self.branch.unlock()
3819
def set(self, name, value):
3820
with self.lock_write():
3821
super(BranchStack, self).set(name, value)
3822
# Unlocking the branch will trigger a store.save_changes() so the
3823
# last unlock saves all the changes.
3825
def remove(self, name):
3826
with self.lock_write():
3827
super(BranchStack, self).remove(name)
3828
# Unlocking the branch will trigger a store.save_changes() so the
3829
# last unlock saves all the changes.
3832
class RemoteControlStack(Stack):
3833
"""Remote control-only options stack."""
3835
# FIXME 2011-11-22 JRV This should probably be renamed to avoid confusion
3836
# with the stack used for remote bzr dirs. RemoteControlStack only uses
3837
# control.conf and is used only for stack options.
3839
def __init__(self, bzrdir):
3840
cstore = bzrdir._get_config_store()
3841
super(RemoteControlStack, self).__init__(
3842
[NameMatcher(cstore, None).get_sections],
3844
self.controldir = bzrdir
3847
class BranchOnlyStack(Stack):
3848
"""Branch-only options stack."""
3850
# FIXME: _BranchOnlyStack only uses branch.conf and is used only for the
3851
# stacked_on_location options waiting for http://pad.lv/832042 to be fixed.
3852
# -- vila 2011-12-16
3854
def __init__(self, branch):
3855
bstore = branch._get_config_store()
3856
super(BranchOnlyStack, self).__init__(
3857
[NameMatcher(bstore, None).get_sections],
3859
self.branch = branch
3861
def lock_write(self, token=None):
3862
return self.branch.lock_write(token)
3865
return self.branch.unlock()
3867
def set(self, name, value):
3868
with self.lock_write():
3869
super(BranchOnlyStack, self).set(name, value)
3870
# Force a write to persistent storage
3871
self.store.save_changes()
3873
def remove(self, name):
3874
with self.lock_write():
3875
super(BranchOnlyStack, self).remove(name)
3876
# Force a write to persistent storage
3877
self.store.save_changes()
3880
class cmd_config(commands.Command):
3881
__doc__ = """Display, set or remove a configuration option.
3883
Display the active value for option NAME.
3885
If --all is specified, NAME is interpreted as a regular expression and all
3886
matching options are displayed mentioning their scope and without resolving
3887
option references in the value). The active value that bzr will take into
3888
account is the first one displayed for each option.
3890
If NAME is not given, --all .* is implied (all options are displayed for the
3893
Setting a value is achieved by using NAME=value without spaces. The value
3894
is set in the most relevant scope and can be checked by displaying the
3897
Removing a value is achieved by using --remove NAME.
3900
takes_args = ['name?']
3904
# FIXME: This should be a registry option so that plugins can register
3905
# their own config files (or not) and will also address
3906
# http://pad.lv/788991 -- vila 20101115
3907
commands.Option('scope', help='Reduce the scope to the specified'
3908
' configuration file.',
3910
commands.Option('all',
3911
help='Display all the defined values for the matching options.',
3913
commands.Option('remove', help='Remove the option from'
3914
' the configuration file.'),
3917
_see_also = ['configuration']
3919
@commands.display_command
3920
def run(self, name=None, all=False, directory=None, scope=None,
3922
if directory is None:
3924
directory = directory_service.directories.dereference(directory)
3925
directory = urlutils.normalize_url(directory)
3927
raise errors.BzrError(
3928
'--all and --remove are mutually exclusive.')
3930
# Delete the option in the given scope
3931
self._remove_config_option(name, directory, scope)
3933
# Defaults to all options
3934
self._show_matching_options('.*', directory, scope)
3937
name, value = name.split('=', 1)
3939
# Display the option(s) value(s)
3941
self._show_matching_options(name, directory, scope)
3943
self._show_value(name, directory, scope)
3946
raise errors.BzrError(
3947
'Only one option can be set.')
3948
# Set the option value
3949
self._set_config_option(name, value, directory, scope)
3951
def _get_stack(self, directory, scope=None, write_access=False):
3952
"""Get the configuration stack specified by ``directory`` and ``scope``.
3954
:param directory: Where the configurations are derived from.
3956
:param scope: A specific config to start from.
3958
:param write_access: Whether a write access to the stack will be
3961
# FIXME: scope should allow access to plugin-specific stacks (even
3962
# reduced to the plugin-specific store), related to
3963
# http://pad.lv/788991 -- vila 2011-11-15
3964
if scope is not None:
3965
if scope == 'breezy':
3966
return GlobalStack()
3967
elif scope == 'locations':
3968
return LocationStack(directory)
3969
elif scope == 'branch':
3971
controldir.ControlDir.open_containing_tree_or_branch(
3974
self.add_cleanup(br.lock_write().unlock)
3975
return br.get_config_stack()
3976
raise NoSuchConfig(scope)
3980
controldir.ControlDir.open_containing_tree_or_branch(
3983
self.add_cleanup(br.lock_write().unlock)
3984
return br.get_config_stack()
3985
except errors.NotBranchError:
3986
return LocationStack(directory)
3988
def _quote_multiline(self, value):
3990
value = '"""' + value + '"""'
3993
def _show_value(self, name, directory, scope):
3994
conf = self._get_stack(directory, scope)
3995
value = conf.get(name, expand=True, convert=False)
3996
if value is not None:
3997
# Quote the value appropriately
3998
value = self._quote_multiline(value)
3999
self.outf.write('%s\n' % (value,))
4001
raise NoSuchConfigOption(name)
4003
def _show_matching_options(self, name, directory, scope):
4004
name = lazy_regex.lazy_compile(name)
4005
# We want any error in the regexp to be raised *now* so we need to
4006
# avoid the delay introduced by the lazy regexp. But, we still do
4007
# want the nicer errors raised by lazy_regex.
4008
name._compile_and_collapse()
4011
conf = self._get_stack(directory, scope)
4012
for store, section in conf.iter_sections():
4013
for oname in section.iter_option_names():
4014
if name.search(oname):
4015
if cur_store_id != store.id:
4016
# Explain where the options are defined
4017
self.outf.write('%s:\n' % (store.id,))
4018
cur_store_id = store.id
4020
if (section.id is not None and cur_section != section.id):
4021
# Display the section id as it appears in the store
4022
# (None doesn't appear by definition)
4023
self.outf.write(' [%s]\n' % (section.id,))
4024
cur_section = section.id
4025
value = section.get(oname, expand=False)
4026
# Quote the value appropriately
4027
value = self._quote_multiline(value)
4028
self.outf.write(' %s = %s\n' % (oname, value))
4030
def _set_config_option(self, name, value, directory, scope):
4031
conf = self._get_stack(directory, scope, write_access=True)
4032
conf.set(name, value)
4033
# Explicitly save the changes
4034
conf.store.save_changes()
4036
def _remove_config_option(self, name, directory, scope):
4038
raise errors.BzrCommandError(
4039
'--remove expects an option to remove.')
4040
conf = self._get_stack(directory, scope, write_access=True)
4043
# Explicitly save the changes
4044
conf.store.save_changes()
4046
raise NoSuchConfigOption(name)
4051
# We need adapters that can build a Store or a Stack in a test context. Test
4052
# classes, based on TestCaseWithTransport, can use the registry to parametrize
4053
# themselves. The builder will receive a test instance and should return a
4054
# ready-to-use store or stack. Plugins that define new store/stacks can also
4055
# register themselves here to be tested against the tests defined in
4056
# breezy.tests.test_config. Note that the builder can be called multiple times
4057
# for the same test.
4059
# The registered object should be a callable receiving a test instance
4060
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Store
4062
test_store_builder_registry = registry.Registry()
4064
# The registered object should be a callable receiving a test instance
4065
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Stack
4067
test_stack_builder_registry = registry.Registry()
421
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
423
raise BzrError("%r doesn't seem to contain "
424
"a reasonable email address" % e)