/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Matthieu Moy
  • Date: 2006-07-08 19:32:30 UTC
  • mfrom: (1845 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1857.
  • Revision ID: Matthieu.Moy@imag.fr-20060708193230-3eb72d871471bd5b
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Configuration that affects the behaviour of Bazaar.
19
19
 
20
20
Currently this configuration resides in ~/.bazaar/bazaar.conf
21
 
and ~/.bazaar/branches.conf, which is written to by bzr.
 
21
and ~/.bazaar/locations.conf, which is written to by bzr.
22
22
 
23
23
In bazaar.conf the following options may be set:
24
24
[DEFAULT]
29
29
gpg_signing_command=name-of-program
30
30
log_format=name-of-format
31
31
 
32
 
in branches.conf, you specify the url of a branch and options for it.
 
32
in locations.conf, you specify the url of a branch and options for it.
33
33
Wildcards may be used - * and ? as normal in shell completion. Options
34
 
set in both bazaar.conf and branches.conf are overriden by the branches.conf
 
34
set in both bazaar.conf and locations.conf are overridden by the locations.conf
35
35
setting.
36
36
[/home/robertc/source]
37
37
recurse=False|True(default)
38
38
email= as above
39
 
check_signatures= as abive 
 
39
check_signatures= as above 
40
40
create_signatures= as above.
41
41
 
42
42
explanation of options
49
49
create_signatures - this option controls whether bzr will always create 
50
50
                    gpg signatures, never create them, or create them if the
51
51
                    branch is configured to require them.
52
 
                    NB: This option is planned, but not implemented yet.
53
52
log_format - This options set the default log format.  Options are long, 
54
53
             short, line, or a plugin can register new formats
55
54
 
64
63
 
65
64
 
66
65
import errno
 
66
from fnmatch import fnmatch
67
67
import os
 
68
import re
68
69
import sys
69
 
from fnmatch import fnmatch
70
 
import re
 
70
from StringIO import StringIO
71
71
 
72
72
import bzrlib
73
73
import bzrlib.errors as errors
74
74
from bzrlib.osutils import pathjoin
75
 
from bzrlib.trace import mutter
 
75
from bzrlib.trace import mutter, warning
76
76
import bzrlib.util.configobj.configobj as configobj
77
 
from StringIO import StringIO
 
77
 
78
78
 
79
79
CHECK_IF_POSSIBLE=0
80
80
CHECK_ALWAYS=1
81
81
CHECK_NEVER=2
82
82
 
83
83
 
 
84
SIGN_WHEN_REQUIRED=0
 
85
SIGN_ALWAYS=1
 
86
SIGN_NEVER=2
 
87
 
 
88
 
84
89
class ConfigObj(configobj.ConfigObj):
85
90
 
86
91
    def get_bool(self, section, key):
106
111
    def _get_signature_checking(self):
107
112
        """Template method to override signature checking policy."""
108
113
 
 
114
    def _get_signing_policy(self):
 
115
        """Template method to override signature creation policy."""
 
116
 
109
117
    def _get_user_option(self, option_name):
110
118
        """Template method to provide a user option."""
111
119
        return None
192
200
            return policy
193
201
        return CHECK_IF_POSSIBLE
194
202
 
 
203
    def signing_policy(self):
 
204
        """What is the current policy for signature checking?."""
 
205
        policy = self._get_signing_policy()
 
206
        if policy is not None:
 
207
            return policy
 
208
        return SIGN_WHEN_REQUIRED
 
209
 
195
210
    def signature_needed(self):
196
211
        """Is a signature needed when committing ?."""
197
 
        policy = self._get_signature_checking()
198
 
        if policy == CHECK_ALWAYS:
 
212
        policy = self._get_signing_policy()
 
213
        if policy is None:
 
214
            policy = self._get_signature_checking()
 
215
            if policy is not None:
 
216
                warning("Please use create_signatures, not check_signatures "
 
217
                        "to set signing policy.")
 
218
            if policy == CHECK_ALWAYS:
 
219
                return True
 
220
        elif policy == SIGN_ALWAYS:
199
221
            return True
200
222
        return False
201
223
 
205
227
    def _get_alias(self, value):
206
228
        pass
207
229
 
 
230
    def get_nickname(self):
 
231
        return self._get_nickname()
 
232
 
 
233
    def _get_nickname(self):
 
234
        return None
 
235
 
208
236
 
209
237
class IniBasedConfig(Config):
210
238
    """A configuration policy that draws from ini files."""
232
260
        if policy:
233
261
            return self._string_to_signature_policy(policy)
234
262
 
 
263
    def _get_signing_policy(self):
 
264
        """See Config._get_signing_policy"""
 
265
        policy = self._get_user_option('create_signatures')
 
266
        if policy:
 
267
            return self._string_to_signing_policy(policy)
 
268
 
235
269
    def _get_user_id(self):
236
270
        """Get the user id from the 'email' key in the current section."""
237
271
        return self._get_user_option('email')
272
306
        raise errors.BzrError("Invalid signatures policy '%s'"
273
307
                              % signature_string)
274
308
 
 
309
    def _string_to_signing_policy(self, signature_string):
 
310
        """Convert a string to a signing policy."""
 
311
        if signature_string.lower() == 'when-required':
 
312
            return SIGN_WHEN_REQUIRED
 
313
        if signature_string.lower() == 'never':
 
314
            return SIGN_NEVER
 
315
        if signature_string.lower() == 'always':
 
316
            return SIGN_ALWAYS
 
317
        raise errors.BzrError("Invalid signing policy '%s'"
 
318
                              % signature_string)
 
319
 
275
320
    def _get_alias(self, value):
276
321
        try:
277
322
            return self._get_parser().get_value("ALIASES", 
279
324
        except KeyError:
280
325
            pass
281
326
 
 
327
    def _get_nickname(self):
 
328
        return self.get_user_option('nickname')
 
329
 
282
330
 
283
331
class GlobalConfig(IniBasedConfig):
284
332
    """The configuration that should be used for a specific location."""
289
337
    def __init__(self):
290
338
        super(GlobalConfig, self).__init__(config_filename)
291
339
 
 
340
    def set_user_option(self, option, value):
 
341
        """Save option and its value in the configuration."""
 
342
        # FIXME: RBC 20051029 This should refresh the parser and also take a
 
343
        # file lock on bazaar.conf.
 
344
        conf_dir = os.path.dirname(self._get_filename())
 
345
        ensure_config_dir_exists(conf_dir)
 
346
        if 'DEFAULT' not in self._get_parser():
 
347
            self._get_parser()['DEFAULT'] = {}
 
348
        self._get_parser()['DEFAULT'][option] = value
 
349
        f = open(self._get_filename(), 'wb')
 
350
        self._get_parser().write(f)
 
351
        f.close()
 
352
 
292
353
 
293
354
class LocationConfig(IniBasedConfig):
294
355
    """A configuration object that gives the policy for a location."""
295
356
 
296
357
    def __init__(self, location):
297
 
        super(LocationConfig, self).__init__(branches_config_filename)
298
 
        self._global_config = None
 
358
        name_generator = locations_config_filename
 
359
        if (not os.path.exists(name_generator()) and 
 
360
                os.path.exists(branches_config_filename())):
 
361
            if sys.platform == 'win32':
 
362
                warning('Please rename %s to %s' 
 
363
                         % (branches_config_filename(),
 
364
                            locations_config_filename()))
 
365
            else:
 
366
                warning('Please rename ~/.bazaar/branches.conf'
 
367
                        ' to ~/.bazaar/locations.conf')
 
368
            name_generator = branches_config_filename
 
369
        super(LocationConfig, self).__init__(name_generator)
299
370
        self.location = location
300
371
 
301
 
    def _get_global_config(self):
302
 
        if self._global_config is None:
303
 
            self._global_config = GlobalConfig()
304
 
        return self._global_config
305
 
 
306
372
    def _get_section(self):
307
373
        """Get the section we should look in for config items.
308
374
 
344
410
        matches.sort(reverse=True)
345
411
        return matches[0][1]
346
412
 
347
 
    def _gpg_signing_command(self):
348
 
        """See Config.gpg_signing_command."""
349
 
        command = super(LocationConfig, self)._gpg_signing_command()
350
 
        if command is not None:
351
 
            return command
352
 
        return self._get_global_config()._gpg_signing_command()
353
 
 
354
 
    def _log_format(self):
355
 
        """See Config.log_format."""
356
 
        command = super(LocationConfig, self)._log_format()
357
 
        if command is not None:
358
 
            return command
359
 
        return self._get_global_config()._log_format()
360
 
 
361
 
    def _get_user_id(self):
362
 
        user_id = super(LocationConfig, self)._get_user_id()
363
 
        if user_id is not None:
364
 
            return user_id
365
 
        return self._get_global_config()._get_user_id()
366
 
 
367
 
    def _get_user_option(self, option_name):
368
 
        """See Config._get_user_option."""
369
 
        option_value = super(LocationConfig, 
370
 
                             self)._get_user_option(option_name)
371
 
        if option_value is not None:
372
 
            return option_value
373
 
        return self._get_global_config()._get_user_option(option_name)
374
 
 
375
 
    def _get_signature_checking(self):
376
 
        """See Config._get_signature_checking."""
377
 
        check = super(LocationConfig, self)._get_signature_checking()
378
 
        if check is not None:
379
 
            return check
380
 
        return self._get_global_config()._get_signature_checking()
381
 
 
382
 
    def _post_commit(self):
383
 
        """See Config.post_commit."""
384
 
        hook = self._get_user_option('post_commit')
385
 
        if hook is not None:
386
 
            return hook
387
 
        return self._get_global_config()._post_commit()
388
 
 
389
413
    def set_user_option(self, option, value):
390
414
        """Save option and its value in the configuration."""
391
415
        # FIXME: RBC 20051029 This should refresh the parser and also take a
392
 
        # file lock on branches.conf.
 
416
        # file lock on locations.conf.
393
417
        conf_dir = os.path.dirname(self._get_filename())
394
418
        ensure_config_dir_exists(conf_dir)
395
419
        location = self.location
407
431
class BranchConfig(Config):
408
432
    """A configuration object giving the policy for a branch."""
409
433
 
 
434
    def _get_branch_data_config(self):
 
435
        if self._branch_data_config is None:
 
436
            self._branch_data_config = TreeConfig(self.branch)
 
437
        return self._branch_data_config
 
438
 
410
439
    def _get_location_config(self):
411
440
        if self._location_config is None:
412
441
            self._location_config = LocationConfig(self.branch.base)
413
442
        return self._location_config
414
443
 
 
444
    def _get_global_config(self):
 
445
        if self._global_config is None:
 
446
            self._global_config = GlobalConfig()
 
447
        return self._global_config
 
448
 
 
449
    def _get_best_value(self, option_name):
 
450
        """This returns a user option from local, tree or global config.
 
451
 
 
452
        They are tried in that order.  Use get_safe_value if trusted values
 
453
        are necessary.
 
454
        """
 
455
        for source in self.option_sources:
 
456
            value = getattr(source(), option_name)()
 
457
            if value is not None:
 
458
                return value
 
459
        return None
 
460
 
 
461
    def _get_safe_value(self, option_name):
 
462
        """This variant of get_best_value never returns untrusted values.
 
463
        
 
464
        It does not return values from the branch data, because the branch may
 
465
        not be controlled by the user.
 
466
 
 
467
        We may wish to allow locations.conf to control whether branches are
 
468
        trusted in the future.
 
469
        """
 
470
        for source in (self._get_location_config, self._get_global_config):
 
471
            value = getattr(source(), option_name)()
 
472
            if value is not None:
 
473
                return value
 
474
        return None
 
475
 
415
476
    def _get_user_id(self):
416
477
        """Return the full user id for the branch.
417
478
    
426
487
        except errors.NoSuchFile, e:
427
488
            pass
428
489
        
429
 
        return self._get_location_config()._get_user_id()
 
490
        return self._get_best_value('_get_user_id')
430
491
 
431
492
    def _get_signature_checking(self):
432
493
        """See Config._get_signature_checking."""
433
 
        return self._get_location_config()._get_signature_checking()
 
494
        return self._get_best_value('_get_signature_checking')
 
495
 
 
496
    def _get_signing_policy(self):
 
497
        """See Config._get_signing_policy."""
 
498
        return self._get_best_value('_get_signing_policy')
434
499
 
435
500
    def _get_user_option(self, option_name):
436
501
        """See Config._get_user_option."""
437
 
        return self._get_location_config()._get_user_option(option_name)
 
502
        for source in self.option_sources:
 
503
            value = source()._get_user_option(option_name)
 
504
            if value is not None:
 
505
                return value
 
506
        return None
 
507
 
 
508
    def set_user_option(self, name, value, local=False):
 
509
        if local is True:
 
510
            self._get_location_config().set_user_option(name, value)
 
511
        else:
 
512
            self._get_branch_data_config().set_option(value, name)
 
513
 
438
514
 
439
515
    def _gpg_signing_command(self):
440
516
        """See Config.gpg_signing_command."""
441
 
        return self._get_location_config()._gpg_signing_command()
 
517
        return self._get_safe_value('_gpg_signing_command')
442
518
        
443
519
    def __init__(self, branch):
444
520
        super(BranchConfig, self).__init__()
445
521
        self._location_config = None
 
522
        self._branch_data_config = None
 
523
        self._global_config = None
446
524
        self.branch = branch
 
525
        self.option_sources = (self._get_location_config, 
 
526
                               self._get_branch_data_config,
 
527
                               self._get_global_config)
447
528
 
448
529
    def _post_commit(self):
449
530
        """See Config.post_commit."""
450
 
        return self._get_location_config()._post_commit()
 
531
        return self._get_safe_value('_post_commit')
 
532
 
 
533
    def _get_nickname(self):
 
534
        value = self._get_explicit_nickname()
 
535
        if value is not None:
 
536
            return value
 
537
        return self.branch.base.split('/')[-2]
 
538
 
 
539
    def has_explicit_nickname(self):
 
540
        """Return true if a nickname has been explicitly assigned."""
 
541
        return self._get_explicit_nickname() is not None
 
542
 
 
543
    def _get_explicit_nickname(self):
 
544
        return self._get_best_value('_get_nickname')
451
545
 
452
546
    def _log_format(self):
453
547
        """See Config.log_format."""
454
 
        return self._get_location_config()._log_format()
 
548
        return self._get_best_value('_log_format')
455
549
 
456
550
 
457
551
def ensure_config_dir_exists(path=None):
486
580
        if base is None:
487
581
            base = os.environ.get('HOME', None)
488
582
        if base is None:
489
 
            raise BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
 
583
            raise errors.BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
490
584
        return pathjoin(base, 'bazaar', '2.0')
491
585
    else:
492
586
        # cygwin, linux, and darwin all have a $HOME directory
505
599
    return pathjoin(config_dir(), 'branches.conf')
506
600
 
507
601
 
 
602
def locations_config_filename():
 
603
    """Return per-user configuration ini file filename."""
 
604
    return pathjoin(config_dir(), 'locations.conf')
 
605
 
 
606
 
508
607
def _auto_user_id():
509
608
    """Calculate automatic user identification.
510
609
 
525
624
        uid = os.getuid()
526
625
        w = pwd.getpwuid(uid)
527
626
 
528
 
        try:
529
 
            gecos = w.pw_gecos.decode(bzrlib.user_encoding)
530
 
            username = w.pw_name.decode(bzrlib.user_encoding)
531
 
        except UnicodeDecodeError:
532
 
            # We're using pwd, therefore we're on Unix, so /etc/passwd is ok.
533
 
            raise errors.BzrError("Can't decode username in " \
534
 
                    "/etc/passwd as %s." % bzrlib.user_encoding)
 
627
        # we try utf-8 first, because on many variants (like Linux),
 
628
        # /etc/passwd "should" be in utf-8, and because it's unlikely to give
 
629
        # false positives.  (many users will have their user encoding set to
 
630
        # latin-1, which cannot raise UnicodeError.)
 
631
        try:
 
632
            gecos = w.pw_gecos.decode('utf-8')
 
633
            encoding = 'utf-8'
 
634
        except UnicodeError:
 
635
            try:
 
636
                gecos = w.pw_gecos.decode(bzrlib.user_encoding)
 
637
                encoding = bzrlib.user_encoding
 
638
            except UnicodeError:
 
639
                raise errors.BzrCommandError('Unable to determine your name.  '
 
640
                   'Use "bzr whoami" to set it.')
 
641
        try:
 
642
            username = w.pw_name.decode(encoding)
 
643
        except UnicodeError:
 
644
            raise errors.BzrCommandError('Unable to determine your name.  '
 
645
                'Use "bzr whoami" to set it.')
535
646
 
536
647
        comma = gecos.find(',')
537
648
        if comma == -1:
568
679
                              "a reasonable email address" % e)
569
680
    return m.group(0)
570
681
 
571
 
class TreeConfig(object):
 
682
 
 
683
class TreeConfig(IniBasedConfig):
572
684
    """Branch configuration data associated with its contents, not location"""
573
685
    def __init__(self, branch):
574
686
        self.branch = branch
575
687
 
 
688
    def _get_parser(self, file=None):
 
689
        if file is not None:
 
690
            return IniBasedConfig._get_parser(file)
 
691
        return self._get_config()
 
692
 
576
693
    def _get_config(self):
577
694
        try:
578
695
            obj = ConfigObj(self.branch.control_files.get('branch.conf'),