907
400
def _get_nickname(self):
908
401
return self.get_user_option('nickname')
910
def remove_user_option(self, option_name, section_name=None):
911
"""Remove a user option and save the configuration file.
913
:param option_name: The option to be removed.
915
:param section_name: The section the option is defined in, default to
919
parser = self._get_parser()
920
if section_name is None:
923
section = parser[section_name]
925
del section[option_name]
927
raise errors.NoSuchConfigOption(option_name)
928
self._write_config_file()
929
for hook in OldConfigHooks['remove']:
930
hook(self, option_name)
932
def _write_config_file(self):
933
if self.file_name is None:
934
raise AssertionError('We cannot save, self.file_name is None')
935
conf_dir = os.path.dirname(self.file_name)
936
ensure_config_dir_exists(conf_dir)
937
atomic_file = atomicfile.AtomicFile(self.file_name)
938
self._get_parser().write(atomic_file)
941
osutils.copy_ownership_from_path(self.file_name)
942
for hook in OldConfigHooks['save']:
946
class LockableConfig(IniBasedConfig):
947
"""A configuration needing explicit locking for access.
949
If several processes try to write the config file, the accesses need to be
952
Daughter classes should decorate all methods that update a config with the
953
``@needs_write_lock`` decorator (they call, directly or indirectly, the
954
``_write_config_file()`` method. These methods (typically ``set_option()``
955
and variants must reload the config file from disk before calling
956
``_write_config_file()``), this can be achieved by calling the
957
``self.reload()`` method. Note that the lock scope should cover both the
958
reading and the writing of the config file which is why the decorator can't
959
be applied to ``_write_config_file()`` only.
961
This should be enough to implement the following logic:
962
- lock for exclusive write access,
963
- reload the config file from disk,
967
This logic guarantees that a writer can update a value without erasing an
968
update made by another writer.
973
def __init__(self, file_name):
974
super(LockableConfig, self).__init__(file_name=file_name)
975
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
self.transport = transport.get_transport(self.dir)
982
self._lock = lockdir.LockDir(self.transport, self.lock_name)
984
def _create_from_string(self, unicode_bytes, save):
985
super(LockableConfig, self)._create_from_string(unicode_bytes, False)
987
# We need to handle the saving here (as opposed to IniBasedConfig)
990
self._write_config_file()
993
def lock_write(self, token=None):
994
"""Takes a write lock in the directory containing the config file.
996
If the directory doesn't exist it is created.
998
ensure_config_dir_exists(self.dir)
999
return self._lock.lock_write(token)
1004
def break_lock(self):
1005
self._lock.break_lock()
1008
def remove_user_option(self, option_name, section_name=None):
1009
super(LockableConfig, self).remove_user_option(option_name,
1012
def _write_config_file(self):
1013
if self._lock is None or not self._lock.is_held:
1014
# NB: if the following exception is raised it probably means a
1015
# missing @needs_write_lock decorator on one of the callers.
1016
raise errors.ObjectNotLocked(self)
1017
super(LockableConfig, self)._write_config_file()
1020
class GlobalConfig(LockableConfig):
404
class GlobalConfig(IniBasedConfig):
1021
405
"""The configuration that should be used for a specific location."""
1024
super(GlobalConfig, self).__init__(file_name=config_filename())
1026
def config_id(self):
1030
def from_string(cls, str_or_unicode, save=False):
1031
"""Create a config object from a string.
1033
:param str_or_unicode: A string representing the file content. This
1034
will be utf-8 encoded.
1036
:param save: Whether the file should be saved upon creation.
1039
conf._create_from_string(str_or_unicode, save)
1042
@deprecated_method(deprecated_in((2, 4, 0)))
1043
407
def get_editor(self):
1044
408
return self._get_user_option('editor')
411
super(GlobalConfig, self).__init__(config_filename)
1047
413
def set_user_option(self, option, value):
1048
414
"""Save option and its value in the configuration."""
1049
self._set_option(option, value, 'DEFAULT')
1051
def get_aliases(self):
1052
"""Return the aliases section."""
1053
if 'ALIASES' in self._get_parser():
1054
return self._get_parser()['ALIASES']
1059
def set_alias(self, alias_name, alias_command):
1060
"""Save the alias in the configuration."""
1061
self._set_option(alias_name, alias_command, 'ALIASES')
1064
def unset_alias(self, alias_name):
1065
"""Unset an existing alias."""
1067
aliases = self._get_parser().get('ALIASES')
1068
if not aliases or alias_name not in aliases:
1069
raise errors.NoSuchAlias(alias_name)
1070
del aliases[alias_name]
1071
self._write_config_file()
1073
def _set_option(self, option, value, section):
1075
self._get_parser().setdefault(section, {})[option] = value
1076
self._write_config_file()
1077
for hook in OldConfigHooks['set']:
1078
hook(self, option, value)
1080
def _get_sections(self, name=None):
1081
"""See IniBasedConfig._get_sections()."""
1082
parser = self._get_parser()
1083
# We don't give access to options defined outside of any section, we
1084
# used the DEFAULT section by... default.
1085
if name in (None, 'DEFAULT'):
1086
# This could happen for an empty file where the DEFAULT section
1087
# doesn't exist yet. So we force DEFAULT when yielding
1089
if 'DEFAULT' not in parser:
1090
parser['DEFAULT']= {}
1091
yield (name, parser[name], self.config_id())
1094
def remove_user_option(self, option_name, section_name=None):
1095
if section_name is None:
1096
# We need to force the default section.
1097
section_name = 'DEFAULT'
1098
# We need to avoid the LockableConfig implementation or we'll lock
1100
super(LockableConfig, self).remove_user_option(option_name,
1103
def _iter_for_location_by_parts(sections, location):
1104
"""Keep only the sessions matching the specified location.
1106
:param sections: An iterable of section names.
1108
:param location: An url or a local path to match against.
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
1115
``location`` will always be a local path and never a 'file://' url but the
1116
section names themselves can be in either form.
1118
location_parts = location.rstrip('/').split('/')
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.
1124
# This also avoids having file:///path be a more exact
1125
# match than '/path'.
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
1130
if section.startswith('file://'):
1131
section_path = urlutils.local_path_from_url(section)
1133
section_path = section
1134
section_parts = section_path.rstrip('/').split('/')
1137
if len(section_parts) > len(location_parts):
1138
# More path components in the section, they can't match
1141
# Rely on zip truncating in length to the length of the shortest
1142
# argument sequence.
1143
names = zip(location_parts, section_parts)
1145
if not fnmatch.fnmatch(name[0], name[1]):
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)
1155
class LocationConfig(LockableConfig):
415
# FIXME: RBC 20051029 This should refresh the parser and also take a
416
# file lock on bazaar.conf.
417
conf_dir = os.path.dirname(self._get_filename())
418
ensure_config_dir_exists(conf_dir)
419
if 'DEFAULT' not in self._get_parser():
420
self._get_parser()['DEFAULT'] = {}
421
self._get_parser()['DEFAULT'][option] = value
422
f = open(self._get_filename(), 'wb')
423
self._get_parser().write(f)
427
class LocationConfig(IniBasedConfig):
1156
428
"""A configuration object that gives the policy for a location."""
1158
430
def __init__(self, location):
1159
super(LocationConfig, self).__init__(
1160
file_name=locations_config_filename())
431
name_generator = locations_config_filename
432
if (not os.path.exists(name_generator()) and
433
os.path.exists(branches_config_filename())):
434
if sys.platform == 'win32':
435
warning('Please rename %s to %s'
436
% (branches_config_filename(),
437
locations_config_filename()))
439
warning('Please rename ~/.bazaar/branches.conf'
440
' to ~/.bazaar/locations.conf')
441
name_generator = branches_config_filename
442
super(LocationConfig, self).__init__(name_generator)
1161
443
# local file locations are looked up by local path, rather than
1162
444
# by file url. This is because the config file is a user
1163
445
# file, and we would rather not expose the user to file urls.
1530
746
return osutils.pathjoin(config_dir(), 'bazaar.conf')
749
def branches_config_filename():
750
"""Return per-user configuration ini file filename."""
751
return osutils.pathjoin(config_dir(), 'branches.conf')
1533
754
def locations_config_filename():
1534
755
"""Return per-user configuration ini file filename."""
1535
756
return osutils.pathjoin(config_dir(), 'locations.conf')
1538
def authentication_config_filename():
1539
"""Return per-user authentication ini file filename."""
1540
return osutils.pathjoin(config_dir(), 'authentication.conf')
1543
759
def user_ignore_config_filename():
1544
760
"""Return the user default ignore filename"""
1545
761
return osutils.pathjoin(config_dir(), 'ignore')
1549
"""Return the directory name to store crash files.
1551
This doesn't implicitly create it.
1553
On Windows it's in the config directory; elsewhere it's /var/crash
1554
which may be monitored by apport. It can be overridden by
1557
if sys.platform == 'win32':
1558
return osutils.pathjoin(config_dir(), 'Crash')
1560
# XXX: hardcoded in apport_python_hook.py; therefore here too -- mbp
1562
return os.environ.get('APPORT_CRASH_DIR', '/var/crash')
1565
def xdg_cache_dir():
1566
# See http://standards.freedesktop.org/basedir-spec/latest/ar01s03.html
1567
# Possibly this should be different on Windows?
1568
e = os.environ.get('XDG_CACHE_DIR', None)
1572
return os.path.expanduser('~/.cache')
1575
def _get_default_mail_domain():
1576
"""If possible, return the assumed default email domain.
1578
:returns: string mail domain, or None.
1580
if sys.platform == 'win32':
1581
# No implementation yet; patches welcome
1584
f = open('/etc/mailname')
1585
except (IOError, OSError), e:
1588
domain = f.read().strip()
1594
764
def _auto_user_id():
1595
765
"""Calculate automatic user identification.
1597
:returns: (realname, email), either of which may be None if they can't be
767
Returns (realname, email).
1600
769
Only used when none is set in the environment or the id file.
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.
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.
771
This previously used the FQDN as the default domain, but that can
772
be very slow on machines where DNS is broken. So now we simply
1609
777
if sys.platform == 'win32':
1610
# No implementation to reliably determine Windows default mail
1611
# address; please add one.
1614
default_mail_domain = _get_default_mail_domain()
1615
if not default_mail_domain:
778
name = win32utils.get_user_name_unicode()
780
raise errors.BzrError("Cannot autodetect user name.\n"
781
"Please, set your name with command like:\n"
782
'bzr whoami "Your Name <name@domain.com>"')
783
host = win32utils.get_host_name_unicode()
785
host = socket.gethostname()
786
return name, (name + '@' + host)
1621
791
w = pwd.getpwuid(uid)
1623
trace.mutter('no passwd entry for uid %d?' % uid)
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.)
1631
gecos = w.pw_gecos.decode('utf-8')
1633
except UnicodeError:
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)
1641
username = w.pw_name.decode(encoding)
1642
except UnicodeError, e:
1643
trace.mutter("cannot decode passwd entry %s" % w)
1646
comma = gecos.find(',')
1650
realname = gecos[:comma]
1652
return realname, (username + '@' + default_mail_domain)
1655
def parse_username(username):
1656
"""Parse e-mail username and return a (name, address) tuple."""
1657
match = re.match(r'(.*?)\s*<?([\w+.-]+@[\w+.-]+)>?', username)
1659
return (username, '')
1661
return (match.group(1), match.group(2))
793
# we try utf-8 first, because on many variants (like Linux),
794
# /etc/passwd "should" be in utf-8, and because it's unlikely to give
795
# false positives. (many users will have their user encoding set to
796
# latin-1, which cannot raise UnicodeError.)
798
gecos = w.pw_gecos.decode('utf-8')
802
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
803
encoding = bzrlib.user_encoding
805
raise errors.BzrCommandError('Unable to determine your name. '
806
'Use "bzr whoami" to set it.')
808
username = w.pw_name.decode(encoding)
810
raise errors.BzrCommandError('Unable to determine your name. '
811
'Use "bzr whoami" to set it.')
813
comma = gecos.find(',')
817
realname = gecos[:comma]
824
realname = username = getpass.getuser().decode(bzrlib.user_encoding)
825
except UnicodeDecodeError:
826
raise errors.BzrError("Can't decode username as %s." % \
827
bzrlib.user_encoding)
829
return realname, (username + '@' + socket.gethostname())
1664
832
def extract_email_address(e):
1665
833
"""Return just the address part of an email string.
1667
That is just the user@domain part, nothing else.
835
That is just the user@domain part, nothing else.
1668
836
This part is required to contain only ascii characters.
1669
837
If it can't be extracted, raises an error.
1671
839
>>> extract_email_address('Jane Tester <jane@test.com>')
1674
name, email = parse_username(e)
842
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
1676
844
raise errors.NoEmailInUsername(e)
1680
848
class TreeConfig(IniBasedConfig):
1681
849
"""Branch configuration data associated with its contents, not location"""
1683
# XXX: Really needs a better name, as this is not part of the tree! -- mbp 20080507
1685
850
def __init__(self, branch):
1686
self._config = branch._get_config()
1687
851
self.branch = branch
1689
853
def _get_parser(self, file=None):
1690
854
if file is not None:
1691
855
return IniBasedConfig._get_parser(file)
1692
return self._config._get_configobj()
856
return self._get_config()
858
def _get_config(self):
860
obj = ConfigObj(self.branch.control_files.get('branch.conf'),
862
except errors.NoSuchFile:
863
obj = ConfigObj(encoding='utf=8')
1694
866
def get_option(self, name, section=None, default=None):
1695
867
self.branch.lock_read()
1697
return self._config.get_option(name, section, default)
869
obj = self._get_config()
871
if section is not None:
1699
877
self.branch.unlock()
1701
880
def set_option(self, value, name, section=None):
1702
881
"""Set a per-branch configuration option"""
1703
# FIXME: We shouldn't need to lock explicitly here but rather rely on
1704
# higher levels providing the right lock -- vila 20101004
1705
self.branch.lock_write()
1707
self._config.set_option(value, name, section)
1709
self.branch.unlock()
1711
def remove_option(self, option_name, section_name=None):
1712
# FIXME: We shouldn't need to lock explicitly here but rather rely on
1713
# higher levels providing the right lock -- vila 20101004
1714
self.branch.lock_write()
1716
self._config.remove_option(option_name, section_name)
1718
self.branch.unlock()
1721
class AuthenticationConfig(object):
1722
"""The authentication configuration file based on a ini file.
1724
Implements the authentication.conf file described in
1725
doc/developers/authentication-ring.txt.
1728
def __init__(self, _file=None):
1729
self._config = None # The ConfigObj
1731
self._filename = authentication_config_filename()
1732
self._input = self._filename = authentication_config_filename()
1734
# Tests can provide a string as _file
1735
self._filename = None
1738
def _get_config(self):
1739
if self._config is not None:
1742
# FIXME: Should we validate something here ? Includes: empty
1743
# sections are useless, at least one of
1744
# user/password/password_encoding should be defined, etc.
1746
# Note: the encoding below declares that the file itself is utf-8
1747
# encoded, but the values in the ConfigObj are always Unicode.
1748
self._config = ConfigObj(self._input, encoding='utf-8')
1749
except configobj.ConfigObjError, e:
1750
raise errors.ParseConfigError(e.errors, e.config.filename)
1751
except UnicodeError:
1752
raise errors.ConfigContentError(self._filename)
1756
"""Save the config file, only tests should use it for now."""
1757
conf_dir = os.path.dirname(self._filename)
1758
ensure_config_dir_exists(conf_dir)
1759
f = file(self._filename, 'wb')
1761
self._get_config().write(f)
1765
def _set_option(self, section_name, option_name, value):
1766
"""Set an authentication configuration option"""
1767
conf = self._get_config()
1768
section = conf.get(section_name)
1771
section = conf[section]
1772
section[option_name] = value
1775
def get_credentials(self, scheme, host, port=None, user=None, path=None,
1777
"""Returns the matching credentials from authentication.conf file.
1779
:param scheme: protocol
1781
:param host: the server address
1783
:param port: the associated port (optional)
1785
:param user: login (optional)
1787
:param path: the absolute path on the server (optional)
1789
:param realm: the http authentication realm (optional)
1791
:return: A dict containing the matching credentials or None.
1793
- name: the section name of the credentials in the
1794
authentication.conf file,
1795
- user: can't be different from the provided user if any,
1796
- scheme: the server protocol,
1797
- host: the server address,
1798
- port: the server port (can be None),
1799
- path: the absolute server path (can be None),
1800
- realm: the http specific authentication realm (can be None),
1801
- password: the decoded password, could be None if the credential
1802
defines only the user
1803
- verify_certificates: https specific, True if the server
1804
certificate should be verified, False otherwise.
1807
for auth_def_name, auth_def in self._get_config().items():
1808
if type(auth_def) is not configobj.Section:
1809
raise ValueError("%s defined outside a section" % auth_def_name)
1811
a_scheme, a_host, a_user, a_path = map(
1812
auth_def.get, ['scheme', 'host', 'user', 'path'])
1815
a_port = auth_def.as_int('port')
1819
raise ValueError("'port' not numeric in %s" % auth_def_name)
1821
a_verify_certificates = auth_def.as_bool('verify_certificates')
1823
a_verify_certificates = True
1826
"'verify_certificates' not boolean in %s" % auth_def_name)
1829
if a_scheme is not None and scheme != a_scheme:
1831
if a_host is not None:
1832
if not (host == a_host
1833
or (a_host.startswith('.') and host.endswith(a_host))):
1835
if a_port is not None and port != a_port:
1837
if (a_path is not None and path is not None
1838
and not path.startswith(a_path)):
1840
if (a_user is not None and user is not None
1841
and a_user != user):
1842
# Never contradict the caller about the user to be used
1847
# Prepare a credentials dictionary with additional keys
1848
# for the credential providers
1849
credentials = dict(name=auth_def_name,
1856
password=auth_def.get('password', None),
1857
verify_certificates=a_verify_certificates)
1858
# Decode the password in the credentials (or get one)
1859
self.decode_password(credentials,
1860
auth_def.get('password_encoding', None))
1861
if 'auth' in debug.debug_flags:
1862
trace.mutter("Using authentication section: %r", auth_def_name)
1865
if credentials is None:
1866
# No credentials were found in authentication.conf, try the fallback
1867
# credentials stores.
1868
credentials = credential_store_registry.get_fallback_credentials(
1869
scheme, host, port, user, path, realm)
1873
def set_credentials(self, name, host, user, scheme=None, password=None,
1874
port=None, path=None, verify_certificates=None,
1876
"""Set authentication credentials for a host.
1878
Any existing credentials with matching scheme, host, port and path
1879
will be deleted, regardless of name.
1881
:param name: An arbitrary name to describe this set of credentials.
1882
:param host: Name of the host that accepts these credentials.
1883
:param user: The username portion of these credentials.
1884
:param scheme: The URL scheme (e.g. ssh, http) the credentials apply
1886
:param password: Password portion of these credentials.
1887
:param port: The IP port on the host that these credentials apply to.
1888
:param path: A filesystem path on the host that these credentials
1890
:param verify_certificates: On https, verify server certificates if
1892
:param realm: The http authentication realm (optional).
1894
values = {'host': host, 'user': user}
1895
if password is not None:
1896
values['password'] = password
1897
if scheme is not None:
1898
values['scheme'] = scheme
1899
if port is not None:
1900
values['port'] = '%d' % port
1901
if path is not None:
1902
values['path'] = path
1903
if verify_certificates is not None:
1904
values['verify_certificates'] = str(verify_certificates)
1905
if realm is not None:
1906
values['realm'] = realm
1907
config = self._get_config()
1909
for section, existing_values in config.items():
1910
for key in ('scheme', 'host', 'port', 'path', 'realm'):
1911
if existing_values.get(key) != values.get(key):
1915
config.update({name: values})
1918
def get_user(self, scheme, host, port=None, realm=None, path=None,
1919
prompt=None, ask=False, default=None):
1920
"""Get a user from authentication file.
1922
:param scheme: protocol
1924
:param host: the server address
1926
:param port: the associated port (optional)
1928
:param realm: the realm sent by the server (optional)
1930
:param path: the absolute path on the server (optional)
1932
:param ask: Ask the user if there is no explicitly configured username
1935
:param default: The username returned if none is defined (optional).
1937
:return: The found user.
1939
credentials = self.get_credentials(scheme, host, port, user=None,
1940
path=path, realm=realm)
1941
if credentials is not None:
1942
user = credentials['user']
1948
# Create a default prompt suitable for most cases
1949
prompt = u'%s' % (scheme.upper(),) + u' %(host)s username'
1950
# Special handling for optional fields in the prompt
1951
if port is not None:
1952
prompt_host = '%s:%d' % (host, port)
1955
user = ui.ui_factory.get_username(prompt, host=prompt_host)
1960
def get_password(self, scheme, host, user, port=None,
1961
realm=None, path=None, prompt=None):
1962
"""Get a password from authentication file or prompt the user for one.
1964
:param scheme: protocol
1966
:param host: the server address
1968
:param port: the associated port (optional)
1972
:param realm: the realm sent by the server (optional)
1974
:param path: the absolute path on the server (optional)
1976
:return: The found password or the one entered by the user.
1978
credentials = self.get_credentials(scheme, host, port, user, path,
1980
if credentials is not None:
1981
password = credentials['password']
1982
if password is not None and scheme is 'ssh':
1983
trace.warning('password ignored in section [%s],'
1984
' use an ssh agent instead'
1985
% credentials['name'])
1989
# Prompt user only if we could't find a password
1990
if password is None:
1992
# Create a default prompt suitable for most cases
1993
prompt = u'%s' % scheme.upper() + u' %(user)s@%(host)s password'
1994
# Special handling for optional fields in the prompt
1995
if port is not None:
1996
prompt_host = '%s:%d' % (host, port)
1999
password = ui.ui_factory.get_password(prompt,
2000
host=prompt_host, user=user)
2003
def decode_password(self, credentials, encoding):
2005
cs = credential_store_registry.get_credential_store(encoding)
2007
raise ValueError('%r is not a known password_encoding' % encoding)
2008
credentials['password'] = cs.decode_password(credentials)
2012
class CredentialStoreRegistry(registry.Registry):
2013
"""A class that registers credential stores.
2015
A credential store provides access to credentials via the password_encoding
2016
field in authentication.conf sections.
2018
Except for stores provided by bzr itself, most stores are expected to be
2019
provided by plugins that will therefore use
2020
register_lazy(password_encoding, module_name, member_name, help=help,
2021
fallback=fallback) to install themselves.
2023
A fallback credential store is one that is queried if no credentials can be
2024
found via authentication.conf.
2027
def get_credential_store(self, encoding=None):
2028
cs = self.get(encoding)
2033
def is_fallback(self, name):
2034
"""Check if the named credentials store should be used as fallback."""
2035
return self.get_info(name)
2037
def get_fallback_credentials(self, scheme, host, port=None, user=None,
2038
path=None, realm=None):
2039
"""Request credentials from all fallback credentials stores.
2041
The first credentials store that can provide credentials wins.
2044
for name in self.keys():
2045
if not self.is_fallback(name):
2047
cs = self.get_credential_store(name)
2048
credentials = cs.get_credentials(scheme, host, port, user,
2050
if credentials is not None:
2051
# We found some credentials
2055
def register(self, key, obj, help=None, override_existing=False,
2057
"""Register a new object to a name.
2059
:param key: This is the key to use to request the object later.
2060
:param obj: The object to register.
2061
:param help: Help text for this entry. This may be a string or
2062
a callable. If it is a callable, it should take two
2063
parameters (registry, key): this registry and the key that
2064
the help was registered under.
2065
:param override_existing: Raise KeyErorr if False and something has
2066
already been registered for that key. If True, ignore if there
2067
is an existing key (always register the new value).
2068
:param fallback: Whether this credential store should be
2071
return super(CredentialStoreRegistry,
2072
self).register(key, obj, help, info=fallback,
2073
override_existing=override_existing)
2075
def register_lazy(self, key, module_name, member_name,
2076
help=None, override_existing=False,
2078
"""Register a new credential store to be loaded on request.
2080
:param module_name: The python path to the module. Such as 'os.path'.
2081
:param member_name: The member of the module to return. If empty or
2082
None, get() will return the module itself.
2083
:param help: Help text for this entry. This may be a string or
2085
:param override_existing: If True, replace the existing object
2086
with the new one. If False, if there is already something
2087
registered with the same key, raise a KeyError
2088
:param fallback: Whether this credential store should be
2091
return super(CredentialStoreRegistry, self).register_lazy(
2092
key, module_name, member_name, help,
2093
info=fallback, override_existing=override_existing)
2096
credential_store_registry = CredentialStoreRegistry()
2099
class CredentialStore(object):
2100
"""An abstract class to implement storage for credentials"""
2102
def decode_password(self, credentials):
2103
"""Returns a clear text password for the provided credentials."""
2104
raise NotImplementedError(self.decode_password)
2106
def get_credentials(self, scheme, host, port=None, user=None, path=None,
2108
"""Return the matching credentials from this credential store.
2110
This method is only called on fallback credential stores.
2112
raise NotImplementedError(self.get_credentials)
2116
class PlainTextCredentialStore(CredentialStore):
2117
__doc__ = """Plain text credential store for the authentication.conf file"""
2119
def decode_password(self, credentials):
2120
"""See CredentialStore.decode_password."""
2121
return credentials['password']
2124
credential_store_registry.register('plain', PlainTextCredentialStore,
2125
help=PlainTextCredentialStore.__doc__)
2126
credential_store_registry.default_key = 'plain'
2129
class BzrDirConfig(object):
2131
def __init__(self, bzrdir):
2132
self._bzrdir = bzrdir
2133
self._config = bzrdir._get_config()
2135
def set_default_stack_on(self, value):
2136
"""Set the default stacking location.
2138
It may be set to a location, or None.
2140
This policy affects all branches contained by this bzrdir, except for
2141
those under repositories.
2143
if self._config is None:
2144
raise errors.BzrError("Cannot set configuration in %s" % self._bzrdir)
2146
self._config.set_option('', 'default_stack_on')
2148
self._config.set_option(value, 'default_stack_on')
2150
def get_default_stack_on(self):
2151
"""Return the default stacking location.
2153
This will either be a location, or None.
2155
This policy affects all branches contained by this bzrdir, except for
2156
those under repositories.
2158
if self._config is None:
2160
value = self._config.get_option('default_stack_on')
2166
class TransportConfig(object):
2167
"""A Config that reads/writes a config file on a Transport.
2169
It is a low-level object that considers config data to be name/value pairs
2170
that may be associated with a section. Assigning meaning to these values
2171
is done at higher levels like TreeConfig.
2174
def __init__(self, transport, filename):
2175
self._transport = transport
2176
self._filename = filename
2178
def get_option(self, name, section=None, default=None):
2179
"""Return the value associated with a named option.
2181
:param name: The name of the value
2182
:param section: The section the option is in (if any)
2183
:param default: The value to return if the value is not set
2184
:return: The value or default value
2186
configobj = self._get_configobj()
2188
section_obj = configobj
2191
section_obj = configobj[section]
2194
value = section_obj.get(name, default)
2195
for hook in OldConfigHooks['get']:
2196
hook(self, name, value)
2199
def set_option(self, value, name, section=None):
2200
"""Set the value associated with a named option.
2202
:param value: The value to set
2203
:param name: The name of the value to set
2204
:param section: The section the option is in (if any)
2206
configobj = self._get_configobj()
2208
configobj[name] = value
2210
configobj.setdefault(section, {})[name] = value
2211
for hook in OldConfigHooks['set']:
2212
hook(self, name, value)
2213
self._set_configobj(configobj)
2215
def remove_option(self, option_name, section_name=None):
2216
configobj = self._get_configobj()
2217
if section_name is None:
2218
del configobj[option_name]
2220
del configobj[section_name][option_name]
2221
for hook in OldConfigHooks['remove']:
2222
hook(self, option_name)
2223
self._set_configobj(configobj)
2225
def _get_config_file(self):
2227
f = StringIO(self._transport.get_bytes(self._filename))
2228
for hook in OldConfigHooks['load']:
2231
except errors.NoSuchFile:
2234
def _external_url(self):
2235
return urlutils.join(self._transport.external_url(), self._filename)
2237
def _get_configobj(self):
2238
f = self._get_config_file()
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())
2250
def _set_configobj(self, configobj):
2251
out_file = StringIO()
2252
configobj.write(out_file)
2254
self._transport.put_file(self._filename, out_file)
2255
for hook in OldConfigHooks['save']:
2259
class Option(object):
2260
"""An option definition.
2262
The option *values* are stored in config files and found in sections.
2264
Here we define various properties about the option itself, its default
2265
value, in which config files it can be stored, etc (TBC).
2268
def __init__(self, name, default=None):
2270
self.default = default
2272
def get_default(self):
2278
option_registry = registry.Registry()
2281
option_registry.register(
2282
'editor', Option('editor'),
2283
help='The command called to launch an editor to enter a message.')
2286
class Section(object):
2287
"""A section defines a dict of option name => value.
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
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
2299
def get(self, name, default=None):
2300
return self.options.get(name, default)
2303
# Mostly for debugging use
2304
return "<config.%s id=%s>" % (self.__class__.__name__, self.id)
2307
_NewlyCreatedOption = object()
2308
"""Was the option created during the MutableSection lifetime"""
2311
class MutableSection(Section):
2312
"""A section allowing changes and keeping track of the original values."""
2314
def __init__(self, section_id, options):
2315
super(MutableSection, self).__init__(section_id, options)
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
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]
2332
class Store(object):
2333
"""Abstract interface to persistent storage for configuration options."""
2335
readonly_section_class = Section
2336
mutable_section_class = MutableSection
2338
def is_loaded(self):
2339
"""Returns True if the Store has been loaded.
2341
This is used to implement lazy loading and ensure the persistent
2342
storage is queried only when needed.
2344
raise NotImplementedError(self.is_loaded)
2347
"""Loads the Store from persistent storage."""
2348
raise NotImplementedError(self.load)
2350
def _load_from_string(self, bytes):
2351
"""Create a store from a string in configobj syntax.
2353
:param bytes: A string representing the file content.
2355
raise NotImplementedError(self._load_from_string)
2358
"""Unloads the Store.
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
2364
raise NotImplementedError(self.unload)
2367
"""Saves the Store to persistent storage."""
2368
raise NotImplementedError(self.save)
2370
def external_url(self):
2371
raise NotImplementedError(self.external_url)
2373
def get_sections(self):
2374
"""Returns an ordered iterable of existing sections.
2376
:returns: An iterable of (name, dict).
2378
raise NotImplementedError(self.get_sections)
2380
def get_mutable_section(self, section_name=None):
2381
"""Returns the specified mutable section.
2383
:param section_name: The section identifier
2385
raise NotImplementedError(self.get_mutable_section)
2388
# Mostly for debugging use
2389
return "<config.%s(%s)>" % (self.__class__.__name__,
2390
self.external_url())
2393
class IniFileStore(Store):
2394
"""A config Store using ConfigObj for storage.
2396
:ivar transport: The transport object where the config file is located.
2398
:ivar file_name: The config file basename in the transport directory.
2400
:ivar _config_obj: Private member to hold the ConfigObj instance used to
2401
serialize/deserialize the config file.
2404
def __init__(self, transport, file_name):
2405
"""A config Store using ConfigObj for storage.
2407
:param transport: The transport object where the config file is located.
2409
:param file_name: The config file basename in the transport directory.
2411
super(IniFileStore, self).__init__()
2412
self.transport = transport
2413
self.file_name = file_name
2414
self._config_obj = None
2416
def is_loaded(self):
2417
return self._config_obj != None
2420
self._config_obj = None
2423
"""Load the store from the associated file."""
2424
if self.is_loaded():
2426
content = self.transport.get_bytes(self.file_name)
2427
self._load_from_string(content)
2428
for hook in ConfigHooks['load']:
2431
def _load_from_string(self, bytes):
2432
"""Create a config store from a string.
2434
:param bytes: A string representing the file content.
2436
if self.is_loaded():
2437
raise AssertionError('Already loaded: %r' % (self._config_obj,))
2438
co_input = StringIO(bytes)
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())
2449
if not self.is_loaded():
2453
self._config_obj.write(out)
2454
self.transport.put_bytes(self.file_name, out.getvalue())
2455
for hook in ConfigHooks['save']:
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)
2466
def get_sections(self):
2467
"""Get the configobj section in the file order.
2469
:returns: An iterable of (name, dict).
2471
# We need a loaded store
2474
except errors.NoSuchFile:
2475
# If the file doesn't exist, there is no sections
2477
cobj = self._config_obj
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])
2483
def get_mutable_section(self, section_name=None):
2484
# We need a loaded store
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
2493
section = self._config_obj.setdefault(section_name, {})
2494
return self.mutable_section_class(section_name, section)
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.
2503
class LockableIniFileStore(IniFileStore):
2504
"""A ConfigObjStore using locks on save to ensure store integrity."""
2506
def __init__(self, transport, file_name, lock_dir_name=None):
2507
"""A config Store using ConfigObj for storage.
2509
:param transport: The transport object where the config file is located.
2511
:param file_name: The config file basename in the transport directory.
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)
2519
def lock_write(self, token=None):
2520
"""Takes a write lock in the directory containing the config file.
2522
If the directory doesn't exist it is created.
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)
2533
def break_lock(self):
2534
self._lock.break_lock()
2538
# We need to be able to override the undecorated implementation
2539
self.save_without_locking()
2541
def save_without_locking(self):
2542
super(LockableIniFileStore, self).save()
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
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):
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')
2561
class LocationStore(LockableIniFileStore):
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')
2569
class BranchStore(IniFileStore):
2571
def __init__(self, branch):
2572
super(BranchStore, self).__init__(branch.control_transport,
2574
self.branch = branch
2576
def lock_write(self, token=None):
2577
return self.branch.lock_write(token)
2580
return self.branch.unlock()
2584
# We need to be able to override the undecorated implementation
2585
self.save_without_locking()
2587
def save_without_locking(self):
2588
super(BranchStore, self).save()
2591
class SectionMatcher(object):
2592
"""Select sections into a given Store.
2594
This intended to be used to postpone getting an iterable of sections from a
2598
def __init__(self, store):
2601
def get_sections(self):
2602
# This is where we require loading the store so we can see all defined
2604
sections = self.store.get_sections()
2605
# Walk the revisions in the order provided
2610
def match(self, secion):
2611
raise NotImplementedError(self.match)
2614
class LocationSection(Section):
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
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)
2631
class LocationMatcher(SectionMatcher):
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
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
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
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
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
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),
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)
2685
# Finally, we have a valid section
2689
class Stack(object):
2690
"""A stack of configurations where an option can be defined"""
2692
def __init__(self, sections_def, store=None, mutable_section_name=None):
2693
"""Creates a stack of sections with an optional store for changes.
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.
2699
:param store: The optional Store where modifications will be
2700
recorded. If none is specified, no modifications can be done.
2702
:param mutable_section_name: The name of the MutableSection where
2703
changes are recorded. This requires the ``store`` parameter to be
2706
self.sections_def = sections_def
2708
self.mutable_section_name = mutable_section_name
2710
def get(self, name):
2711
"""Return the *first* option value found in the sections.
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).
2719
# FIXME: No caching of options nor sections yet -- vila 20110503
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
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()
2730
sections = [section_or_callable]
2731
for section in sections:
2732
value = section.get(name)
2733
if value is not None:
2735
if value is not None:
2738
# If the option is registered, it may provide a default value
2740
opt = option_registry.get(name)
2745
value = opt.get_default()
2746
for hook in ConfigHooks['get']:
2747
hook(self, name, value)
2750
def _get_mutable_section(self):
2751
"""Get the MutableSection for the Stack.
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.
2758
section = self.store.get_mutable_section(self.mutable_section_name)
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)
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']:
2776
# Mostly for debugging use
2777
return "<config.%s(%s)>" % (self.__class__.__name__, id(self))
2780
class _CompatibleStack(Stack):
2781
"""Place holder for compatibility with previous design.
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.
2786
One assumption made here is that the daughter classes will all use Stores
2787
derived from LockableIniFileStore).
2789
It implements set() by re-loading the store before applying the
2790
modification and saving it.
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.
2796
def set(self, name, value):
2799
super(_CompatibleStack, self).set(name, value)
2800
# Force a write to persistent storage
2804
class GlobalStack(_CompatibleStack):
2808
gstore = GlobalStore()
2809
super(GlobalStack, self).__init__([gstore.get_sections], gstore)
2812
class LocationStack(_CompatibleStack):
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)
2821
class BranchStack(_CompatibleStack):
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],
2831
self.branch = branch
2834
class cmd_config(commands.Command):
2835
__doc__ = """Display, set or remove a configuration option.
2837
Display the active value for a given option.
2839
If --all is specified, NAME is interpreted as a regular expression and all
2840
matching options are displayed mentioning their scope. The active value
2841
that bzr will take into account is the first one displayed for each option.
2843
If no NAME is given, --all .* is implied.
2845
Setting a value is achieved by using name=value without spaces. The value
2846
is set in the most relevant scope and can be checked by displaying the
2850
takes_args = ['name?']
2854
# FIXME: This should be a registry option so that plugins can register
2855
# their own config files (or not) -- vila 20101002
2856
commands.Option('scope', help='Reduce the scope to the specified'
2857
' configuration file',
2859
commands.Option('all',
2860
help='Display all the defined values for the matching options.',
2862
commands.Option('remove', help='Remove the option from'
2863
' the configuration file'),
2866
_see_also = ['configuration']
2868
@commands.display_command
2869
def run(self, name=None, all=False, directory=None, scope=None,
2871
if directory is None:
2873
directory = urlutils.normalize_url(directory)
2875
raise errors.BzrError(
2876
'--all and --remove are mutually exclusive.')
2878
# Delete the option in the given scope
2879
self._remove_config_option(name, directory, scope)
2881
# Defaults to all options
2882
self._show_matching_options('.*', directory, scope)
2885
name, value = name.split('=', 1)
2887
# Display the option(s) value(s)
2889
self._show_matching_options(name, directory, scope)
2891
self._show_value(name, directory, scope)
2894
raise errors.BzrError(
2895
'Only one option can be set.')
2896
# Set the option value
2897
self._set_config_option(name, value, directory, scope)
2899
def _get_configs(self, directory, scope=None):
2900
"""Iterate the configurations specified by ``directory`` and ``scope``.
2902
:param directory: Where the configurations are derived from.
2904
:param scope: A specific config to start from.
2906
if scope is not None:
2907
if scope == 'bazaar':
2908
yield GlobalConfig()
2909
elif scope == 'locations':
2910
yield LocationConfig(directory)
2911
elif scope == 'branch':
2912
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
2914
yield br.get_config()
2917
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
2919
yield br.get_config()
2920
except errors.NotBranchError:
2921
yield LocationConfig(directory)
2922
yield GlobalConfig()
2924
def _show_value(self, name, directory, scope):
2926
for c in self._get_configs(directory, scope):
2929
for (oname, value, section, conf_id, parser) in c._get_options():
2931
# Display only the first value and exit
2933
# FIXME: We need to use get_user_option to take policies
2934
# into account and we need to make sure the option exists
2935
# too (hence the two for loops), this needs a better API
2937
value = c.get_user_option(name)
2938
# Quote the value appropriately
2939
value = parser._quote(value)
2940
self.outf.write('%s\n' % (value,))
2944
raise errors.NoSuchConfigOption(name)
2946
def _show_matching_options(self, name, directory, scope):
2947
name = lazy_regex.lazy_compile(name)
2948
# 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.
2951
name._compile_and_collapse()
2954
for c in self._get_configs(directory, scope):
2955
for (oname, value, section, conf_id, parser) in c._get_options():
2956
if name.search(oname):
2957
if cur_conf_id != conf_id:
2958
# Explain where the options are defined
2959
self.outf.write('%s:\n' % (conf_id,))
2960
cur_conf_id = conf_id
2962
if (section not in (None, 'DEFAULT')
2963
and cur_section != section):
2964
# Display the section if it's not the default (or only)
2966
self.outf.write(' [%s]\n' % (section,))
2967
cur_section = section
2968
self.outf.write(' %s = %s\n' % (oname, value))
2970
def _set_config_option(self, name, value, directory, scope):
2971
for conf in self._get_configs(directory, scope):
2972
conf.set_user_option(name, value)
2975
raise errors.NoSuchConfig(scope)
2977
def _remove_config_option(self, name, directory, scope):
2979
raise errors.BzrCommandError(
2980
'--remove expects an option to remove.')
2982
for conf in self._get_configs(directory, scope):
2983
for (section_name, section, conf_id) in conf._get_sections():
2984
if scope is not None and conf_id != scope:
2985
# Not the right configuration file
2988
if conf_id != conf.config_id():
2989
conf = self._get_configs(directory, conf_id).next()
2990
# We use the first section in the first config where the
2991
# option is defined to remove it
2992
conf.remove_user_option(name, section_name)
2997
raise errors.NoSuchConfig(scope)
2999
raise errors.NoSuchConfigOption(name)
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.
3011
# The registered object should be a callable receiving a test instance
3012
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Store
3014
test_store_builder_registry = registry.Registry()
3016
# The registered object should be a callable receiving a test instance
3017
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Stack
3019
test_stack_builder_registry = registry.Registry()
882
self.branch.lock_write()
884
cfg_obj = self._get_config()
889
obj = cfg_obj[section]
891
cfg_obj[section] = {}
892
obj = cfg_obj[section]
894
out_file = StringIO()
895
cfg_obj.write(out_file)
897
self.branch.control_files.put('branch.conf', out_file)