68
from bzrlib.lazy_import import lazy_import
69
lazy_import(globals(), """
71
66
from fnmatch import fnmatch
73
70
from StringIO import StringIO
73
from bzrlib import errors, urlutils
74
from bzrlib.osutils import pathjoin
75
from bzrlib.trace import mutter, warning
83
76
import bzrlib.util.configobj.configobj as configobj
86
from bzrlib.trace import mutter, warning
89
79
CHECK_IF_POSSIBLE=0
101
POLICY_APPENDPATH = 2
105
POLICY_NORECURSE: 'norecurse',
106
POLICY_APPENDPATH: 'appendpath',
111
'norecurse': POLICY_NORECURSE,
112
'appendpath': POLICY_APPENDPATH,
116
STORE_LOCATION = POLICY_NONE
117
STORE_LOCATION_NORECURSE = POLICY_NORECURSE
118
STORE_LOCATION_APPENDPATH = POLICY_APPENDPATH
123
89
class ConfigObj(configobj.ConfigObj):
125
91
def get_bool(self, section, key):
289
255
raise errors.ParseConfigError(e.errors, e.config.filename)
290
256
return self._parser
292
def _get_matching_sections(self):
293
"""Return an ordered list of (section_name, extra_path) pairs.
295
If the section contains inherited configuration, extra_path is
296
a string containing the additional path components.
298
section = self._get_section()
299
if section is not None:
300
return [(section, '')]
304
258
def _get_section(self):
305
259
"""Override this to define the section used by the config."""
308
def _get_option_policy(self, section, option_name):
309
"""Return the policy for the given (section, option_name) pair."""
312
262
def _get_signature_checking(self):
313
263
"""See Config._get_signature_checking."""
314
264
policy = self._get_user_option('check_signatures')
328
278
def _get_user_option(self, option_name):
329
279
"""See Config._get_user_option."""
330
for (section, extra_path) in self._get_matching_sections():
332
value = self._get_parser().get_value(section, option_name)
335
policy = self._get_option_policy(section, option_name)
336
if policy == POLICY_NONE:
338
elif policy == POLICY_NORECURSE:
339
# norecurse items only apply to the exact path
344
elif policy == POLICY_APPENDPATH:
346
value = urlutils.join(value, extra_path)
349
raise AssertionError('Unexpected config policy %r' % policy)
281
return self._get_parser().get_value(self._get_section(),
353
286
def _gpg_signing_command(self):
354
287
"""See Config.gpg_signing_command."""
446
379
location = urlutils.local_path_from_url(location)
447
380
self.location = location
449
def _get_matching_sections(self):
450
"""Return an ordered list of section names matching this location."""
382
def _get_section(self):
383
"""Get the section we should look in for config items.
385
Returns None if none exists.
386
TODO: perhaps return a NullSection that thunks through to the
451
389
sections = self._get_parser()
452
390
location_names = self.location.split('/')
453
391
if self.location.endswith('/'):
477
415
# if section is longer, no match.
478
416
if len(section_names) > len(location_names):
480
matches.append((len(section_names), section,
481
'/'.join(location_names[len(section_names):])))
418
# if path is longer, and recurse is not true, no match
419
if len(section_names) < len(location_names):
421
if not self._get_parser()[section].as_bool('recurse'):
425
matches.append((len(section_names), section))
482
428
matches.sort(reverse=True)
484
for (length, section, extra_path) in matches:
485
sections.append((section, extra_path))
486
# should we stop looking for parent configs here?
488
if self._get_parser()[section].as_bool('ignore_parents'):
494
def _get_option_policy(self, section, option_name):
495
"""Return the policy for the given (section, option_name) pair."""
496
# check for the old 'recurse=False' flag
498
recurse = self._get_parser()[section].as_bool('recurse')
502
return POLICY_NORECURSE
504
policy_key = option_name + ':policy'
506
policy_name = self._get_parser()[section][policy_key]
510
return _policy_value[policy_name]
512
def _set_option_policy(self, section, option_name, option_policy):
513
"""Set the policy for the given option name in the given section."""
514
# The old recurse=False option affects all options in the
515
# section. To handle multiple policies in the section, we
516
# need to convert it to a policy_norecurse key.
518
recurse = self._get_parser()[section].as_bool('recurse')
522
symbol_versioning.warn(
523
'The recurse option is deprecated as of 0.14. '
524
'The section "%s" has been converted to use policies.'
527
del self._get_parser()[section]['recurse']
529
for key in self._get_parser()[section].keys():
530
if not key.endswith(':policy'):
531
self._get_parser()[section][key +
532
':policy'] = 'norecurse'
534
policy_key = option_name + ':policy'
535
policy_name = _policy_name[option_policy]
536
if policy_name is not None:
537
self._get_parser()[section][policy_key] = policy_name
539
if policy_key in self._get_parser()[section]:
540
del self._get_parser()[section][policy_key]
542
def set_user_option(self, option, value, store=STORE_LOCATION):
431
def set_user_option(self, option, value):
543
432
"""Save option and its value in the configuration."""
544
assert store in [STORE_LOCATION,
545
STORE_LOCATION_NORECURSE,
546
STORE_LOCATION_APPENDPATH], 'bad storage policy'
547
433
# FIXME: RBC 20051029 This should refresh the parser and also take a
548
434
# file lock on locations.conf.
549
435
conf_dir = os.path.dirname(self._get_filename())
642
def set_user_option(self, name, value, store=STORE_BRANCH):
643
if store == STORE_BRANCH:
526
def set_user_option(self, name, value, local=False):
528
self._get_location_config().set_user_option(name, value)
644
530
self._get_branch_data_config().set_option(value, name)
645
elif store == STORE_GLOBAL:
646
self._get_global_config().set_user_option(name, value)
648
self._get_location_config().set_user_option(name, value, store)
650
533
def _gpg_signing_command(self):
651
534
"""See Config.gpg_signing_command."""
711
594
base = os.environ.get('BZR_HOME', None)
712
595
if sys.platform == 'win32':
714
base = win32utils.get_appdata_location_unicode()
597
base = os.environ.get('APPDATA', None)
716
599
base = os.environ.get('HOME', None)
718
601
raise errors.BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
719
return osutils.pathjoin(base, 'bazaar', '2.0')
602
return pathjoin(base, 'bazaar', '2.0')
721
604
# cygwin, linux, and darwin all have a $HOME directory
723
606
base = os.path.expanduser("~")
724
return osutils.pathjoin(base, ".bazaar")
607
return pathjoin(base, ".bazaar")
727
610
def config_filename():
728
611
"""Return per-user configuration ini file filename."""
729
return osutils.pathjoin(config_dir(), 'bazaar.conf')
612
return pathjoin(config_dir(), 'bazaar.conf')
732
615
def branches_config_filename():
733
616
"""Return per-user configuration ini file filename."""
734
return osutils.pathjoin(config_dir(), 'branches.conf')
617
return pathjoin(config_dir(), 'branches.conf')
737
620
def locations_config_filename():
738
621
"""Return per-user configuration ini file filename."""
739
return osutils.pathjoin(config_dir(), 'locations.conf')
622
return pathjoin(config_dir(), 'locations.conf')
742
625
def user_ignore_config_filename():
743
626
"""Return the user default ignore filename"""
744
return osutils.pathjoin(config_dir(), 'ignore')
627
return pathjoin(config_dir(), 'ignore')
747
630
def _auto_user_id():
760
if sys.platform == 'win32':
761
name = win32utils.get_user_name_unicode()
763
raise errors.BzrError("Cannot autodetect user name.\n"
764
"Please, set your name with command like:\n"
765
'bzr whoami "Your Name <name@domain.com>"')
766
host = win32utils.get_host_name_unicode()
768
host = socket.gethostname()
769
return name, (name + '@' + host)
643
# XXX: Any good way to get real user name on win32?