~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

(vila) Fix bug #701212,
 don't set the tag dict of the master branch you are updating from. (John A
 Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#            and others
4
4
#
29
29
create_signatures=always|never|when-required(default)
30
30
gpg_signing_command=name-of-program
31
31
log_format=name-of-format
32
 
validate_signatures_in_log=true|false(default)
33
 
acceptable_keys=pattern1,pattern2
34
32
 
35
33
in locations.conf, you specify the url of a branch and options for it.
36
34
Wildcards may be used - * and ? as normal in shell completion. Options
41
39
email= as above
42
40
check_signatures= as above
43
41
create_signatures= as above.
44
 
validate_signatures_in_log=as above
45
 
acceptable_keys=as above
46
42
 
47
43
explanation of options
48
44
----------------------
49
45
editor - this option sets the pop up editor to use during commits.
50
46
email - this option sets the user id bzr will use when committing.
51
 
check_signatures - this option will control whether bzr will require good gpg
 
47
check_signatures - this option controls whether bzr will require good gpg
52
48
                   signatures, ignore them, or check them if they are
53
 
                   present.  Currently it is unused except that check_signatures
54
 
                   turns on create_signatures.
 
49
                   present.
55
50
create_signatures - this option controls whether bzr will always create
56
 
                    gpg signatures or not on commits.  There is an unused
57
 
                    option which in future is expected to work if               
58
 
                    branch settings require signatures.
 
51
                    gpg signatures, never create them, or create them if the
 
52
                    branch is configured to require them.
59
53
log_format - this option sets the default log format.  Possible values are
60
54
             long, short, line, or a plugin can register new formats.
61
 
validate_signatures_in_log - show GPG signature validity in log output
62
 
acceptable_keys - comma separated list of key patterns acceptable for
63
 
                  verify-signatures command
64
55
 
65
56
In bazaar.conf you can also define aliases in the ALIASES sections, example
66
57
 
72
63
"""
73
64
 
74
65
import os
75
 
import string
76
66
import sys
77
67
 
78
 
 
 
68
from bzrlib import commands
79
69
from bzrlib.decorators import needs_write_lock
80
70
from bzrlib.lazy_import import lazy_import
81
71
lazy_import(globals(), """
 
72
import errno
82
73
import fnmatch
83
74
import re
84
75
from cStringIO import StringIO
85
76
 
 
77
import bzrlib
86
78
from bzrlib import (
87
79
    atomicfile,
88
80
    bzrdir,
89
81
    debug,
90
82
    errors,
91
 
    lazy_regex,
92
83
    lockdir,
93
84
    mail_client,
94
 
    mergetools,
95
85
    osutils,
 
86
    registry,
96
87
    symbol_versioning,
97
88
    trace,
98
89
    transport,
102
93
    )
103
94
from bzrlib.util.configobj import configobj
104
95
""")
105
 
from bzrlib import (
106
 
    commands,
107
 
    hooks,
108
 
    registry,
109
 
    )
110
 
from bzrlib.symbol_versioning import (
111
 
    deprecated_in,
112
 
    deprecated_method,
113
 
    )
114
96
 
115
97
 
116
98
CHECK_IF_POSSIBLE=0
146
128
STORE_BRANCH = 3
147
129
STORE_GLOBAL = 4
148
130
 
149
 
 
150
 
class ConfigObj(configobj.ConfigObj):
151
 
 
152
 
    def __init__(self, infile=None, **kwargs):
153
 
        # We define our own interpolation mechanism calling it option expansion
154
 
        super(ConfigObj, self).__init__(infile=infile,
155
 
                                        interpolation=False,
156
 
                                        **kwargs)
157
 
 
158
 
    def get_bool(self, section, key):
159
 
        return self[section].as_bool(key)
160
 
 
161
 
    def get_value(self, section, name):
162
 
        # Try [] for the old DEFAULT section.
163
 
        if section == "DEFAULT":
164
 
            try:
165
 
                return self[name]
166
 
            except KeyError:
167
 
                pass
168
 
        return self[section][name]
169
 
 
170
 
 
171
 
# FIXME: Until we can guarantee that each config file is loaded once and
172
 
# only once for a given bzrlib session, we don't want to re-read the file every
173
 
# time we query for an option so we cache the value (bad ! watch out for tests
174
 
# needing to restore the proper value).This shouldn't be part of 2.4.0 final,
175
 
# yell at mgz^W vila and the RM if this is still present at that time
176
 
# -- vila 20110219
177
 
_expand_default_value = None
178
 
def _get_expand_default_value():
179
 
    global _expand_default_value
180
 
    if _expand_default_value is not None:
181
 
        return _expand_default_value
182
 
    conf = GlobalConfig()
183
 
    # Note that we must not use None for the expand value below or we'll run
184
 
    # into infinite recursion. Using False really would be quite silly ;)
185
 
    expand = conf.get_user_option_as_bool('bzr.config.expand', expand=True)
186
 
    if expand is None:
187
 
        # This is an opt-in feature, you *really* need to clearly say you want
188
 
        # to activate it !
189
 
        expand = False
190
 
    _expand_default_value = expand
191
 
    return expand
 
131
_ConfigObj = None
 
132
def ConfigObj(*args, **kwargs):
 
133
    global _ConfigObj
 
134
    if _ConfigObj is None:
 
135
        class ConfigObj(configobj.ConfigObj):
 
136
 
 
137
            def get_bool(self, section, key):
 
138
                return self[section].as_bool(key)
 
139
 
 
140
            def get_value(self, section, name):
 
141
                # Try [] for the old DEFAULT section.
 
142
                if section == "DEFAULT":
 
143
                    try:
 
144
                        return self[name]
 
145
                    except KeyError:
 
146
                        pass
 
147
                return self[section][name]
 
148
        _ConfigObj = ConfigObj
 
149
    return _ConfigObj(*args, **kwargs)
192
150
 
193
151
 
194
152
class Config(object):
201
159
        """Returns a unique ID for the config."""
202
160
        raise NotImplementedError(self.config_id)
203
161
 
204
 
    @deprecated_method(deprecated_in((2, 4, 0)))
205
162
    def get_editor(self):
206
163
        """Get the users pop up editor."""
207
164
        raise NotImplementedError
214
171
        return diff.DiffFromTool.from_string(cmd, old_tree, new_tree,
215
172
                                             sys.stdout)
216
173
 
 
174
 
217
175
    def get_mail_client(self):
218
176
        """Get a mail client to use"""
219
177
        selected_client = self.get_user_option('mail_client')
230
188
    def _get_signing_policy(self):
231
189
        """Template method to override signature creation policy."""
232
190
 
233
 
    option_ref_re = None
234
 
 
235
 
    def expand_options(self, string, env=None):
236
 
        """Expand option references in the string in the configuration context.
237
 
 
238
 
        :param string: The string containing option to expand.
239
 
 
240
 
        :param env: An option dict defining additional configuration options or
241
 
            overriding existing ones.
242
 
 
243
 
        :returns: The expanded string.
244
 
        """
245
 
        return self._expand_options_in_string(string, env)
246
 
 
247
 
    def _expand_options_in_list(self, slist, env=None, _ref_stack=None):
248
 
        """Expand options in  a list of strings in the configuration context.
249
 
 
250
 
        :param slist: A list of strings.
251
 
 
252
 
        :param env: An option dict defining additional configuration options or
253
 
            overriding existing ones.
254
 
 
255
 
        :param _ref_stack: Private list containing the options being
256
 
            expanded to detect loops.
257
 
 
258
 
        :returns: The flatten list of expanded strings.
259
 
        """
260
 
        # expand options in each value separately flattening lists
261
 
        result = []
262
 
        for s in slist:
263
 
            value = self._expand_options_in_string(s, env, _ref_stack)
264
 
            if isinstance(value, list):
265
 
                result.extend(value)
266
 
            else:
267
 
                result.append(value)
268
 
        return result
269
 
 
270
 
    def _expand_options_in_string(self, string, env=None, _ref_stack=None):
271
 
        """Expand options in the string in the configuration context.
272
 
 
273
 
        :param string: The string to be expanded.
274
 
 
275
 
        :param env: An option dict defining additional configuration options or
276
 
            overriding existing ones.
277
 
 
278
 
        :param _ref_stack: Private list containing the options being
279
 
            expanded to detect loops.
280
 
 
281
 
        :returns: The expanded string.
282
 
        """
283
 
        if string is None:
284
 
            # Not much to expand there
285
 
            return None
286
 
        if _ref_stack is None:
287
 
            # What references are currently resolved (to detect loops)
288
 
            _ref_stack = []
289
 
        if self.option_ref_re is None:
290
 
            # We want to match the most embedded reference first (i.e. for
291
 
            # '{{foo}}' we will get '{foo}',
292
 
            # for '{bar{baz}}' we will get '{baz}'
293
 
            self.option_ref_re = re.compile('({[^{}]+})')
294
 
        result = string
295
 
        # We need to iterate until no more refs appear ({{foo}} will need two
296
 
        # iterations for example).
297
 
        while True:
298
 
            raw_chunks = self.option_ref_re.split(result)
299
 
            if len(raw_chunks) == 1:
300
 
                # Shorcut the trivial case: no refs
301
 
                return result
302
 
            chunks = []
303
 
            list_value = False
304
 
            # Split will isolate refs so that every other chunk is a ref
305
 
            chunk_is_ref = False
306
 
            for chunk in raw_chunks:
307
 
                if not chunk_is_ref:
308
 
                    if chunk:
309
 
                        # Keep only non-empty strings (or we get bogus empty
310
 
                        # slots when a list value is involved).
311
 
                        chunks.append(chunk)
312
 
                    chunk_is_ref = True
313
 
                else:
314
 
                    name = chunk[1:-1]
315
 
                    if name in _ref_stack:
316
 
                        raise errors.OptionExpansionLoop(string, _ref_stack)
317
 
                    _ref_stack.append(name)
318
 
                    value = self._expand_option(name, env, _ref_stack)
319
 
                    if value is None:
320
 
                        raise errors.ExpandingUnknownOption(name, string)
321
 
                    if isinstance(value, list):
322
 
                        list_value = True
323
 
                        chunks.extend(value)
324
 
                    else:
325
 
                        chunks.append(value)
326
 
                    _ref_stack.pop()
327
 
                    chunk_is_ref = False
328
 
            if list_value:
329
 
                # Once a list appears as the result of an expansion, all
330
 
                # callers will get a list result. This allows a consistent
331
 
                # behavior even when some options in the expansion chain
332
 
                # defined as strings (no comma in their value) but their
333
 
                # expanded value is a list.
334
 
                return self._expand_options_in_list(chunks, env, _ref_stack)
335
 
            else:
336
 
                result = ''.join(chunks)
337
 
        return result
338
 
 
339
 
    def _expand_option(self, name, env, _ref_stack):
340
 
        if env is not None and name in env:
341
 
            # Special case, values provided in env takes precedence over
342
 
            # anything else
343
 
            value = env[name]
344
 
        else:
345
 
            # FIXME: This is a limited implementation, what we really need is a
346
 
            # way to query the bzr config for the value of an option,
347
 
            # respecting the scope rules (That is, once we implement fallback
348
 
            # configs, getting the option value should restart from the top
349
 
            # config, not the current one) -- vila 20101222
350
 
            value = self.get_user_option(name, expand=False)
351
 
            if isinstance(value, list):
352
 
                value = self._expand_options_in_list(value, env, _ref_stack)
353
 
            else:
354
 
                value = self._expand_options_in_string(value, env, _ref_stack)
355
 
        return value
356
 
 
357
191
    def _get_user_option(self, option_name):
358
192
        """Template method to provide a user option."""
359
193
        return None
360
194
 
361
 
    def get_user_option(self, option_name, expand=None):
362
 
        """Get a generic option - no special process, no default.
363
 
 
364
 
        :param option_name: The queried option.
365
 
 
366
 
        :param expand: Whether options references should be expanded.
367
 
 
368
 
        :returns: The value of the option.
369
 
        """
370
 
        if expand is None:
371
 
            expand = _get_expand_default_value()
372
 
        value = self._get_user_option(option_name)
373
 
        if expand:
374
 
            if isinstance(value, list):
375
 
                value = self._expand_options_in_list(value)
376
 
            elif isinstance(value, dict):
377
 
                trace.warning('Cannot expand "%s":'
378
 
                              ' Dicts do not support option expansion'
379
 
                              % (option_name,))
380
 
            else:
381
 
                value = self._expand_options_in_string(value)
382
 
        for hook in OldConfigHooks['get']:
383
 
            hook(self, option_name, value)
384
 
        return value
385
 
 
386
 
    def get_user_option_as_bool(self, option_name, expand=None, default=None):
387
 
        """Get a generic option as a boolean.
388
 
 
389
 
        :param expand: Allow expanding references to other config values.
390
 
        :param default: Default value if nothing is configured
 
195
    def get_user_option(self, option_name):
 
196
        """Get a generic option - no special process, no default."""
 
197
        return self._get_user_option(option_name)
 
198
 
 
199
    def get_user_option_as_bool(self, option_name):
 
200
        """Get a generic option as a boolean - no special process, no default.
 
201
 
391
202
        :return None if the option doesn't exist or its value can't be
392
203
            interpreted as a boolean. Returns True or False otherwise.
393
204
        """
394
 
        s = self.get_user_option(option_name, expand=expand)
 
205
        s = self._get_user_option(option_name)
395
206
        if s is None:
396
207
            # The option doesn't exist
397
 
            return default
 
208
            return None
398
209
        val = ui.bool_from_string(s)
399
210
        if val is None:
400
211
            # The value can't be interpreted as a boolean
402
213
                          s, option_name)
403
214
        return val
404
215
 
405
 
    def get_user_option_as_list(self, option_name, expand=None):
 
216
    def get_user_option_as_list(self, option_name):
406
217
        """Get a generic option as a list - no special process, no default.
407
218
 
408
219
        :return None if the option doesn't exist. Returns the value as a list
409
220
            otherwise.
410
221
        """
411
 
        l = self.get_user_option(option_name, expand=expand)
 
222
        l = self._get_user_option(option_name)
412
223
        if isinstance(l, (str, unicode)):
413
 
            # A single value, most probably the user forgot (or didn't care to
414
 
            # add) the final ','
 
224
            # A single value, most probably the user forgot the final ','
415
225
            l = [l]
416
226
        return l
417
227
 
437
247
        """See log_format()."""
438
248
        return None
439
249
 
440
 
    def validate_signatures_in_log(self):
441
 
        """Show GPG signature validity in log"""
442
 
        result = self._validate_signatures_in_log()
443
 
        if result == "true":
444
 
            result = True
445
 
        else:
446
 
            result = False
447
 
        return result
448
 
 
449
 
    def _validate_signatures_in_log(self):
450
 
        """See validate_signatures_in_log()."""
451
 
        return None
452
 
 
453
 
    def acceptable_keys(self):
454
 
        """Comma separated list of key patterns acceptable to 
455
 
        verify-signatures command"""
456
 
        result = self._acceptable_keys()
457
 
        return result
458
 
 
459
 
    def _acceptable_keys(self):
460
 
        """See acceptable_keys()."""
461
 
        return None
462
 
 
463
250
    def post_commit(self):
464
251
        """An ordered list of python functions to call.
465
252
 
484
271
        the concrete policy type is checked, and finally
485
272
        $EMAIL is examined.
486
273
        If no username can be found, errors.NoWhoami exception is raised.
 
274
 
 
275
        TODO: Check it's reasonably well-formed.
487
276
        """
488
277
        v = os.environ.get('BZR_EMAIL')
489
278
        if v:
490
279
            return v.decode(osutils.get_user_encoding())
 
280
 
491
281
        v = self._get_user_id()
492
282
        if v:
493
283
            return v
 
284
 
494
285
        v = os.environ.get('EMAIL')
495
286
        if v:
496
287
            return v.decode(osutils.get_user_encoding())
497
 
        name, email = _auto_user_id()
498
 
        if name and email:
499
 
            return '%s <%s>' % (name, email)
500
 
        elif email:
501
 
            return email
 
288
 
502
289
        raise errors.NoWhoami()
503
290
 
504
291
    def ensure_username(self):
528
315
        if policy is None:
529
316
            policy = self._get_signature_checking()
530
317
            if policy is not None:
531
 
                #this warning should go away once check_signatures is
532
 
                #implemented (if not before)
533
318
                trace.warning("Please use create_signatures,"
534
319
                              " not check_signatures to set signing policy.")
 
320
            if policy == CHECK_ALWAYS:
 
321
                return True
535
322
        elif policy == SIGN_ALWAYS:
536
323
            return True
537
324
        return False
570
357
        else:
571
358
            return True
572
359
 
573
 
    def get_merge_tools(self):
574
 
        tools = {}
575
 
        for (oname, value, section, conf_id, parser) in self._get_options():
576
 
            if oname.startswith('bzr.mergetool.'):
577
 
                tool_name = oname[len('bzr.mergetool.'):]
578
 
                tools[tool_name] = value
579
 
        trace.mutter('loaded merge tools: %r' % tools)
580
 
        return tools
581
 
 
582
 
    def find_merge_tool(self, name):
583
 
        # We fake a defaults mechanism here by checking if the given name can
584
 
        # be found in the known_merge_tools if it's not found in the config.
585
 
        # This should be done through the proposed config defaults mechanism
586
 
        # when it becomes available in the future.
587
 
        command_line = (self.get_user_option('bzr.mergetool.%s' % name,
588
 
                                             expand=False)
589
 
                        or mergetools.known_merge_tools.get(name, None))
590
 
        return command_line
591
 
 
592
 
 
593
 
class _ConfigHooks(hooks.Hooks):
594
 
    """A dict mapping hook names and a list of callables for configs.
595
 
    """
596
 
 
597
 
    def __init__(self):
598
 
        """Create the default hooks.
599
 
 
600
 
        These are all empty initially, because by default nothing should get
601
 
        notified.
602
 
        """
603
 
        super(_ConfigHooks, self).__init__('bzrlib.config', 'ConfigHooks')
604
 
        self.add_hook('load',
605
 
                      'Invoked when a config store is loaded.'
606
 
                      ' The signature is (store).',
607
 
                      (2, 4))
608
 
        self.add_hook('save',
609
 
                      'Invoked when a config store is saved.'
610
 
                      ' The signature is (store).',
611
 
                      (2, 4))
612
 
        # The hooks for config options
613
 
        self.add_hook('get',
614
 
                      'Invoked when a config option is read.'
615
 
                      ' The signature is (stack, name, value).',
616
 
                      (2, 4))
617
 
        self.add_hook('set',
618
 
                      'Invoked when a config option is set.'
619
 
                      ' The signature is (stack, name, value).',
620
 
                      (2, 4))
621
 
        self.add_hook('remove',
622
 
                      'Invoked when a config option is removed.'
623
 
                      ' The signature is (stack, name).',
624
 
                      (2, 4))
625
 
ConfigHooks = _ConfigHooks()
626
 
 
627
 
 
628
 
class _OldConfigHooks(hooks.Hooks):
629
 
    """A dict mapping hook names and a list of callables for configs.
630
 
    """
631
 
 
632
 
    def __init__(self):
633
 
        """Create the default hooks.
634
 
 
635
 
        These are all empty initially, because by default nothing should get
636
 
        notified.
637
 
        """
638
 
        super(_OldConfigHooks, self).__init__('bzrlib.config', 'OldConfigHooks')
639
 
        self.add_hook('load',
640
 
                      'Invoked when a config store is loaded.'
641
 
                      ' The signature is (config).',
642
 
                      (2, 4))
643
 
        self.add_hook('save',
644
 
                      'Invoked when a config store is saved.'
645
 
                      ' The signature is (config).',
646
 
                      (2, 4))
647
 
        # The hooks for config options
648
 
        self.add_hook('get',
649
 
                      'Invoked when a config option is read.'
650
 
                      ' The signature is (config, name, value).',
651
 
                      (2, 4))
652
 
        self.add_hook('set',
653
 
                      'Invoked when a config option is set.'
654
 
                      ' The signature is (config, name, value).',
655
 
                      (2, 4))
656
 
        self.add_hook('remove',
657
 
                      'Invoked when a config option is removed.'
658
 
                      ' The signature is (config, name).',
659
 
                      (2, 4))
660
 
OldConfigHooks = _OldConfigHooks()
661
 
 
662
360
 
663
361
class IniBasedConfig(Config):
664
362
    """A configuration policy that draws from ini files."""
725
423
            self._parser = ConfigObj(co_input, encoding='utf-8')
726
424
        except configobj.ConfigObjError, e:
727
425
            raise errors.ParseConfigError(e.errors, e.config.filename)
728
 
        except UnicodeDecodeError:
729
 
            raise errors.ConfigContentError(self.file_name)
730
426
        # Make sure self.reload() will use the right file name
731
427
        self._parser.filename = self.file_name
732
 
        for hook in OldConfigHooks['load']:
733
 
            hook(self)
734
428
        return self._parser
735
429
 
736
430
    def reload(self):
739
433
            raise AssertionError('We need a file name to reload the config')
740
434
        if self._parser is not None:
741
435
            self._parser.reload()
742
 
        for hook in ConfigHooks['load']:
743
 
            hook(self)
744
436
 
745
437
    def _get_matching_sections(self):
746
438
        """Return an ordered list of (section_name, extra_path) pairs.
863
555
        """See Config.log_format."""
864
556
        return self._get_user_option('log_format')
865
557
 
866
 
    def _validate_signatures_in_log(self):
867
 
        """See Config.validate_signatures_in_log."""
868
 
        return self._get_user_option('validate_signatures_in_log')
869
 
 
870
 
    def _acceptable_keys(self):
871
 
        """See Config.acceptable_keys."""
872
 
        return self._get_user_option('acceptable_keys')
873
 
 
874
558
    def _post_commit(self):
875
559
        """See Config.post_commit."""
876
560
        return self._get_user_option('post_commit')
926
610
        except KeyError:
927
611
            raise errors.NoSuchConfigOption(option_name)
928
612
        self._write_config_file()
929
 
        for hook in OldConfigHooks['remove']:
930
 
            hook(self, option_name)
931
613
 
932
614
    def _write_config_file(self):
933
615
        if self.file_name is None:
939
621
        atomic_file.commit()
940
622
        atomic_file.close()
941
623
        osutils.copy_ownership_from_path(self.file_name)
942
 
        for hook in OldConfigHooks['save']:
943
 
            hook(self)
944
624
 
945
625
 
946
626
class LockableConfig(IniBasedConfig):
973
653
    def __init__(self, file_name):
974
654
        super(LockableConfig, self).__init__(file_name=file_name)
975
655
        self.dir = osutils.dirname(osutils.safe_unicode(self.file_name))
976
 
        # FIXME: It doesn't matter that we don't provide possible_transports
977
 
        # below since this is currently used only for local config files ;
978
 
        # local transports are not shared. But if/when we start using
979
 
        # LockableConfig for other kind of transports, we will need to reuse
980
 
        # whatever connection is already established -- vila 20100929
981
656
        self.transport = transport.get_transport(self.dir)
982
 
        self._lock = lockdir.LockDir(self.transport, self.lock_name)
 
657
        self._lock = lockdir.LockDir(self.transport, 'lock')
983
658
 
984
659
    def _create_from_string(self, unicode_bytes, save):
985
660
        super(LockableConfig, self)._create_from_string(unicode_bytes, False)
1039
714
        conf._create_from_string(str_or_unicode, save)
1040
715
        return conf
1041
716
 
1042
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1043
717
    def get_editor(self):
1044
718
        return self._get_user_option('editor')
1045
719
 
1074
748
        self.reload()
1075
749
        self._get_parser().setdefault(section, {})[option] = value
1076
750
        self._write_config_file()
1077
 
        for hook in OldConfigHooks['set']:
1078
 
            hook(self, option, value)
 
751
 
1079
752
 
1080
753
    def _get_sections(self, name=None):
1081
754
        """See IniBasedConfig._get_sections()."""
1100
773
        super(LockableConfig, self).remove_user_option(option_name,
1101
774
                                                       section_name)
1102
775
 
1103
 
def _iter_for_location_by_parts(sections, location):
1104
 
    """Keep only the sessions matching the specified location.
1105
 
 
1106
 
    :param sections: An iterable of section names.
1107
 
 
1108
 
    :param location: An url or a local path to match against.
1109
 
 
1110
 
    :returns: An iterator of (section, extra_path, nb_parts) where nb is the
1111
 
        number of path components in the section name, section is the section
1112
 
        name and extra_path is the difference between location and the section
1113
 
        name.
1114
 
 
1115
 
    ``location`` will always be a local path and never a 'file://' url but the
1116
 
    section names themselves can be in either form.
1117
 
    """
1118
 
    location_parts = location.rstrip('/').split('/')
1119
 
 
1120
 
    for section in sections:
1121
 
        # location is a local path if possible, so we need to convert 'file://'
1122
 
        # urls in section names to local paths if necessary.
1123
 
 
1124
 
        # This also avoids having file:///path be a more exact
1125
 
        # match than '/path'.
1126
 
 
1127
 
        # FIXME: This still raises an issue if a user defines both file:///path
1128
 
        # *and* /path. Should we raise an error in this case -- vila 20110505
1129
 
 
1130
 
        if section.startswith('file://'):
1131
 
            section_path = urlutils.local_path_from_url(section)
1132
 
        else:
1133
 
            section_path = section
1134
 
        section_parts = section_path.rstrip('/').split('/')
1135
 
 
1136
 
        matched = True
1137
 
        if len(section_parts) > len(location_parts):
1138
 
            # More path components in the section, they can't match
1139
 
            matched = False
1140
 
        else:
1141
 
            # Rely on zip truncating in length to the length of the shortest
1142
 
            # argument sequence.
1143
 
            names = zip(location_parts, section_parts)
1144
 
            for name in names:
1145
 
                if not fnmatch.fnmatch(name[0], name[1]):
1146
 
                    matched = False
1147
 
                    break
1148
 
        if not matched:
1149
 
            continue
1150
 
        # build the path difference between the section and the location
1151
 
        extra_path = '/'.join(location_parts[len(section_parts):])
1152
 
        yield section, extra_path, len(section_parts)
1153
 
 
1154
776
 
1155
777
class LocationConfig(LockableConfig):
1156
778
    """A configuration object that gives the policy for a location."""
1185
807
 
1186
808
    def _get_matching_sections(self):
1187
809
        """Return an ordered list of section names matching this location."""
1188
 
        matches = list(_iter_for_location_by_parts(self._get_parser(),
1189
 
                                                   self.location))
 
810
        sections = self._get_parser()
 
811
        location_names = self.location.split('/')
 
812
        if self.location.endswith('/'):
 
813
            del location_names[-1]
 
814
        matches=[]
 
815
        for section in sections:
 
816
            # location is a local path if possible, so we need
 
817
            # to convert 'file://' urls to local paths if necessary.
 
818
            # This also avoids having file:///path be a more exact
 
819
            # match than '/path'.
 
820
            if section.startswith('file://'):
 
821
                section_path = urlutils.local_path_from_url(section)
 
822
            else:
 
823
                section_path = section
 
824
            section_names = section_path.split('/')
 
825
            if section.endswith('/'):
 
826
                del section_names[-1]
 
827
            names = zip(location_names, section_names)
 
828
            matched = True
 
829
            for name in names:
 
830
                if not fnmatch.fnmatch(name[0], name[1]):
 
831
                    matched = False
 
832
                    break
 
833
            if not matched:
 
834
                continue
 
835
            # so, for the common prefix they matched.
 
836
            # if section is longer, no match.
 
837
            if len(section_names) > len(location_names):
 
838
                continue
 
839
            matches.append((len(section_names), section,
 
840
                            '/'.join(location_names[len(section_names):])))
1190
841
        # put the longest (aka more specific) locations first
1191
 
        matches.sort(
1192
 
            key=lambda (section, extra_path, length): (length, section),
1193
 
            reverse=True)
1194
 
        for (section, extra_path, length) in matches:
1195
 
            yield section, extra_path
 
842
        matches.sort(reverse=True)
 
843
        sections = []
 
844
        for (length, section, extra_path) in matches:
 
845
            sections.append((section, extra_path))
1196
846
            # should we stop looking for parent configs here?
1197
847
            try:
1198
848
                if self._get_parser()[section].as_bool('ignore_parents'):
1199
849
                    break
1200
850
            except KeyError:
1201
851
                pass
 
852
        return sections
1202
853
 
1203
854
    def _get_sections(self, name=None):
1204
855
        """See IniBasedConfig._get_sections()."""
1277
928
        # the allowed values of store match the config policies
1278
929
        self._set_option_policy(location, option, store)
1279
930
        self._write_config_file()
1280
 
        for hook in OldConfigHooks['set']:
1281
 
            hook(self, option, value)
1282
931
 
1283
932
 
1284
933
class BranchConfig(Config):
1451
1100
        """See Config.log_format."""
1452
1101
        return self._get_best_value('_log_format')
1453
1102
 
1454
 
    def _validate_signatures_in_log(self):
1455
 
        """See Config.validate_signatures_in_log."""
1456
 
        return self._get_best_value('_validate_signatures_in_log')
1457
 
 
1458
 
    def _acceptable_keys(self):
1459
 
        """See Config.acceptable_keys."""
1460
 
        return self._get_best_value('_acceptable_keys')
1461
 
 
1462
1103
 
1463
1104
def ensure_config_dir_exists(path=None):
1464
1105
    """Make sure a configuration directory exists.
1490
1131
    """
1491
1132
    base = os.environ.get('BZR_HOME', None)
1492
1133
    if sys.platform == 'win32':
1493
 
        # environ variables on Windows are in user encoding/mbcs. So decode
1494
 
        # before using one
1495
 
        if base is not None:
1496
 
            base = base.decode('mbcs')
1497
1134
        if base is None:
1498
1135
            base = win32utils.get_appdata_location_unicode()
1499
1136
        if base is None:
1500
1137
            base = os.environ.get('HOME', None)
1501
 
            if base is not None:
1502
 
                base = base.decode('mbcs')
1503
1138
        if base is None:
1504
1139
            raise errors.BzrError('You must have one of BZR_HOME, APPDATA,'
1505
1140
                                  ' or HOME set')
1572
1207
        return os.path.expanduser('~/.cache')
1573
1208
 
1574
1209
 
1575
 
def _get_default_mail_domain():
1576
 
    """If possible, return the assumed default email domain.
1577
 
 
1578
 
    :returns: string mail domain, or None.
1579
 
    """
1580
 
    if sys.platform == 'win32':
1581
 
        # No implementation yet; patches welcome
1582
 
        return None
1583
 
    try:
1584
 
        f = open('/etc/mailname')
1585
 
    except (IOError, OSError), e:
1586
 
        return None
1587
 
    try:
1588
 
        domain = f.read().strip()
1589
 
        return domain
1590
 
    finally:
1591
 
        f.close()
1592
 
 
1593
 
 
1594
 
def _auto_user_id():
1595
 
    """Calculate automatic user identification.
1596
 
 
1597
 
    :returns: (realname, email), either of which may be None if they can't be
1598
 
    determined.
1599
 
 
1600
 
    Only used when none is set in the environment or the id file.
1601
 
 
1602
 
    This only returns an email address if we can be fairly sure the 
1603
 
    address is reasonable, ie if /etc/mailname is set on unix.
1604
 
 
1605
 
    This doesn't use the FQDN as the default domain because that may be 
1606
 
    slow, and it doesn't use the hostname alone because that's not normally 
1607
 
    a reasonable address.
1608
 
    """
1609
 
    if sys.platform == 'win32':
1610
 
        # No implementation to reliably determine Windows default mail
1611
 
        # address; please add one.
1612
 
        return None, None
1613
 
 
1614
 
    default_mail_domain = _get_default_mail_domain()
1615
 
    if not default_mail_domain:
1616
 
        return None, None
1617
 
 
1618
 
    import pwd
1619
 
    uid = os.getuid()
1620
 
    try:
1621
 
        w = pwd.getpwuid(uid)
1622
 
    except KeyError:
1623
 
        trace.mutter('no passwd entry for uid %d?' % uid)
1624
 
        return None, None
1625
 
 
1626
 
    # we try utf-8 first, because on many variants (like Linux),
1627
 
    # /etc/passwd "should" be in utf-8, and because it's unlikely to give
1628
 
    # false positives.  (many users will have their user encoding set to
1629
 
    # latin-1, which cannot raise UnicodeError.)
1630
 
    try:
1631
 
        gecos = w.pw_gecos.decode('utf-8')
1632
 
        encoding = 'utf-8'
1633
 
    except UnicodeError:
1634
 
        try:
1635
 
            encoding = osutils.get_user_encoding()
1636
 
            gecos = w.pw_gecos.decode(encoding)
1637
 
        except UnicodeError, e:
1638
 
            trace.mutter("cannot decode passwd entry %s" % w)
1639
 
            return None, None
1640
 
    try:
1641
 
        username = w.pw_name.decode(encoding)
1642
 
    except UnicodeError, e:
1643
 
        trace.mutter("cannot decode passwd entry %s" % w)
1644
 
        return None, None
1645
 
 
1646
 
    comma = gecos.find(',')
1647
 
    if comma == -1:
1648
 
        realname = gecos
1649
 
    else:
1650
 
        realname = gecos[:comma]
1651
 
 
1652
 
    return realname, (username + '@' + default_mail_domain)
1653
 
 
1654
 
 
1655
1210
def parse_username(username):
1656
1211
    """Parse e-mail username and return a (name, address) tuple."""
1657
1212
    match = re.match(r'(.*?)\s*<?([\w+.-]+@[\w+.-]+)>?', username)
1748
1303
            self._config = ConfigObj(self._input, encoding='utf-8')
1749
1304
        except configobj.ConfigObjError, e:
1750
1305
            raise errors.ParseConfigError(e.errors, e.config.filename)
1751
 
        except UnicodeError:
1752
 
            raise errors.ConfigContentError(self._filename)
1753
1306
        return self._config
1754
1307
 
1755
1308
    def _save(self):
1772
1325
        section[option_name] = value
1773
1326
        self._save()
1774
1327
 
1775
 
    def get_credentials(self, scheme, host, port=None, user=None, path=None,
 
1328
    def get_credentials(self, scheme, host, port=None, user=None, path=None, 
1776
1329
                        realm=None):
1777
1330
        """Returns the matching credentials from authentication.conf file.
1778
1331
 
1946
1499
            if ask:
1947
1500
                if prompt is None:
1948
1501
                    # Create a default prompt suitable for most cases
1949
 
                    prompt = u'%s' % (scheme.upper(),) + u' %(host)s username'
 
1502
                    prompt = scheme.upper() + ' %(host)s username'
1950
1503
                # Special handling for optional fields in the prompt
1951
1504
                if port is not None:
1952
1505
                    prompt_host = '%s:%d' % (host, port)
1990
1543
        if password is None:
1991
1544
            if prompt is None:
1992
1545
                # Create a default prompt suitable for most cases
1993
 
                prompt = u'%s' % scheme.upper() + u' %(user)s@%(host)s password'
 
1546
                prompt = '%s' % scheme.upper() + ' %(user)s@%(host)s password'
1994
1547
            # Special handling for optional fields in the prompt
1995
1548
            if port is not None:
1996
1549
                prompt_host = '%s:%d' % (host, port)
2191
1744
                section_obj = configobj[section]
2192
1745
            except KeyError:
2193
1746
                return default
2194
 
        value = section_obj.get(name, default)
2195
 
        for hook in OldConfigHooks['get']:
2196
 
            hook(self, name, value)
2197
 
        return value
 
1747
        return section_obj.get(name, default)
2198
1748
 
2199
1749
    def set_option(self, value, name, section=None):
2200
1750
        """Set the value associated with a named option.
2208
1758
            configobj[name] = value
2209
1759
        else:
2210
1760
            configobj.setdefault(section, {})[name] = value
2211
 
        for hook in OldConfigHooks['set']:
2212
 
            hook(self, name, value)
2213
1761
        self._set_configobj(configobj)
2214
1762
 
2215
1763
    def remove_option(self, option_name, section_name=None):
2218
1766
            del configobj[option_name]
2219
1767
        else:
2220
1768
            del configobj[section_name][option_name]
2221
 
        for hook in OldConfigHooks['remove']:
2222
 
            hook(self, option_name)
2223
1769
        self._set_configobj(configobj)
2224
1770
 
2225
1771
    def _get_config_file(self):
2226
1772
        try:
2227
 
            f = StringIO(self._transport.get_bytes(self._filename))
2228
 
            for hook in OldConfigHooks['load']:
2229
 
                hook(self)
2230
 
            return f
 
1773
            return StringIO(self._transport.get_bytes(self._filename))
2231
1774
        except errors.NoSuchFile:
2232
1775
            return StringIO()
2233
1776
 
2234
 
    def _external_url(self):
2235
 
        return urlutils.join(self._transport.external_url(), self._filename)
2236
 
 
2237
1777
    def _get_configobj(self):
2238
1778
        f = self._get_config_file()
2239
1779
        try:
2240
 
            try:
2241
 
                conf = ConfigObj(f, encoding='utf-8')
2242
 
            except configobj.ConfigObjError, e:
2243
 
                raise errors.ParseConfigError(e.errors, self._external_url())
2244
 
            except UnicodeDecodeError:
2245
 
                raise errors.ConfigContentError(self._external_url())
 
1780
            return ConfigObj(f, encoding='utf-8')
2246
1781
        finally:
2247
1782
            f.close()
2248
 
        return conf
2249
1783
 
2250
1784
    def _set_configobj(self, configobj):
2251
1785
        out_file = StringIO()
2252
1786
        configobj.write(out_file)
2253
1787
        out_file.seek(0)
2254
1788
        self._transport.put_file(self._filename, out_file)
2255
 
        for hook in OldConfigHooks['save']:
2256
 
            hook(self)
2257
 
 
2258
 
 
2259
 
class Option(object):
2260
 
    """An option definition.
2261
 
 
2262
 
    The option *values* are stored in config files and found in sections.
2263
 
 
2264
 
    Here we define various properties about the option itself, its default
2265
 
    value, in which config files it can be stored, etc (TBC).
2266
 
    """
2267
 
 
2268
 
    def __init__(self, name, default=None):
2269
 
        self.name = name
2270
 
        self.default = default
2271
 
 
2272
 
    def get_default(self):
2273
 
        return self.default
2274
 
 
2275
 
 
2276
 
# Options registry
2277
 
 
2278
 
option_registry = registry.Registry()
2279
 
 
2280
 
 
2281
 
option_registry.register(
2282
 
    'editor', Option('editor'),
2283
 
    help='The command called to launch an editor to enter a message.')
2284
 
 
2285
 
 
2286
 
class Section(object):
2287
 
    """A section defines a dict of option name => value.
2288
 
 
2289
 
    This is merely a read-only dict which can add some knowledge about the
2290
 
    options. It is *not* a python dict object though and doesn't try to mimic
2291
 
    its API.
2292
 
    """
2293
 
 
2294
 
    def __init__(self, section_id, options):
2295
 
        self.id = section_id
2296
 
        # We re-use the dict-like object received
2297
 
        self.options = options
2298
 
 
2299
 
    def get(self, name, default=None):
2300
 
        return self.options.get(name, default)
2301
 
 
2302
 
    def __repr__(self):
2303
 
        # Mostly for debugging use
2304
 
        return "<config.%s id=%s>" % (self.__class__.__name__, self.id)
2305
 
 
2306
 
 
2307
 
_NewlyCreatedOption = object()
2308
 
"""Was the option created during the MutableSection lifetime"""
2309
 
 
2310
 
 
2311
 
class MutableSection(Section):
2312
 
    """A section allowing changes and keeping track of the original values."""
2313
 
 
2314
 
    def __init__(self, section_id, options):
2315
 
        super(MutableSection, self).__init__(section_id, options)
2316
 
        self.orig = {}
2317
 
 
2318
 
    def set(self, name, value):
2319
 
        if name not in self.options:
2320
 
            # This is a new option
2321
 
            self.orig[name] = _NewlyCreatedOption
2322
 
        elif name not in self.orig:
2323
 
            self.orig[name] = self.get(name, None)
2324
 
        self.options[name] = value
2325
 
 
2326
 
    def remove(self, name):
2327
 
        if name not in self.orig:
2328
 
            self.orig[name] = self.get(name, None)
2329
 
        del self.options[name]
2330
 
 
2331
 
 
2332
 
class Store(object):
2333
 
    """Abstract interface to persistent storage for configuration options."""
2334
 
 
2335
 
    readonly_section_class = Section
2336
 
    mutable_section_class = MutableSection
2337
 
 
2338
 
    def is_loaded(self):
2339
 
        """Returns True if the Store has been loaded.
2340
 
 
2341
 
        This is used to implement lazy loading and ensure the persistent
2342
 
        storage is queried only when needed.
2343
 
        """
2344
 
        raise NotImplementedError(self.is_loaded)
2345
 
 
2346
 
    def load(self):
2347
 
        """Loads the Store from persistent storage."""
2348
 
        raise NotImplementedError(self.load)
2349
 
 
2350
 
    def _load_from_string(self, bytes):
2351
 
        """Create a store from a string in configobj syntax.
2352
 
 
2353
 
        :param bytes: A string representing the file content.
2354
 
        """
2355
 
        raise NotImplementedError(self._load_from_string)
2356
 
 
2357
 
    def unload(self):
2358
 
        """Unloads the Store.
2359
 
 
2360
 
        This should make is_loaded() return False. This is used when the caller
2361
 
        knows that the persistent storage has changed or may have change since
2362
 
        the last load.
2363
 
        """
2364
 
        raise NotImplementedError(self.unload)
2365
 
 
2366
 
    def save(self):
2367
 
        """Saves the Store to persistent storage."""
2368
 
        raise NotImplementedError(self.save)
2369
 
 
2370
 
    def external_url(self):
2371
 
        raise NotImplementedError(self.external_url)
2372
 
 
2373
 
    def get_sections(self):
2374
 
        """Returns an ordered iterable of existing sections.
2375
 
 
2376
 
        :returns: An iterable of (name, dict).
2377
 
        """
2378
 
        raise NotImplementedError(self.get_sections)
2379
 
 
2380
 
    def get_mutable_section(self, section_name=None):
2381
 
        """Returns the specified mutable section.
2382
 
 
2383
 
        :param section_name: The section identifier
2384
 
        """
2385
 
        raise NotImplementedError(self.get_mutable_section)
2386
 
 
2387
 
    def __repr__(self):
2388
 
        # Mostly for debugging use
2389
 
        return "<config.%s(%s)>" % (self.__class__.__name__,
2390
 
                                    self.external_url())
2391
 
 
2392
 
 
2393
 
class IniFileStore(Store):
2394
 
    """A config Store using ConfigObj for storage.
2395
 
 
2396
 
    :ivar transport: The transport object where the config file is located.
2397
 
 
2398
 
    :ivar file_name: The config file basename in the transport directory.
2399
 
 
2400
 
    :ivar _config_obj: Private member to hold the ConfigObj instance used to
2401
 
        serialize/deserialize the config file.
2402
 
    """
2403
 
 
2404
 
    def __init__(self, transport, file_name):
2405
 
        """A config Store using ConfigObj for storage.
2406
 
 
2407
 
        :param transport: The transport object where the config file is located.
2408
 
 
2409
 
        :param file_name: The config file basename in the transport directory.
2410
 
        """
2411
 
        super(IniFileStore, self).__init__()
2412
 
        self.transport = transport
2413
 
        self.file_name = file_name
2414
 
        self._config_obj = None
2415
 
 
2416
 
    def is_loaded(self):
2417
 
        return self._config_obj != None
2418
 
 
2419
 
    def unload(self):
2420
 
        self._config_obj = None
2421
 
 
2422
 
    def load(self):
2423
 
        """Load the store from the associated file."""
2424
 
        if self.is_loaded():
2425
 
            return
2426
 
        content = self.transport.get_bytes(self.file_name)
2427
 
        self._load_from_string(content)
2428
 
        for hook in ConfigHooks['load']:
2429
 
            hook(self)
2430
 
 
2431
 
    def _load_from_string(self, bytes):
2432
 
        """Create a config store from a string.
2433
 
 
2434
 
        :param bytes: A string representing the file content.
2435
 
        """
2436
 
        if self.is_loaded():
2437
 
            raise AssertionError('Already loaded: %r' % (self._config_obj,))
2438
 
        co_input = StringIO(bytes)
2439
 
        try:
2440
 
            # The config files are always stored utf8-encoded
2441
 
            self._config_obj = ConfigObj(co_input, encoding='utf-8')
2442
 
        except configobj.ConfigObjError, e:
2443
 
            self._config_obj = None
2444
 
            raise errors.ParseConfigError(e.errors, self.external_url())
2445
 
        except UnicodeDecodeError:
2446
 
            raise errors.ConfigContentError(self.external_url())
2447
 
 
2448
 
    def save(self):
2449
 
        if not self.is_loaded():
2450
 
            # Nothing to save
2451
 
            return
2452
 
        out = StringIO()
2453
 
        self._config_obj.write(out)
2454
 
        self.transport.put_bytes(self.file_name, out.getvalue())
2455
 
        for hook in ConfigHooks['save']:
2456
 
            hook(self)
2457
 
 
2458
 
    def external_url(self):
2459
 
        # FIXME: external_url should really accepts an optional relpath
2460
 
        # parameter (bug #750169) :-/ -- vila 2011-04-04
2461
 
        # The following will do in the interim but maybe we don't want to
2462
 
        # expose a path here but rather a config ID and its associated
2463
 
        # object </hand wawe>.
2464
 
        return urlutils.join(self.transport.external_url(), self.file_name)
2465
 
 
2466
 
    def get_sections(self):
2467
 
        """Get the configobj section in the file order.
2468
 
 
2469
 
        :returns: An iterable of (name, dict).
2470
 
        """
2471
 
        # We need a loaded store
2472
 
        try:
2473
 
            self.load()
2474
 
        except errors.NoSuchFile:
2475
 
            # If the file doesn't exist, there is no sections
2476
 
            return
2477
 
        cobj = self._config_obj
2478
 
        if cobj.scalars:
2479
 
            yield self.readonly_section_class(None, cobj)
2480
 
        for section_name in cobj.sections:
2481
 
            yield self.readonly_section_class(section_name, cobj[section_name])
2482
 
 
2483
 
    def get_mutable_section(self, section_name=None):
2484
 
        # We need a loaded store
2485
 
        try:
2486
 
            self.load()
2487
 
        except errors.NoSuchFile:
2488
 
            # The file doesn't exist, let's pretend it was empty
2489
 
            self._load_from_string('')
2490
 
        if section_name is None:
2491
 
            section = self._config_obj
2492
 
        else:
2493
 
            section = self._config_obj.setdefault(section_name, {})
2494
 
        return self.mutable_section_class(section_name, section)
2495
 
 
2496
 
 
2497
 
# Note that LockableConfigObjStore inherits from ConfigObjStore because we need
2498
 
# unlockable stores for use with objects that can already ensure the locking
2499
 
# (think branches). If different stores (not based on ConfigObj) are created,
2500
 
# they may face the same issue.
2501
 
 
2502
 
 
2503
 
class LockableIniFileStore(IniFileStore):
2504
 
    """A ConfigObjStore using locks on save to ensure store integrity."""
2505
 
 
2506
 
    def __init__(self, transport, file_name, lock_dir_name=None):
2507
 
        """A config Store using ConfigObj for storage.
2508
 
 
2509
 
        :param transport: The transport object where the config file is located.
2510
 
 
2511
 
        :param file_name: The config file basename in the transport directory.
2512
 
        """
2513
 
        if lock_dir_name is None:
2514
 
            lock_dir_name = 'lock'
2515
 
        self.lock_dir_name = lock_dir_name
2516
 
        super(LockableIniFileStore, self).__init__(transport, file_name)
2517
 
        self._lock = lockdir.LockDir(self.transport, self.lock_dir_name)
2518
 
 
2519
 
    def lock_write(self, token=None):
2520
 
        """Takes a write lock in the directory containing the config file.
2521
 
 
2522
 
        If the directory doesn't exist it is created.
2523
 
        """
2524
 
        # FIXME: This doesn't check the ownership of the created directories as
2525
 
        # ensure_config_dir_exists does. It should if the transport is local
2526
 
        # -- vila 2011-04-06
2527
 
        self.transport.create_prefix()
2528
 
        return self._lock.lock_write(token)
2529
 
 
2530
 
    def unlock(self):
2531
 
        self._lock.unlock()
2532
 
 
2533
 
    def break_lock(self):
2534
 
        self._lock.break_lock()
2535
 
 
2536
 
    @needs_write_lock
2537
 
    def save(self):
2538
 
        # We need to be able to override the undecorated implementation
2539
 
        self.save_without_locking()
2540
 
 
2541
 
    def save_without_locking(self):
2542
 
        super(LockableIniFileStore, self).save()
2543
 
 
2544
 
 
2545
 
# FIXME: global, bazaar, shouldn't that be 'user' instead or even
2546
 
# 'user_defaults' as opposed to 'user_overrides', 'system_defaults'
2547
 
# (/etc/bzr/bazaar.conf) and 'system_overrides' ? -- vila 2011-04-05
2548
 
 
2549
 
# FIXME: Moreover, we shouldn't need classes for these stores either, factory
2550
 
# functions or a registry will make it easier and clearer for tests, focusing
2551
 
# on the relevant parts of the API that needs testing -- vila 20110503 (based
2552
 
# on a poolie's remark)
2553
 
class GlobalStore(LockableIniFileStore):
2554
 
 
2555
 
    def __init__(self, possible_transports=None):
2556
 
        t = transport.get_transport(config_dir(),
2557
 
                                    possible_transports=possible_transports)
2558
 
        super(GlobalStore, self).__init__(t, 'bazaar.conf')
2559
 
 
2560
 
 
2561
 
class LocationStore(LockableIniFileStore):
2562
 
 
2563
 
    def __init__(self, possible_transports=None):
2564
 
        t = transport.get_transport(config_dir(),
2565
 
                                    possible_transports=possible_transports)
2566
 
        super(LocationStore, self).__init__(t, 'locations.conf')
2567
 
 
2568
 
 
2569
 
class BranchStore(IniFileStore):
2570
 
 
2571
 
    def __init__(self, branch):
2572
 
        super(BranchStore, self).__init__(branch.control_transport,
2573
 
                                          'branch.conf')
2574
 
        self.branch = branch
2575
 
 
2576
 
    def lock_write(self, token=None):
2577
 
        return self.branch.lock_write(token)
2578
 
 
2579
 
    def unlock(self):
2580
 
        return self.branch.unlock()
2581
 
 
2582
 
    @needs_write_lock
2583
 
    def save(self):
2584
 
        # We need to be able to override the undecorated implementation
2585
 
        self.save_without_locking()
2586
 
 
2587
 
    def save_without_locking(self):
2588
 
        super(BranchStore, self).save()
2589
 
 
2590
 
 
2591
 
class SectionMatcher(object):
2592
 
    """Select sections into a given Store.
2593
 
 
2594
 
    This intended to be used to postpone getting an iterable of sections from a
2595
 
    store.
2596
 
    """
2597
 
 
2598
 
    def __init__(self, store):
2599
 
        self.store = store
2600
 
 
2601
 
    def get_sections(self):
2602
 
        # This is where we require loading the store so we can see all defined
2603
 
        # sections.
2604
 
        sections = self.store.get_sections()
2605
 
        # Walk the revisions in the order provided
2606
 
        for s in sections:
2607
 
            if self.match(s):
2608
 
                yield s
2609
 
 
2610
 
    def match(self, secion):
2611
 
        raise NotImplementedError(self.match)
2612
 
 
2613
 
 
2614
 
class LocationSection(Section):
2615
 
 
2616
 
    def __init__(self, section, length, extra_path):
2617
 
        super(LocationSection, self).__init__(section.id, section.options)
2618
 
        self.length = length
2619
 
        self.extra_path = extra_path
2620
 
 
2621
 
    def get(self, name, default=None):
2622
 
        value = super(LocationSection, self).get(name, default)
2623
 
        if value is not None:
2624
 
            policy_name = self.get(name + ':policy', None)
2625
 
            policy = _policy_value.get(policy_name, POLICY_NONE)
2626
 
            if policy == POLICY_APPENDPATH:
2627
 
                value = urlutils.join(value, self.extra_path)
2628
 
        return value
2629
 
 
2630
 
 
2631
 
class LocationMatcher(SectionMatcher):
2632
 
 
2633
 
    def __init__(self, store, location):
2634
 
        super(LocationMatcher, self).__init__(store)
2635
 
        if location.startswith('file://'):
2636
 
            location = urlutils.local_path_from_url(location)
2637
 
        self.location = location
2638
 
 
2639
 
    def _get_matching_sections(self):
2640
 
        """Get all sections matching ``location``."""
2641
 
        # We slightly diverge from LocalConfig here by allowing the no-name
2642
 
        # section as the most generic one and the lower priority.
2643
 
        no_name_section = None
2644
 
        sections = []
2645
 
        # Filter out the no_name_section so _iter_for_location_by_parts can be
2646
 
        # used (it assumes all sections have a name).
2647
 
        for section in self.store.get_sections():
2648
 
            if section.id is None:
2649
 
                no_name_section = section
2650
 
            else:
2651
 
                sections.append(section)
2652
 
        # Unfortunately _iter_for_location_by_parts deals with section names so
2653
 
        # we have to resync.
2654
 
        filtered_sections = _iter_for_location_by_parts(
2655
 
            [s.id for s in sections], self.location)
2656
 
        iter_sections = iter(sections)
2657
 
        matching_sections = []
2658
 
        if no_name_section is not None:
2659
 
            matching_sections.append(
2660
 
                LocationSection(no_name_section, 0, self.location))
2661
 
        for section_id, extra_path, length in filtered_sections:
2662
 
            # a section id is unique for a given store so it's safe to iterate
2663
 
            # again
2664
 
            section = iter_sections.next()
2665
 
            if section_id == section.id:
2666
 
                matching_sections.append(
2667
 
                    LocationSection(section, length, extra_path))
2668
 
        return matching_sections
2669
 
 
2670
 
    def get_sections(self):
2671
 
        # Override the default implementation as we want to change the order
2672
 
        matching_sections = self._get_matching_sections()
2673
 
        # We want the longest (aka more specific) locations first
2674
 
        sections = sorted(matching_sections,
2675
 
                          key=lambda section: (section.length, section.id),
2676
 
                          reverse=True)
2677
 
        # Sections mentioning 'ignore_parents' restrict the selection
2678
 
        for section in sections:
2679
 
            # FIXME: We really want to use as_bool below -- vila 2011-04-07
2680
 
            ignore = section.get('ignore_parents', None)
2681
 
            if ignore is not None:
2682
 
                ignore = ui.bool_from_string(ignore)
2683
 
            if ignore:
2684
 
                break
2685
 
            # Finally, we have a valid section
2686
 
            yield section
2687
 
 
2688
 
 
2689
 
class Stack(object):
2690
 
    """A stack of configurations where an option can be defined"""
2691
 
 
2692
 
    def __init__(self, sections_def, store=None, mutable_section_name=None):
2693
 
        """Creates a stack of sections with an optional store for changes.
2694
 
 
2695
 
        :param sections_def: A list of Section or callables that returns an
2696
 
            iterable of Section. This defines the Sections for the Stack and
2697
 
            can be called repeatedly if needed.
2698
 
 
2699
 
        :param store: The optional Store where modifications will be
2700
 
            recorded. If none is specified, no modifications can be done.
2701
 
 
2702
 
        :param mutable_section_name: The name of the MutableSection where
2703
 
            changes are recorded. This requires the ``store`` parameter to be
2704
 
            specified.
2705
 
        """
2706
 
        self.sections_def = sections_def
2707
 
        self.store = store
2708
 
        self.mutable_section_name = mutable_section_name
2709
 
 
2710
 
    def get(self, name):
2711
 
        """Return the *first* option value found in the sections.
2712
 
 
2713
 
        This is where we guarantee that sections coming from Store are loaded
2714
 
        lazily: the loading is delayed until we need to either check that an
2715
 
        option exists or get its value, which in turn may require to discover
2716
 
        in which sections it can be defined. Both of these (section and option
2717
 
        existence) require loading the store (even partially).
2718
 
        """
2719
 
        # FIXME: No caching of options nor sections yet -- vila 20110503
2720
 
        value = None
2721
 
        # Ensuring lazy loading is achieved by delaying section matching (which
2722
 
        # implies querying the persistent storage) until it can't be avoided
2723
 
        # anymore by using callables to describe (possibly empty) section
2724
 
        # lists.
2725
 
        for section_or_callable in self.sections_def:
2726
 
            # Each section can expand to multiple ones when a callable is used
2727
 
            if callable(section_or_callable):
2728
 
                sections = section_or_callable()
2729
 
            else:
2730
 
                sections = [section_or_callable]
2731
 
            for section in sections:
2732
 
                value = section.get(name)
2733
 
                if value is not None:
2734
 
                    break
2735
 
            if value is not None:
2736
 
                break
2737
 
        if value is None:
2738
 
            # If the option is registered, it may provide a default value
2739
 
            try:
2740
 
                opt = option_registry.get(name)
2741
 
            except KeyError:
2742
 
                # Not registered
2743
 
                opt = None
2744
 
            if opt is not None:
2745
 
                value = opt.get_default()
2746
 
        for hook in ConfigHooks['get']:
2747
 
            hook(self, name, value)
2748
 
        return value
2749
 
 
2750
 
    def _get_mutable_section(self):
2751
 
        """Get the MutableSection for the Stack.
2752
 
 
2753
 
        This is where we guarantee that the mutable section is lazily loaded:
2754
 
        this means we won't load the corresponding store before setting a value
2755
 
        or deleting an option. In practice the store will often be loaded but
2756
 
        this allows helps catching some programming errors.
2757
 
        """
2758
 
        section = self.store.get_mutable_section(self.mutable_section_name)
2759
 
        return section
2760
 
 
2761
 
    def set(self, name, value):
2762
 
        """Set a new value for the option."""
2763
 
        section = self._get_mutable_section()
2764
 
        section.set(name, value)
2765
 
        for hook in ConfigHooks['set']:
2766
 
            hook(self, name, value)
2767
 
 
2768
 
    def remove(self, name):
2769
 
        """Remove an existing option."""
2770
 
        section = self._get_mutable_section()
2771
 
        section.remove(name)
2772
 
        for hook in ConfigHooks['remove']:
2773
 
            hook(self, name)
2774
 
 
2775
 
    def __repr__(self):
2776
 
        # Mostly for debugging use
2777
 
        return "<config.%s(%s)>" % (self.__class__.__name__, id(self))
2778
 
 
2779
 
 
2780
 
class _CompatibleStack(Stack):
2781
 
    """Place holder for compatibility with previous design.
2782
 
 
2783
 
    This is intended to ease the transition from the Config-based design to the
2784
 
    Stack-based design and should not be used nor relied upon by plugins.
2785
 
 
2786
 
    One assumption made here is that the daughter classes will all use Stores
2787
 
    derived from LockableIniFileStore).
2788
 
 
2789
 
    It implements set() by re-loading the store before applying the
2790
 
    modification and saving it.
2791
 
 
2792
 
    The long term plan being to implement a single write by store to save
2793
 
    all modifications, this class should not be used in the interim.
2794
 
    """
2795
 
 
2796
 
    def set(self, name, value):
2797
 
        # Force a reload
2798
 
        self.store.unload()
2799
 
        super(_CompatibleStack, self).set(name, value)
2800
 
        # Force a write to persistent storage
2801
 
        self.store.save()
2802
 
 
2803
 
 
2804
 
class GlobalStack(_CompatibleStack):
2805
 
 
2806
 
    def __init__(self):
2807
 
        # Get a GlobalStore
2808
 
        gstore = GlobalStore()
2809
 
        super(GlobalStack, self).__init__([gstore.get_sections], gstore)
2810
 
 
2811
 
 
2812
 
class LocationStack(_CompatibleStack):
2813
 
 
2814
 
    def __init__(self, location):
2815
 
        lstore = LocationStore()
2816
 
        matcher = LocationMatcher(lstore, location)
2817
 
        gstore = GlobalStore()
2818
 
        super(LocationStack, self).__init__(
2819
 
            [matcher.get_sections, gstore.get_sections], lstore)
2820
 
 
2821
 
class BranchStack(_CompatibleStack):
2822
 
 
2823
 
    def __init__(self, branch):
2824
 
        bstore = BranchStore(branch)
2825
 
        lstore = LocationStore()
2826
 
        matcher = LocationMatcher(lstore, branch.base)
2827
 
        gstore = GlobalStore()
2828
 
        super(BranchStack, self).__init__(
2829
 
            [matcher.get_sections, bstore.get_sections, gstore.get_sections],
2830
 
            bstore)
2831
 
        self.branch = branch
2832
1789
 
2833
1790
 
2834
1791
class cmd_config(commands.Command):
2863
1820
                        ' the configuration file'),
2864
1821
        ]
2865
1822
 
2866
 
    _see_also = ['configuration']
2867
 
 
2868
1823
    @commands.display_command
2869
1824
    def run(self, name=None, all=False, directory=None, scope=None,
2870
1825
            remove=False):
2944
1899
            raise errors.NoSuchConfigOption(name)
2945
1900
 
2946
1901
    def _show_matching_options(self, name, directory, scope):
2947
 
        name = lazy_regex.lazy_compile(name)
 
1902
        name = re.compile(name)
2948
1903
        # We want any error in the regexp to be raised *now* so we need to
2949
 
        # avoid the delay introduced by the lazy regexp.  But, we still do
2950
 
        # want the nicer errors raised by lazy_regex.
 
1904
        # avoid the delay introduced by the lazy regexp.
2951
1905
        name._compile_and_collapse()
2952
1906
        cur_conf_id = None
2953
1907
        cur_section = None
2997
1951
            raise errors.NoSuchConfig(scope)
2998
1952
        if not removed:
2999
1953
            raise errors.NoSuchConfigOption(name)
3000
 
 
3001
 
# Test registries
3002
 
#
3003
 
# We need adapters that can build a Store or a Stack in a test context. Test
3004
 
# classes, based on TestCaseWithTransport, can use the registry to parametrize
3005
 
# themselves. The builder will receive a test instance and should return a
3006
 
# ready-to-use store or stack.  Plugins that define new store/stacks can also
3007
 
# register themselves here to be tested against the tests defined in
3008
 
# bzrlib.tests.test_config. Note that the builder can be called multiple times
3009
 
# for the same tests.
3010
 
 
3011
 
# The registered object should be a callable receiving a test instance
3012
 
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Store
3013
 
# object.
3014
 
test_store_builder_registry = registry.Registry()
3015
 
 
3016
 
# The registered object should be a callable receiving a test instance
3017
 
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Stack
3018
 
# object.
3019
 
test_stack_builder_registry = registry.Registry()