192
171
def _get_signing_policy(self):
193
172
"""Template method to override signature creation policy."""
197
def interpolate(self, string, env=None):
198
"""Interpolate the string in the configuration context.
200
:param string: The string to interpolate
202
:param env: An option dict defining additional configuration options or
203
overriding existing ones.
205
:returns: The interpolated string.
207
return self._interpolate_string(string, env)
209
def _interpolate_list(self, slist, env=None, _ref_stack=None):
210
"""Interpolate a list of strings in the configuration context.
212
:param slist: A list of strings.
214
:param env: An option dict defining additional configuration options or
215
overriding existing ones.
217
:param _ref_stack: Private list containing the options being
218
interpolated to detect loops.
220
:returns: The flatten list of interpolated strings.
222
# interpolate each value separately flattening lists
225
value = self._interpolate_string(s, env, _ref_stack)
226
if isinstance(value, list):
232
def _interpolate_string(self, string, env=None, _ref_stack=None):
233
"""Interpolate the string in the configuration context.
235
:param string: The string to interpolate
237
:param env: An option dict defining additional configuration options or
238
overriding existing ones.
240
:param _ref_stack: Private list containing the options being
241
interpolated to detect loops.
243
:returns: The interpolated string.
246
# Not much to interpolate there
248
if _ref_stack is None:
249
# What references are currently resolved (to detect loops)
251
if self.option_ref_re is None:
252
# We want to match the most embedded reference first (i.e. for
253
# '{{foo}}' we will get '{foo}',
254
# for '{bar{baz}}' we will get '{baz}'
255
self.option_ref_re = re.compile('({[^{}]+})')
257
# We need to iterate until no more refs appear ({{foo}} will need two
258
# iterations for example).
261
raw_chunks = self.option_ref_re.split(result)
263
import pdb; pdb.set_trace()
264
if len(raw_chunks) == 1:
265
# Shorcut the trivial case: no refs
269
# Split will isolate refs so that every other chunk is a ref
271
for chunk in raw_chunks:
274
# Keep only non-empty strings
279
if name in _ref_stack:
280
raise errors.InterpolationLoop(string, _ref_stack)
281
_ref_stack.append(name)
282
value = self._interpolate_option(name, env, _ref_stack)
284
raise errors.InterpolationUnknownOption(name, string)
285
if isinstance(value, list):
293
# Once a list appears as the result of an interpolation, all
294
# callers will get a list result. This allows a consistent
295
# behavior even when some options in the interpolation chain
296
# may be seen defined as strings even if their interpolated
298
return self._interpolate_list(chunks, env, _ref_stack)
300
result = ''.join(chunks)
303
def _interpolate_option(self, name, env, _ref_stack):
304
if env is not None and name in env:
305
# Special case, values provided in env takes precedence over
309
# FIXME: This is a limited implementation, what we really need
310
# is a way to query the bzr config for the value of an option,
311
# respecting the scope rules -- vila 20101222
312
value = self.get_user_option(name, interpolate=False)
313
if isinstance(value, list):
314
value = self._interpolate_list(value, env, _ref_stack)
316
value = self._interpolate_string(value, env, _ref_stack)
319
174
def _get_user_option(self, option_name):
320
175
"""Template method to provide a user option."""
323
def get_user_option(self, option_name, interpolate=True):
178
def get_user_option(self, option_name):
324
179
"""Get a generic option - no special process, no default."""
325
value = self._get_user_option(option_name)
327
if isinstance(value, list):
328
value = self._interpolate_list(value)
329
elif isinstance(value, dict):
330
trace.warning('Cannot expand "%s":'
331
' Dicts do not support option expansion'
334
value = self._interpolate_string(value)
337
def get_user_option_as_bool(self, option_name):
338
"""Get a generic option as a boolean - no special process, no default.
340
:return None if the option doesn't exist or its value can't be
341
interpreted as a boolean. Returns True or False otherwise.
343
s = self._get_user_option(option_name)
345
# The option doesn't exist
347
val = ui.bool_from_string(s)
349
# The value can't be interpreted as a boolean
350
trace.warning('Value "%s" is not a boolean for "%s"',
354
def get_user_option_as_list(self, option_name):
355
"""Get a generic option as a list - no special process, no default.
357
:return None if the option doesn't exist. Returns the value as a list
360
l = self._get_user_option(option_name)
361
if isinstance(l, (str, unicode)):
362
# A single value, most probably the user forgot (or didn't care to
180
return self._get_user_option(option_name)
367
182
def gpg_signing_command(self):
368
183
"""What program should be used to sign signatures?"""
486
def suppress_warning(self, warning):
487
"""Should the warning be suppressed or emitted.
489
:param warning: The name of the warning being tested.
491
:returns: True if the warning should be suppressed, False otherwise.
493
warnings = self.get_user_option_as_list('suppress_warnings')
494
if warnings is None or warning not in warnings:
499
def get_merge_tools(self):
501
for (oname, value, section, conf_id, parser) in self._get_options():
502
if oname.startswith('bzr.mergetool.'):
503
tool_name = oname[len('bzr.mergetool.'):]
504
tools[tool_name] = value
505
trace.mutter('loaded merge tools: %r' % tools)
508
def find_merge_tool(self, name):
509
# We fake a defaults mechanism here by checking if the given name can
510
# be found in the known_merge_tools if it's not found in the config.
511
# This should be done through the proposed config defaults mechanism
512
# when it becomes available in the future.
513
command_line = (self.get_user_option('bzr.mergetool.%s' % name,
515
or mergetools.known_merge_tools.get(name, None))
519
304
class IniBasedConfig(Config):
520
305
"""A configuration policy that draws from ini files."""
522
def __init__(self, get_filename=symbol_versioning.DEPRECATED_PARAMETER,
524
"""Base class for configuration files using an ini-like syntax.
526
:param file_name: The configuration file path.
528
super(IniBasedConfig, self).__init__()
529
self.file_name = file_name
530
if symbol_versioning.deprecated_passed(get_filename):
531
symbol_versioning.warn(
532
'IniBasedConfig.__init__(get_filename) was deprecated in 2.3.'
533
' Use file_name instead.',
536
if get_filename is not None:
537
self.file_name = get_filename()
539
self.file_name = file_name
544
def from_string(cls, str_or_unicode, file_name=None, save=False):
545
"""Create a config object from a string.
547
:param str_or_unicode: A string representing the file content. This will
550
:param file_name: The configuration file path.
552
:param _save: Whether the file should be saved upon creation.
554
conf = cls(file_name=file_name)
555
conf._create_from_string(str_or_unicode, save)
558
def _create_from_string(self, str_or_unicode, save):
559
self._content = StringIO(str_or_unicode.encode('utf-8'))
560
# Some tests use in-memory configs, some other always need the config
561
# file to exist on disk.
563
self._write_config_file()
565
def _get_parser(self, file=symbol_versioning.DEPRECATED_PARAMETER):
307
def _get_parser(self, file=None):
566
308
if self._parser is not None:
567
309
return self._parser
568
if symbol_versioning.deprecated_passed(file):
569
symbol_versioning.warn(
570
'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
571
' Use IniBasedConfig(_content=xxx) instead.',
574
if self._content is not None:
575
co_input = self._content
576
elif self.file_name is None:
577
raise AssertionError('We have no content to create the config')
311
input = self._get_filename()
579
co_input = self.file_name
581
self._parser = ConfigObj(co_input, encoding='utf-8')
315
self._parser = ConfigObj(input, encoding='utf-8')
582
316
except configobj.ConfigObjError, e:
583
317
raise errors.ParseConfigError(e.errors, e.config.filename)
584
# Make sure self.reload() will use the right file name
585
self._parser.filename = self.file_name
586
318
return self._parser
589
"""Reload the config file from disk."""
590
if self.file_name is None:
591
raise AssertionError('We need a file name to reload the config')
592
if self._parser is not None:
593
self._parser.reload()
595
320
def _get_matching_sections(self):
596
321
"""Return an ordered list of (section_name, extra_path) pairs.
749
427
def _get_nickname(self):
750
428
return self.get_user_option('nickname')
752
def remove_user_option(self, option_name, section_name=None):
753
"""Remove a user option and save the configuration file.
755
:param option_name: The option to be removed.
757
:param section_name: The section the option is defined in, default to
761
parser = self._get_parser()
762
if section_name is None:
765
section = parser[section_name]
767
del section[option_name]
769
raise errors.NoSuchConfigOption(option_name)
770
self._write_config_file()
772
def _write_config_file(self):
773
if self.file_name is None:
774
raise AssertionError('We cannot save, self.file_name is None')
775
conf_dir = os.path.dirname(self.file_name)
776
ensure_config_dir_exists(conf_dir)
777
atomic_file = atomicfile.AtomicFile(self.file_name)
778
self._get_parser().write(atomic_file)
781
osutils.copy_ownership_from_path(self.file_name)
784
class LockableConfig(IniBasedConfig):
785
"""A configuration needing explicit locking for access.
787
If several processes try to write the config file, the accesses need to be
790
Daughter classes should decorate all methods that update a config with the
791
``@needs_write_lock`` decorator (they call, directly or indirectly, the
792
``_write_config_file()`` method. These methods (typically ``set_option()``
793
and variants must reload the config file from disk before calling
794
``_write_config_file()``), this can be achieved by calling the
795
``self.reload()`` method. Note that the lock scope should cover both the
796
reading and the writing of the config file which is why the decorator can't
797
be applied to ``_write_config_file()`` only.
799
This should be enough to implement the following logic:
800
- lock for exclusive write access,
801
- reload the config file from disk,
805
This logic guarantees that a writer can update a value without erasing an
806
update made by another writer.
811
def __init__(self, file_name):
812
super(LockableConfig, self).__init__(file_name=file_name)
813
self.dir = osutils.dirname(osutils.safe_unicode(self.file_name))
814
# FIXME: It doesn't matter that we don't provide possible_transports
815
# below since this is currently used only for local config files ;
816
# local transports are not shared. But if/when we start using
817
# LockableConfig for other kind of transports, we will need to reuse
818
# whatever connection is already established -- vila 20100929
819
self.transport = transport.get_transport(self.dir)
820
self._lock = lockdir.LockDir(self.transport, 'lock')
822
def _create_from_string(self, unicode_bytes, save):
823
super(LockableConfig, self)._create_from_string(unicode_bytes, False)
825
# We need to handle the saving here (as opposed to IniBasedConfig)
828
self._write_config_file()
831
def lock_write(self, token=None):
832
"""Takes a write lock in the directory containing the config file.
834
If the directory doesn't exist it is created.
836
ensure_config_dir_exists(self.dir)
837
return self._lock.lock_write(token)
842
def break_lock(self):
843
self._lock.break_lock()
846
def remove_user_option(self, option_name, section_name=None):
847
super(LockableConfig, self).remove_user_option(option_name,
850
def _write_config_file(self):
851
if self._lock is None or not self._lock.is_held:
852
# NB: if the following exception is raised it probably means a
853
# missing @needs_write_lock decorator on one of the callers.
854
raise errors.ObjectNotLocked(self)
855
super(LockableConfig, self)._write_config_file()
858
class GlobalConfig(LockableConfig):
431
class GlobalConfig(IniBasedConfig):
859
432
"""The configuration that should be used for a specific location."""
862
super(GlobalConfig, self).__init__(file_name=config_filename())
868
def from_string(cls, str_or_unicode, save=False):
869
"""Create a config object from a string.
871
:param str_or_unicode: A string representing the file content. This
872
will be utf-8 encoded.
874
:param save: Whether the file should be saved upon creation.
877
conf._create_from_string(str_or_unicode, save)
880
434
def get_editor(self):
881
435
return self._get_user_option('editor')
438
super(GlobalConfig, self).__init__(config_filename)
884
440
def set_user_option(self, option, value):
885
441
"""Save option and its value in the configuration."""
886
self._set_option(option, value, 'DEFAULT')
888
def get_aliases(self):
889
"""Return the aliases section."""
890
if 'ALIASES' in self._get_parser():
891
return self._get_parser()['ALIASES']
896
def set_alias(self, alias_name, alias_command):
897
"""Save the alias in the configuration."""
898
self._set_option(alias_name, alias_command, 'ALIASES')
901
def unset_alias(self, alias_name):
902
"""Unset an existing alias."""
904
aliases = self._get_parser().get('ALIASES')
905
if not aliases or alias_name not in aliases:
906
raise errors.NoSuchAlias(alias_name)
907
del aliases[alias_name]
908
self._write_config_file()
910
def _set_option(self, option, value, section):
912
self._get_parser().setdefault(section, {})[option] = value
913
self._write_config_file()
916
def _get_sections(self, name=None):
917
"""See IniBasedConfig._get_sections()."""
918
parser = self._get_parser()
919
# We don't give access to options defined outside of any section, we
920
# used the DEFAULT section by... default.
921
if name in (None, 'DEFAULT'):
922
# This could happen for an empty file where the DEFAULT section
923
# doesn't exist yet. So we force DEFAULT when yielding
925
if 'DEFAULT' not in parser:
926
parser['DEFAULT']= {}
927
yield (name, parser[name], self.config_id())
930
def remove_user_option(self, option_name, section_name=None):
931
if section_name is None:
932
# We need to force the default section.
933
section_name = 'DEFAULT'
934
# We need to avoid the LockableConfig implementation or we'll lock
936
super(LockableConfig, self).remove_user_option(option_name,
940
class LocationConfig(LockableConfig):
442
# FIXME: RBC 20051029 This should refresh the parser and also take a
443
# file lock on bazaar.conf.
444
conf_dir = os.path.dirname(self._get_filename())
445
ensure_config_dir_exists(conf_dir)
446
if 'DEFAULT' not in self._get_parser():
447
self._get_parser()['DEFAULT'] = {}
448
self._get_parser()['DEFAULT'][option] = value
449
f = open(self._get_filename(), 'wb')
450
self._get_parser().write(f)
454
class LocationConfig(IniBasedConfig):
941
455
"""A configuration object that gives the policy for a location."""
943
457
def __init__(self, location):
944
super(LocationConfig, self).__init__(
945
file_name=locations_config_filename())
458
name_generator = locations_config_filename
459
if (not os.path.exists(name_generator()) and
460
os.path.exists(branches_config_filename())):
461
if sys.platform == 'win32':
462
trace.warning('Please rename %s to %s'
463
% (branches_config_filename(),
464
locations_config_filename()))
466
trace.warning('Please rename ~/.bazaar/branches.conf'
467
' to ~/.bazaar/locations.conf')
468
name_generator = branches_config_filename
469
super(LocationConfig, self).__init__(name_generator)
946
470
# local file locations are looked up by local path, rather than
947
471
# by file url. This is because the config file is a user
948
472
# file, and we would rather not expose the user to file urls.
1566
1051
if a_user is None:
1567
1052
# Can't find a user
1569
# Prepare a credentials dictionary with additional keys
1570
# for the credential providers
1571
1054
credentials = dict(name=auth_def_name,
1578
password=auth_def.get('password', None),
1055
user=a_user, password=auth_def['password'],
1579
1056
verify_certificates=a_verify_certificates)
1580
# Decode the password in the credentials (or get one)
1581
1057
self.decode_password(credentials,
1582
1058
auth_def.get('password_encoding', None))
1583
1059
if 'auth' in debug.debug_flags:
1584
1060
trace.mutter("Using authentication section: %r", auth_def_name)
1587
if credentials is None:
1588
# No credentials were found in authentication.conf, try the fallback
1589
# credentials stores.
1590
credentials = credential_store_registry.get_fallback_credentials(
1591
scheme, host, port, user, path, realm)
1593
1063
return credentials
1595
def set_credentials(self, name, host, user, scheme=None, password=None,
1596
port=None, path=None, verify_certificates=None,
1598
"""Set authentication credentials for a host.
1600
Any existing credentials with matching scheme, host, port and path
1601
will be deleted, regardless of name.
1603
:param name: An arbitrary name to describe this set of credentials.
1604
:param host: Name of the host that accepts these credentials.
1605
:param user: The username portion of these credentials.
1606
:param scheme: The URL scheme (e.g. ssh, http) the credentials apply
1608
:param password: Password portion of these credentials.
1609
:param port: The IP port on the host that these credentials apply to.
1610
:param path: A filesystem path on the host that these credentials
1612
:param verify_certificates: On https, verify server certificates if
1614
:param realm: The http authentication realm (optional).
1616
values = {'host': host, 'user': user}
1617
if password is not None:
1618
values['password'] = password
1619
if scheme is not None:
1620
values['scheme'] = scheme
1621
if port is not None:
1622
values['port'] = '%d' % port
1623
if path is not None:
1624
values['path'] = path
1625
if verify_certificates is not None:
1626
values['verify_certificates'] = str(verify_certificates)
1627
if realm is not None:
1628
values['realm'] = realm
1629
config = self._get_config()
1631
for section, existing_values in config.items():
1632
for key in ('scheme', 'host', 'port', 'path', 'realm'):
1633
if existing_values.get(key) != values.get(key):
1637
config.update({name: values})
1640
def get_user(self, scheme, host, port=None, realm=None, path=None,
1641
prompt=None, ask=False, default=None):
1065
def get_user(self, scheme, host, port=None,
1066
realm=None, path=None, prompt=None):
1642
1067
"""Get a user from authentication file.
1644
1069
:param scheme: protocol
1723
1124
return password
1725
1126
def decode_password(self, credentials, encoding):
1727
cs = credential_store_registry.get_credential_store(encoding)
1729
raise ValueError('%r is not a known password_encoding' % encoding)
1730
credentials['password'] = cs.decode_password(credentials)
1734
class CredentialStoreRegistry(registry.Registry):
1735
"""A class that registers credential stores.
1737
A credential store provides access to credentials via the password_encoding
1738
field in authentication.conf sections.
1740
Except for stores provided by bzr itself, most stores are expected to be
1741
provided by plugins that will therefore use
1742
register_lazy(password_encoding, module_name, member_name, help=help,
1743
fallback=fallback) to install themselves.
1745
A fallback credential store is one that is queried if no credentials can be
1746
found via authentication.conf.
1749
def get_credential_store(self, encoding=None):
1750
cs = self.get(encoding)
1755
def is_fallback(self, name):
1756
"""Check if the named credentials store should be used as fallback."""
1757
return self.get_info(name)
1759
def get_fallback_credentials(self, scheme, host, port=None, user=None,
1760
path=None, realm=None):
1761
"""Request credentials from all fallback credentials stores.
1763
The first credentials store that can provide credentials wins.
1766
for name in self.keys():
1767
if not self.is_fallback(name):
1769
cs = self.get_credential_store(name)
1770
credentials = cs.get_credentials(scheme, host, port, user,
1772
if credentials is not None:
1773
# We found some credentials
1777
def register(self, key, obj, help=None, override_existing=False,
1779
"""Register a new object to a name.
1781
:param key: This is the key to use to request the object later.
1782
:param obj: The object to register.
1783
:param help: Help text for this entry. This may be a string or
1784
a callable. If it is a callable, it should take two
1785
parameters (registry, key): this registry and the key that
1786
the help was registered under.
1787
:param override_existing: Raise KeyErorr if False and something has
1788
already been registered for that key. If True, ignore if there
1789
is an existing key (always register the new value).
1790
:param fallback: Whether this credential store should be
1793
return super(CredentialStoreRegistry,
1794
self).register(key, obj, help, info=fallback,
1795
override_existing=override_existing)
1797
def register_lazy(self, key, module_name, member_name,
1798
help=None, override_existing=False,
1800
"""Register a new credential store to be loaded on request.
1802
:param module_name: The python path to the module. Such as 'os.path'.
1803
:param member_name: The member of the module to return. If empty or
1804
None, get() will return the module itself.
1805
:param help: Help text for this entry. This may be a string or
1807
:param override_existing: If True, replace the existing object
1808
with the new one. If False, if there is already something
1809
registered with the same key, raise a KeyError
1810
:param fallback: Whether this credential store should be
1813
return super(CredentialStoreRegistry, self).register_lazy(
1814
key, module_name, member_name, help,
1815
info=fallback, override_existing=override_existing)
1818
credential_store_registry = CredentialStoreRegistry()
1821
class CredentialStore(object):
1822
"""An abstract class to implement storage for credentials"""
1824
def decode_password(self, credentials):
1825
"""Returns a clear text password for the provided credentials."""
1826
raise NotImplementedError(self.decode_password)
1828
def get_credentials(self, scheme, host, port=None, user=None, path=None,
1830
"""Return the matching credentials from this credential store.
1832
This method is only called on fallback credential stores.
1834
raise NotImplementedError(self.get_credentials)
1838
class PlainTextCredentialStore(CredentialStore):
1839
__doc__ = """Plain text credential store for the authentication.conf file"""
1841
def decode_password(self, credentials):
1842
"""See CredentialStore.decode_password."""
1843
return credentials['password']
1846
credential_store_registry.register('plain', PlainTextCredentialStore,
1847
help=PlainTextCredentialStore.__doc__)
1848
credential_store_registry.default_key = 'plain'
1851
class BzrDirConfig(object):
1853
def __init__(self, bzrdir):
1854
self._bzrdir = bzrdir
1855
self._config = bzrdir._get_config()
1857
def set_default_stack_on(self, value):
1858
"""Set the default stacking location.
1860
It may be set to a location, or None.
1862
This policy affects all branches contained by this bzrdir, except for
1863
those under repositories.
1865
if self._config is None:
1866
raise errors.BzrError("Cannot set configuration in %s" % self._bzrdir)
1868
self._config.set_option('', 'default_stack_on')
1870
self._config.set_option(value, 'default_stack_on')
1872
def get_default_stack_on(self):
1873
"""Return the default stacking location.
1875
This will either be a location, or None.
1877
This policy affects all branches contained by this bzrdir, except for
1878
those under repositories.
1880
if self._config is None:
1882
value = self._config.get_option('default_stack_on')
1888
class TransportConfig(object):
1889
"""A Config that reads/writes a config file on a Transport.
1891
It is a low-level object that considers config data to be name/value pairs
1892
that may be associated with a section. Assigning meaning to these values
1893
is done at higher levels like TreeConfig.
1896
def __init__(self, transport, filename):
1897
self._transport = transport
1898
self._filename = filename
1900
def get_option(self, name, section=None, default=None):
1901
"""Return the value associated with a named option.
1903
:param name: The name of the value
1904
:param section: The section the option is in (if any)
1905
:param default: The value to return if the value is not set
1906
:return: The value or default value
1908
configobj = self._get_configobj()
1910
section_obj = configobj
1913
section_obj = configobj[section]
1916
return section_obj.get(name, default)
1918
def set_option(self, value, name, section=None):
1919
"""Set the value associated with a named option.
1921
:param value: The value to set
1922
:param name: The name of the value to set
1923
:param section: The section the option is in (if any)
1925
configobj = self._get_configobj()
1927
configobj[name] = value
1929
configobj.setdefault(section, {})[name] = value
1930
self._set_configobj(configobj)
1932
def remove_option(self, option_name, section_name=None):
1933
configobj = self._get_configobj()
1934
if section_name is None:
1935
del configobj[option_name]
1937
del configobj[section_name][option_name]
1938
self._set_configobj(configobj)
1940
def _get_config_file(self):
1942
return StringIO(self._transport.get_bytes(self._filename))
1943
except errors.NoSuchFile:
1946
def _get_configobj(self):
1947
f = self._get_config_file()
1949
return ConfigObj(f, encoding='utf-8')
1953
def _set_configobj(self, configobj):
1954
out_file = StringIO()
1955
configobj.write(out_file)
1957
self._transport.put_file(self._filename, out_file)
1960
class cmd_config(commands.Command):
1961
__doc__ = """Display, set or remove a configuration option.
1963
Display the active value for a given option.
1965
If --all is specified, NAME is interpreted as a regular expression and all
1966
matching options are displayed mentioning their scope. The active value
1967
that bzr will take into account is the first one displayed for each option.
1969
If no NAME is given, --all .* is implied.
1971
Setting a value is achieved by using name=value without spaces. The value
1972
is set in the most relevant scope and can be checked by displaying the
1976
takes_args = ['name?']
1980
# FIXME: This should be a registry option so that plugins can register
1981
# their own config files (or not) -- vila 20101002
1982
commands.Option('scope', help='Reduce the scope to the specified'
1983
' configuration file',
1985
commands.Option('all',
1986
help='Display all the defined values for the matching options.',
1988
commands.Option('remove', help='Remove the option from'
1989
' the configuration file'),
1992
@commands.display_command
1993
def run(self, name=None, all=False, directory=None, scope=None,
1995
if directory is None:
1997
directory = urlutils.normalize_url(directory)
1999
raise errors.BzrError(
2000
'--all and --remove are mutually exclusive.')
2002
# Delete the option in the given scope
2003
self._remove_config_option(name, directory, scope)
2005
# Defaults to all options
2006
self._show_matching_options('.*', directory, scope)
2009
name, value = name.split('=', 1)
2011
# Display the option(s) value(s)
2013
self._show_matching_options(name, directory, scope)
2015
self._show_value(name, directory, scope)
2018
raise errors.BzrError(
2019
'Only one option can be set.')
2020
# Set the option value
2021
self._set_config_option(name, value, directory, scope)
2023
def _get_configs(self, directory, scope=None):
2024
"""Iterate the configurations specified by ``directory`` and ``scope``.
2026
:param directory: Where the configurations are derived from.
2028
:param scope: A specific config to start from.
2030
if scope is not None:
2031
if scope == 'bazaar':
2032
yield GlobalConfig()
2033
elif scope == 'locations':
2034
yield LocationConfig(directory)
2035
elif scope == 'branch':
2036
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
2038
yield br.get_config()
2041
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
2043
yield br.get_config()
2044
except errors.NotBranchError:
2045
yield LocationConfig(directory)
2046
yield GlobalConfig()
2048
def _show_value(self, name, directory, scope):
2050
for c in self._get_configs(directory, scope):
2053
for (oname, value, section, conf_id, parser) in c._get_options():
2055
# Display only the first value and exit
2057
# FIXME: We need to use get_user_option to take policies
2058
# into account and we need to make sure the option exists
2059
# too (hence the two for loops), this needs a better API
2061
value = c.get_user_option(name)
2062
# Quote the value appropriately
2063
value = parser._quote(value)
2064
self.outf.write('%s\n' % (value,))
2068
raise errors.NoSuchConfigOption(name)
2070
def _show_matching_options(self, name, directory, scope):
2071
name = re.compile(name)
2072
# We want any error in the regexp to be raised *now* so we need to
2073
# avoid the delay introduced by the lazy regexp.
2074
name._compile_and_collapse()
2077
for c in self._get_configs(directory, scope):
2078
for (oname, value, section, conf_id, parser) in c._get_options():
2079
if name.search(oname):
2080
if cur_conf_id != conf_id:
2081
# Explain where the options are defined
2082
self.outf.write('%s:\n' % (conf_id,))
2083
cur_conf_id = conf_id
2085
if (section not in (None, 'DEFAULT')
2086
and cur_section != section):
2087
# Display the section if it's not the default (or only)
2089
self.outf.write(' [%s]\n' % (section,))
2090
cur_section = section
2091
self.outf.write(' %s = %s\n' % (oname, value))
2093
def _set_config_option(self, name, value, directory, scope):
2094
for conf in self._get_configs(directory, scope):
2095
conf.set_user_option(name, value)
2098
raise errors.NoSuchConfig(scope)
2100
def _remove_config_option(self, name, directory, scope):
2102
raise errors.BzrCommandError(
2103
'--remove expects an option to remove.')
2105
for conf in self._get_configs(directory, scope):
2106
for (section_name, section, conf_id) in conf._get_sections():
2107
if scope is not None and conf_id != scope:
2108
# Not the right configuration file
2111
if conf_id != conf.config_id():
2112
conf = self._get_configs(directory, conf_id).next()
2113
# We use the first section in the first config where the
2114
# option is defined to remove it
2115
conf.remove_user_option(name, section_name)
2120
raise errors.NoSuchConfig(scope)
2122
raise errors.NoSuchConfigOption(name)