1538
729
return osutils.pathjoin(config_dir(), 'bazaar.conf')
732
def branches_config_filename():
733
"""Return per-user configuration ini file filename."""
734
return osutils.pathjoin(config_dir(), 'branches.conf')
1541
737
def locations_config_filename():
1542
738
"""Return per-user configuration ini file filename."""
1543
739
return osutils.pathjoin(config_dir(), 'locations.conf')
1546
def authentication_config_filename():
1547
"""Return per-user authentication ini file filename."""
1548
return osutils.pathjoin(config_dir(), 'authentication.conf')
1551
742
def user_ignore_config_filename():
1552
743
"""Return the user default ignore filename"""
1553
744
return osutils.pathjoin(config_dir(), 'ignore')
1557
"""Return the directory name to store crash files.
1559
This doesn't implicitly create it.
1561
On Windows it's in the config directory; elsewhere it's /var/crash
1562
which may be monitored by apport. It can be overridden by
1565
if sys.platform == 'win32':
1566
return osutils.pathjoin(config_dir(), 'Crash')
1568
# XXX: hardcoded in apport_python_hook.py; therefore here too -- mbp
1570
return os.environ.get('APPORT_CRASH_DIR', '/var/crash')
1573
def xdg_cache_dir():
1574
# See http://standards.freedesktop.org/basedir-spec/latest/ar01s03.html
1575
# Possibly this should be different on Windows?
1576
e = os.environ.get('XDG_CACHE_DIR', None)
1580
return os.path.expanduser('~/.cache')
1583
def _get_default_mail_domain():
1584
"""If possible, return the assumed default email domain.
1586
:returns: string mail domain, or None.
1588
if sys.platform == 'win32':
1589
# No implementation yet; patches welcome
1592
f = open('/etc/mailname')
1593
except (IOError, OSError), e:
1596
domain = f.read().strip()
1602
747
def _auto_user_id():
1603
748
"""Calculate automatic user identification.
1605
:returns: (realname, email), either of which may be None if they can't be
750
Returns (realname, email).
1608
752
Only used when none is set in the environment or the id file.
1610
This only returns an email address if we can be fairly sure the
1611
address is reasonable, ie if /etc/mailname is set on unix.
1613
This doesn't use the FQDN as the default domain because that may be
1614
slow, and it doesn't use the hostname alone because that's not normally
1615
a reasonable address.
754
This previously used the FQDN as the default domain, but that can
755
be very slow on machines where DNS is broken. So now we simply
1617
760
if sys.platform == 'win32':
1618
# No implementation to reliably determine Windows default mail
1619
# address; please add one.
1622
default_mail_domain = _get_default_mail_domain()
1623
if not default_mail_domain:
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)
1629
774
w = pwd.getpwuid(uid)
1631
trace.mutter('no passwd entry for uid %d?' % uid)
1634
# we try utf-8 first, because on many variants (like Linux),
1635
# /etc/passwd "should" be in utf-8, and because it's unlikely to give
1636
# false positives. (many users will have their user encoding set to
1637
# latin-1, which cannot raise UnicodeError.)
1639
gecos = w.pw_gecos.decode('utf-8')
1641
except UnicodeError:
1643
encoding = osutils.get_user_encoding()
1644
gecos = w.pw_gecos.decode(encoding)
1645
except UnicodeError, e:
1646
trace.mutter("cannot decode passwd entry %s" % w)
1649
username = w.pw_name.decode(encoding)
1650
except UnicodeError, e:
1651
trace.mutter("cannot decode passwd entry %s" % w)
1654
comma = gecos.find(',')
1658
realname = gecos[:comma]
1660
return realname, (username + '@' + default_mail_domain)
1663
def parse_username(username):
1664
"""Parse e-mail username and return a (name, address) tuple."""
1665
match = re.match(r'(.*?)\s*<?([\w+.-]+@[\w+.-]+)>?', username)
1667
return (username, '')
1669
return (match.group(1), match.group(2))
776
# we try utf-8 first, because on many variants (like Linux),
777
# /etc/passwd "should" be in utf-8, and because it's unlikely to give
778
# false positives. (many users will have their user encoding set to
779
# latin-1, which cannot raise UnicodeError.)
781
gecos = w.pw_gecos.decode('utf-8')
785
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
786
encoding = bzrlib.user_encoding
788
raise errors.BzrCommandError('Unable to determine your name. '
789
'Use "bzr whoami" to set it.')
791
username = w.pw_name.decode(encoding)
793
raise errors.BzrCommandError('Unable to determine your name. '
794
'Use "bzr whoami" to set it.')
796
comma = gecos.find(',')
800
realname = gecos[:comma]
807
realname = username = getpass.getuser().decode(bzrlib.user_encoding)
808
except UnicodeDecodeError:
809
raise errors.BzrError("Can't decode username as %s." % \
810
bzrlib.user_encoding)
812
return realname, (username + '@' + socket.gethostname())
1672
815
def extract_email_address(e):
1673
816
"""Return just the address part of an email string.
1675
That is just the user@domain part, nothing else.
818
That is just the user@domain part, nothing else.
1676
819
This part is required to contain only ascii characters.
1677
820
If it can't be extracted, raises an error.
1679
822
>>> extract_email_address('Jane Tester <jane@test.com>')
1682
name, email = parse_username(e)
825
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
1684
827
raise errors.NoEmailInUsername(e)
1688
831
class TreeConfig(IniBasedConfig):
1689
832
"""Branch configuration data associated with its contents, not location"""
1691
# XXX: Really needs a better name, as this is not part of the tree! -- mbp 20080507
1693
833
def __init__(self, branch):
1694
self._config = branch._get_config()
1695
834
self.branch = branch
1697
836
def _get_parser(self, file=None):
1698
837
if file is not None:
1699
838
return IniBasedConfig._get_parser(file)
1700
return self._config._get_configobj()
839
return self._get_config()
841
def _get_config(self):
843
obj = ConfigObj(self.branch.control_files.get('branch.conf'),
845
except errors.NoSuchFile:
846
obj = ConfigObj(encoding='utf=8')
1702
849
def get_option(self, name, section=None, default=None):
1703
850
self.branch.lock_read()
1705
return self._config.get_option(name, section, default)
852
obj = self._get_config()
854
if section is not None:
1707
860
self.branch.unlock()
1709
863
def set_option(self, value, name, section=None):
1710
864
"""Set a per-branch configuration option"""
1711
# FIXME: We shouldn't need to lock explicitly here but rather rely on
1712
# higher levels providing the right lock -- vila 20101004
1713
self.branch.lock_write()
1715
self._config.set_option(value, name, section)
1717
self.branch.unlock()
1719
def remove_option(self, option_name, section_name=None):
1720
# FIXME: We shouldn't need to lock explicitly here but rather rely on
1721
# higher levels providing the right lock -- vila 20101004
1722
self.branch.lock_write()
1724
self._config.remove_option(option_name, section_name)
1726
self.branch.unlock()
1729
class AuthenticationConfig(object):
1730
"""The authentication configuration file based on a ini file.
1732
Implements the authentication.conf file described in
1733
doc/developers/authentication-ring.txt.
1736
def __init__(self, _file=None):
1737
self._config = None # The ConfigObj
1739
self._filename = authentication_config_filename()
1740
self._input = self._filename = authentication_config_filename()
1742
# Tests can provide a string as _file
1743
self._filename = None
1746
def _get_config(self):
1747
if self._config is not None:
1750
# FIXME: Should we validate something here ? Includes: empty
1751
# sections are useless, at least one of
1752
# user/password/password_encoding should be defined, etc.
1754
# Note: the encoding below declares that the file itself is utf-8
1755
# encoded, but the values in the ConfigObj are always Unicode.
1756
self._config = ConfigObj(self._input, encoding='utf-8')
1757
except configobj.ConfigObjError, e:
1758
raise errors.ParseConfigError(e.errors, e.config.filename)
1759
except UnicodeError:
1760
raise errors.ConfigContentError(self._filename)
1764
"""Save the config file, only tests should use it for now."""
1765
conf_dir = os.path.dirname(self._filename)
1766
ensure_config_dir_exists(conf_dir)
1767
f = file(self._filename, 'wb')
1769
self._get_config().write(f)
1773
def _set_option(self, section_name, option_name, value):
1774
"""Set an authentication configuration option"""
1775
conf = self._get_config()
1776
section = conf.get(section_name)
1779
section = conf[section]
1780
section[option_name] = value
1783
def get_credentials(self, scheme, host, port=None, user=None, path=None,
1785
"""Returns the matching credentials from authentication.conf file.
1787
:param scheme: protocol
1789
:param host: the server address
1791
:param port: the associated port (optional)
1793
:param user: login (optional)
1795
:param path: the absolute path on the server (optional)
1797
:param realm: the http authentication realm (optional)
1799
:return: A dict containing the matching credentials or None.
1801
- name: the section name of the credentials in the
1802
authentication.conf file,
1803
- user: can't be different from the provided user if any,
1804
- scheme: the server protocol,
1805
- host: the server address,
1806
- port: the server port (can be None),
1807
- path: the absolute server path (can be None),
1808
- realm: the http specific authentication realm (can be None),
1809
- password: the decoded password, could be None if the credential
1810
defines only the user
1811
- verify_certificates: https specific, True if the server
1812
certificate should be verified, False otherwise.
1815
for auth_def_name, auth_def in self._get_config().items():
1816
if type(auth_def) is not configobj.Section:
1817
raise ValueError("%s defined outside a section" % auth_def_name)
1819
a_scheme, a_host, a_user, a_path = map(
1820
auth_def.get, ['scheme', 'host', 'user', 'path'])
1823
a_port = auth_def.as_int('port')
1827
raise ValueError("'port' not numeric in %s" % auth_def_name)
1829
a_verify_certificates = auth_def.as_bool('verify_certificates')
1831
a_verify_certificates = True
1834
"'verify_certificates' not boolean in %s" % auth_def_name)
1837
if a_scheme is not None and scheme != a_scheme:
1839
if a_host is not None:
1840
if not (host == a_host
1841
or (a_host.startswith('.') and host.endswith(a_host))):
1843
if a_port is not None and port != a_port:
1845
if (a_path is not None and path is not None
1846
and not path.startswith(a_path)):
1848
if (a_user is not None and user is not None
1849
and a_user != user):
1850
# Never contradict the caller about the user to be used
1855
# Prepare a credentials dictionary with additional keys
1856
# for the credential providers
1857
credentials = dict(name=auth_def_name,
1864
password=auth_def.get('password', None),
1865
verify_certificates=a_verify_certificates)
1866
# Decode the password in the credentials (or get one)
1867
self.decode_password(credentials,
1868
auth_def.get('password_encoding', None))
1869
if 'auth' in debug.debug_flags:
1870
trace.mutter("Using authentication section: %r", auth_def_name)
1873
if credentials is None:
1874
# No credentials were found in authentication.conf, try the fallback
1875
# credentials stores.
1876
credentials = credential_store_registry.get_fallback_credentials(
1877
scheme, host, port, user, path, realm)
1881
def set_credentials(self, name, host, user, scheme=None, password=None,
1882
port=None, path=None, verify_certificates=None,
1884
"""Set authentication credentials for a host.
1886
Any existing credentials with matching scheme, host, port and path
1887
will be deleted, regardless of name.
1889
:param name: An arbitrary name to describe this set of credentials.
1890
:param host: Name of the host that accepts these credentials.
1891
:param user: The username portion of these credentials.
1892
:param scheme: The URL scheme (e.g. ssh, http) the credentials apply
1894
:param password: Password portion of these credentials.
1895
:param port: The IP port on the host that these credentials apply to.
1896
:param path: A filesystem path on the host that these credentials
1898
:param verify_certificates: On https, verify server certificates if
1900
:param realm: The http authentication realm (optional).
1902
values = {'host': host, 'user': user}
1903
if password is not None:
1904
values['password'] = password
1905
if scheme is not None:
1906
values['scheme'] = scheme
1907
if port is not None:
1908
values['port'] = '%d' % port
1909
if path is not None:
1910
values['path'] = path
1911
if verify_certificates is not None:
1912
values['verify_certificates'] = str(verify_certificates)
1913
if realm is not None:
1914
values['realm'] = realm
1915
config = self._get_config()
1917
for section, existing_values in config.items():
1918
for key in ('scheme', 'host', 'port', 'path', 'realm'):
1919
if existing_values.get(key) != values.get(key):
1923
config.update({name: values})
1926
def get_user(self, scheme, host, port=None, realm=None, path=None,
1927
prompt=None, ask=False, default=None):
1928
"""Get a user from authentication file.
1930
:param scheme: protocol
1932
:param host: the server address
1934
:param port: the associated port (optional)
1936
:param realm: the realm sent by the server (optional)
1938
:param path: the absolute path on the server (optional)
1940
:param ask: Ask the user if there is no explicitly configured username
1943
:param default: The username returned if none is defined (optional).
1945
:return: The found user.
1947
credentials = self.get_credentials(scheme, host, port, user=None,
1948
path=path, realm=realm)
1949
if credentials is not None:
1950
user = credentials['user']
1956
# Create a default prompt suitable for most cases
1957
prompt = u'%s' % (scheme.upper(),) + u' %(host)s username'
1958
# Special handling for optional fields in the prompt
1959
if port is not None:
1960
prompt_host = '%s:%d' % (host, port)
1963
user = ui.ui_factory.get_username(prompt, host=prompt_host)
1968
def get_password(self, scheme, host, user, port=None,
1969
realm=None, path=None, prompt=None):
1970
"""Get a password from authentication file or prompt the user for one.
1972
:param scheme: protocol
1974
:param host: the server address
1976
:param port: the associated port (optional)
1980
:param realm: the realm sent by the server (optional)
1982
:param path: the absolute path on the server (optional)
1984
:return: The found password or the one entered by the user.
1986
credentials = self.get_credentials(scheme, host, port, user, path,
1988
if credentials is not None:
1989
password = credentials['password']
1990
if password is not None and scheme is 'ssh':
1991
trace.warning('password ignored in section [%s],'
1992
' use an ssh agent instead'
1993
% credentials['name'])
1997
# Prompt user only if we could't find a password
1998
if password is None:
2000
# Create a default prompt suitable for most cases
2001
prompt = u'%s' % scheme.upper() + u' %(user)s@%(host)s password'
2002
# Special handling for optional fields in the prompt
2003
if port is not None:
2004
prompt_host = '%s:%d' % (host, port)
2007
password = ui.ui_factory.get_password(prompt,
2008
host=prompt_host, user=user)
2011
def decode_password(self, credentials, encoding):
2013
cs = credential_store_registry.get_credential_store(encoding)
2015
raise ValueError('%r is not a known password_encoding' % encoding)
2016
credentials['password'] = cs.decode_password(credentials)
2020
class CredentialStoreRegistry(registry.Registry):
2021
"""A class that registers credential stores.
2023
A credential store provides access to credentials via the password_encoding
2024
field in authentication.conf sections.
2026
Except for stores provided by bzr itself, most stores are expected to be
2027
provided by plugins that will therefore use
2028
register_lazy(password_encoding, module_name, member_name, help=help,
2029
fallback=fallback) to install themselves.
2031
A fallback credential store is one that is queried if no credentials can be
2032
found via authentication.conf.
2035
def get_credential_store(self, encoding=None):
2036
cs = self.get(encoding)
2041
def is_fallback(self, name):
2042
"""Check if the named credentials store should be used as fallback."""
2043
return self.get_info(name)
2045
def get_fallback_credentials(self, scheme, host, port=None, user=None,
2046
path=None, realm=None):
2047
"""Request credentials from all fallback credentials stores.
2049
The first credentials store that can provide credentials wins.
2052
for name in self.keys():
2053
if not self.is_fallback(name):
2055
cs = self.get_credential_store(name)
2056
credentials = cs.get_credentials(scheme, host, port, user,
2058
if credentials is not None:
2059
# We found some credentials
2063
def register(self, key, obj, help=None, override_existing=False,
2065
"""Register a new object to a name.
2067
:param key: This is the key to use to request the object later.
2068
:param obj: The object to register.
2069
:param help: Help text for this entry. This may be a string or
2070
a callable. If it is a callable, it should take two
2071
parameters (registry, key): this registry and the key that
2072
the help was registered under.
2073
:param override_existing: Raise KeyErorr if False and something has
2074
already been registered for that key. If True, ignore if there
2075
is an existing key (always register the new value).
2076
:param fallback: Whether this credential store should be
2079
return super(CredentialStoreRegistry,
2080
self).register(key, obj, help, info=fallback,
2081
override_existing=override_existing)
2083
def register_lazy(self, key, module_name, member_name,
2084
help=None, override_existing=False,
2086
"""Register a new credential store to be loaded on request.
2088
:param module_name: The python path to the module. Such as 'os.path'.
2089
:param member_name: The member of the module to return. If empty or
2090
None, get() will return the module itself.
2091
:param help: Help text for this entry. This may be a string or
2093
:param override_existing: If True, replace the existing object
2094
with the new one. If False, if there is already something
2095
registered with the same key, raise a KeyError
2096
:param fallback: Whether this credential store should be
2099
return super(CredentialStoreRegistry, self).register_lazy(
2100
key, module_name, member_name, help,
2101
info=fallback, override_existing=override_existing)
2104
credential_store_registry = CredentialStoreRegistry()
2107
class CredentialStore(object):
2108
"""An abstract class to implement storage for credentials"""
2110
def decode_password(self, credentials):
2111
"""Returns a clear text password for the provided credentials."""
2112
raise NotImplementedError(self.decode_password)
2114
def get_credentials(self, scheme, host, port=None, user=None, path=None,
2116
"""Return the matching credentials from this credential store.
2118
This method is only called on fallback credential stores.
2120
raise NotImplementedError(self.get_credentials)
2124
class PlainTextCredentialStore(CredentialStore):
2125
__doc__ = """Plain text credential store for the authentication.conf file"""
2127
def decode_password(self, credentials):
2128
"""See CredentialStore.decode_password."""
2129
return credentials['password']
2132
credential_store_registry.register('plain', PlainTextCredentialStore,
2133
help=PlainTextCredentialStore.__doc__)
2134
credential_store_registry.default_key = 'plain'
2137
class BzrDirConfig(object):
2139
def __init__(self, bzrdir):
2140
self._bzrdir = bzrdir
2141
self._config = bzrdir._get_config()
2143
def set_default_stack_on(self, value):
2144
"""Set the default stacking location.
2146
It may be set to a location, or None.
2148
This policy affects all branches contained by this bzrdir, except for
2149
those under repositories.
2151
if self._config is None:
2152
raise errors.BzrError("Cannot set configuration in %s" % self._bzrdir)
2154
self._config.set_option('', 'default_stack_on')
2156
self._config.set_option(value, 'default_stack_on')
2158
def get_default_stack_on(self):
2159
"""Return the default stacking location.
2161
This will either be a location, or None.
2163
This policy affects all branches contained by this bzrdir, except for
2164
those under repositories.
2166
if self._config is None:
2168
value = self._config.get_option('default_stack_on')
2174
class TransportConfig(object):
2175
"""A Config that reads/writes a config file on a Transport.
2177
It is a low-level object that considers config data to be name/value pairs
2178
that may be associated with a section. Assigning meaning to these values
2179
is done at higher levels like TreeConfig.
2182
def __init__(self, transport, filename):
2183
self._transport = transport
2184
self._filename = filename
2186
def get_option(self, name, section=None, default=None):
2187
"""Return the value associated with a named option.
2189
:param name: The name of the value
2190
:param section: The section the option is in (if any)
2191
:param default: The value to return if the value is not set
2192
:return: The value or default value
2194
configobj = self._get_configobj()
2196
section_obj = configobj
2199
section_obj = configobj[section]
2202
value = section_obj.get(name, default)
2203
for hook in OldConfigHooks['get']:
2204
hook(self, name, value)
2207
def set_option(self, value, name, section=None):
2208
"""Set the value associated with a named option.
2210
:param value: The value to set
2211
:param name: The name of the value to set
2212
:param section: The section the option is in (if any)
2214
configobj = self._get_configobj()
2216
configobj[name] = value
2218
configobj.setdefault(section, {})[name] = value
2219
for hook in OldConfigHooks['set']:
2220
hook(self, name, value)
2221
self._set_configobj(configobj)
2223
def remove_option(self, option_name, section_name=None):
2224
configobj = self._get_configobj()
2225
if section_name is None:
2226
del configobj[option_name]
2228
del configobj[section_name][option_name]
2229
for hook in OldConfigHooks['remove']:
2230
hook(self, option_name)
2231
self._set_configobj(configobj)
2233
def _get_config_file(self):
2235
f = StringIO(self._transport.get_bytes(self._filename))
2236
for hook in OldConfigHooks['load']:
2239
except errors.NoSuchFile:
2242
def _external_url(self):
2243
return urlutils.join(self._transport.external_url(), self._filename)
2245
def _get_configobj(self):
2246
f = self._get_config_file()
2249
conf = ConfigObj(f, encoding='utf-8')
2250
except configobj.ConfigObjError, e:
2251
raise errors.ParseConfigError(e.errors, self._external_url())
2252
except UnicodeDecodeError:
2253
raise errors.ConfigContentError(self._external_url())
2258
def _set_configobj(self, configobj):
2259
out_file = StringIO()
2260
configobj.write(out_file)
2262
self._transport.put_file(self._filename, out_file)
2263
for hook in OldConfigHooks['save']:
2267
class Option(object):
2268
"""An option definition.
2270
The option *values* are stored in config files and found in sections.
2272
Here we define various properties about the option itself, its default
2273
value, how to convert it from stores, what to do when invalid values are
2274
encoutered, in which config files it can be stored.
2277
def __init__(self, name, default=None, help=None, from_unicode=None,
2279
"""Build an option definition.
2281
:param name: the name used to refer to the option.
2283
:param default: the default value to use when none exist in the config
2286
:param help: a doc string to explain the option to the user.
2288
:param from_unicode: a callable to convert the unicode string
2289
representing the option value in a store. This is not called for
2292
:param invalid: the action to be taken when an invalid value is
2293
encountered in a store. This is called only when from_unicode is
2294
invoked to convert a string and returns None or raise ValueError or
2295
TypeError. Accepted values are: None (ignore invalid values),
2296
'warning' (emit a warning), 'error' (emit an error message and
2300
self.default = default
2302
self.from_unicode = from_unicode
2303
if invalid and invalid not in ('warning', 'error'):
2304
raise AssertionError("%s not supported for 'invalid'" % (invalid,))
2305
self.invalid = invalid
2307
def get_default(self):
2310
def get_help_text(self, additional_see_also=None, plain=True):
2312
from bzrlib import help_topics
2313
result += help_topics._format_see_also(additional_see_also)
2315
result = help_topics.help_as_plain_text(result)
2319
# Predefined converters to get proper values from store
2321
def bool_from_store(unicode_str):
2322
return ui.bool_from_string(unicode_str)
2325
def int_from_store(unicode_str):
2326
return int(unicode_str)
2329
def list_from_store(unicode_str):
2330
# ConfigObj return '' instead of u''. Use 'str' below to catch all cases.
2331
if isinstance(unicode_str, (str, unicode)):
2333
# A single value, most probably the user forgot (or didn't care to
2334
# add) the final ','
2337
# The empty string, convert to empty list
2340
# We rely on ConfigObj providing us with a list already
2345
class OptionRegistry(registry.Registry):
2346
"""Register config options by their name.
2348
This overrides ``registry.Registry`` to simplify registration by acquiring
2349
some information from the option object itself.
2352
def register(self, option):
2353
"""Register a new option to its name.
2355
:param option: The option to register. Its name is used as the key.
2357
super(OptionRegistry, self).register(option.name, option,
2360
def register_lazy(self, key, module_name, member_name):
2361
"""Register a new option to be loaded on request.
2363
:param key: the key to request the option later. Since the registration
2364
is lazy, it should be provided and match the option name.
2366
:param module_name: the python path to the module. Such as 'os.path'.
2368
:param member_name: the member of the module to return. If empty or
2369
None, get() will return the module itself.
2371
super(OptionRegistry, self).register_lazy(key,
2372
module_name, member_name)
2374
def get_help(self, key=None):
2375
"""Get the help text associated with the given key"""
2376
option = self.get(key)
2377
the_help = option.help
2378
if callable(the_help):
2379
return the_help(self, key)
2383
option_registry = OptionRegistry()
2386
# Registered options in lexicographical order
2388
option_registry.register(
2389
Option('bzr.workingtree.worth_saving_limit', default=10,
2390
from_unicode=int_from_store, invalid='warning',
2392
How many changes before saving the dirstate.
2394
-1 means that we will never rewrite the dirstate file for only
2395
stat-cache changes. Regardless of this setting, we will always rewrite
2396
the dirstate file if a file is added/removed/renamed/etc. This flag only
2397
affects the behavior of updating the dirstate file after we notice that
2398
a file has been touched.
2400
option_registry.register(
2401
Option('dirstate.fdatasync', default=True,
2402
from_unicode=bool_from_store,
2404
Flush dirstate changes onto physical disk?
2406
If true (default), working tree metadata changes are flushed through the
2407
OS buffers to physical disk. This is somewhat slower, but means data
2408
should not be lost if the machine crashes. See also repository.fdatasync.
2410
option_registry.register(
2411
Option('debug_flags', default=[], from_unicode=list_from_store,
2412
help='Debug flags to activate.'))
2413
option_registry.register(
2414
Option('default_format', default='2a',
2415
help='Format used when creating branches.'))
2416
option_registry.register(
2418
help='The command called to launch an editor to enter a message.'))
2419
option_registry.register(
2420
Option('ignore_missing_extensions', default=False,
2421
from_unicode=bool_from_store,
2423
Control the missing extensions warning display.
2425
The warning will not be emitted if set to True.
2427
option_registry.register(
2429
help='Language to translate messages into.'))
2430
option_registry.register(
2431
Option('locks.steal_dead', default=False, from_unicode=bool_from_store,
2433
Steal locks that appears to be dead.
2435
If set to True, bzr will check if a lock is supposed to be held by an
2436
active process from the same user on the same machine. If the user and
2437
machine match, but no process with the given PID is active, then bzr
2438
will automatically break the stale lock, and create a new lock for
2440
Otherwise, bzr will prompt as normal to break the lock.
2442
option_registry.register(
2443
Option('output_encoding',
2444
help= 'Unicode encoding for output'
2445
' (terminal encoding if not specified).'))
2446
option_registry.register(
2447
Option('repository.fdatasync', default=True, from_unicode=bool_from_store,
2449
Flush repository changes onto physical disk?
2451
If true (default), repository changes are flushed through the OS buffers
2452
to physical disk. This is somewhat slower, but means data should not be
2453
lost if the machine crashes. See also dirstate.fdatasync.
2457
class Section(object):
2458
"""A section defines a dict of option name => value.
2460
This is merely a read-only dict which can add some knowledge about the
2461
options. It is *not* a python dict object though and doesn't try to mimic
2465
def __init__(self, section_id, options):
2466
self.id = section_id
2467
# We re-use the dict-like object received
2468
self.options = options
2470
def get(self, name, default=None):
2471
return self.options.get(name, default)
2474
# Mostly for debugging use
2475
return "<config.%s id=%s>" % (self.__class__.__name__, self.id)
2478
_NewlyCreatedOption = object()
2479
"""Was the option created during the MutableSection lifetime"""
2482
class MutableSection(Section):
2483
"""A section allowing changes and keeping track of the original values."""
2485
def __init__(self, section_id, options):
2486
super(MutableSection, self).__init__(section_id, options)
2489
def set(self, name, value):
2490
if name not in self.options:
2491
# This is a new option
2492
self.orig[name] = _NewlyCreatedOption
2493
elif name not in self.orig:
2494
self.orig[name] = self.get(name, None)
2495
self.options[name] = value
2497
def remove(self, name):
2498
if name not in self.orig:
2499
self.orig[name] = self.get(name, None)
2500
del self.options[name]
2503
class Store(object):
2504
"""Abstract interface to persistent storage for configuration options."""
2506
readonly_section_class = Section
2507
mutable_section_class = MutableSection
2509
def is_loaded(self):
2510
"""Returns True if the Store has been loaded.
2512
This is used to implement lazy loading and ensure the persistent
2513
storage is queried only when needed.
2515
raise NotImplementedError(self.is_loaded)
2518
"""Loads the Store from persistent storage."""
2519
raise NotImplementedError(self.load)
2521
def _load_from_string(self, bytes):
2522
"""Create a store from a string in configobj syntax.
2524
:param bytes: A string representing the file content.
2526
raise NotImplementedError(self._load_from_string)
2529
"""Unloads the Store.
2531
This should make is_loaded() return False. This is used when the caller
2532
knows that the persistent storage has changed or may have change since
2535
raise NotImplementedError(self.unload)
2538
"""Saves the Store to persistent storage."""
2539
raise NotImplementedError(self.save)
2541
def external_url(self):
2542
raise NotImplementedError(self.external_url)
2544
def get_sections(self):
2545
"""Returns an ordered iterable of existing sections.
2547
:returns: An iterable of (name, dict).
2549
raise NotImplementedError(self.get_sections)
2551
def get_mutable_section(self, section_name=None):
2552
"""Returns the specified mutable section.
2554
:param section_name: The section identifier
2556
raise NotImplementedError(self.get_mutable_section)
2559
# Mostly for debugging use
2560
return "<config.%s(%s)>" % (self.__class__.__name__,
2561
self.external_url())
2564
class IniFileStore(Store):
2565
"""A config Store using ConfigObj for storage.
2567
:ivar transport: The transport object where the config file is located.
2569
:ivar file_name: The config file basename in the transport directory.
2571
:ivar _config_obj: Private member to hold the ConfigObj instance used to
2572
serialize/deserialize the config file.
2575
def __init__(self, transport, file_name):
2576
"""A config Store using ConfigObj for storage.
2578
:param transport: The transport object where the config file is located.
2580
:param file_name: The config file basename in the transport directory.
2582
super(IniFileStore, self).__init__()
2583
self.transport = transport
2584
self.file_name = file_name
2585
self._config_obj = None
2587
def is_loaded(self):
2588
return self._config_obj != None
2591
self._config_obj = None
2594
"""Load the store from the associated file."""
2595
if self.is_loaded():
2597
content = self.transport.get_bytes(self.file_name)
2598
self._load_from_string(content)
2599
for hook in ConfigHooks['load']:
2602
def _load_from_string(self, bytes):
2603
"""Create a config store from a string.
2605
:param bytes: A string representing the file content.
2607
if self.is_loaded():
2608
raise AssertionError('Already loaded: %r' % (self._config_obj,))
2609
co_input = StringIO(bytes)
2611
# The config files are always stored utf8-encoded
2612
self._config_obj = ConfigObj(co_input, encoding='utf-8')
2613
except configobj.ConfigObjError, e:
2614
self._config_obj = None
2615
raise errors.ParseConfigError(e.errors, self.external_url())
2616
except UnicodeDecodeError:
2617
raise errors.ConfigContentError(self.external_url())
2620
if not self.is_loaded():
2624
self._config_obj.write(out)
2625
self.transport.put_bytes(self.file_name, out.getvalue())
2626
for hook in ConfigHooks['save']:
2629
def external_url(self):
2630
# FIXME: external_url should really accepts an optional relpath
2631
# parameter (bug #750169) :-/ -- vila 2011-04-04
2632
# The following will do in the interim but maybe we don't want to
2633
# expose a path here but rather a config ID and its associated
2634
# object </hand wawe>.
2635
return urlutils.join(self.transport.external_url(), self.file_name)
2637
def get_sections(self):
2638
"""Get the configobj section in the file order.
2640
:returns: An iterable of (name, dict).
2642
# We need a loaded store
2645
except errors.NoSuchFile:
2646
# If the file doesn't exist, there is no sections
2648
cobj = self._config_obj
2650
yield self.readonly_section_class(None, cobj)
2651
for section_name in cobj.sections:
2652
yield self.readonly_section_class(section_name, cobj[section_name])
2654
def get_mutable_section(self, section_name=None):
2655
# We need a loaded store
2658
except errors.NoSuchFile:
2659
# The file doesn't exist, let's pretend it was empty
2660
self._load_from_string('')
2661
if section_name is None:
2662
section = self._config_obj
2664
section = self._config_obj.setdefault(section_name, {})
2665
return self.mutable_section_class(section_name, section)
2668
# Note that LockableConfigObjStore inherits from ConfigObjStore because we need
2669
# unlockable stores for use with objects that can already ensure the locking
2670
# (think branches). If different stores (not based on ConfigObj) are created,
2671
# they may face the same issue.
2674
class LockableIniFileStore(IniFileStore):
2675
"""A ConfigObjStore using locks on save to ensure store integrity."""
2677
def __init__(self, transport, file_name, lock_dir_name=None):
2678
"""A config Store using ConfigObj for storage.
2680
:param transport: The transport object where the config file is located.
2682
:param file_name: The config file basename in the transport directory.
2684
if lock_dir_name is None:
2685
lock_dir_name = 'lock'
2686
self.lock_dir_name = lock_dir_name
2687
super(LockableIniFileStore, self).__init__(transport, file_name)
2688
self._lock = lockdir.LockDir(self.transport, self.lock_dir_name)
2690
def lock_write(self, token=None):
2691
"""Takes a write lock in the directory containing the config file.
2693
If the directory doesn't exist it is created.
2695
# FIXME: This doesn't check the ownership of the created directories as
2696
# ensure_config_dir_exists does. It should if the transport is local
2697
# -- vila 2011-04-06
2698
self.transport.create_prefix()
2699
return self._lock.lock_write(token)
2704
def break_lock(self):
2705
self._lock.break_lock()
2709
# We need to be able to override the undecorated implementation
2710
self.save_without_locking()
2712
def save_without_locking(self):
2713
super(LockableIniFileStore, self).save()
2716
# FIXME: global, bazaar, shouldn't that be 'user' instead or even
2717
# 'user_defaults' as opposed to 'user_overrides', 'system_defaults'
2718
# (/etc/bzr/bazaar.conf) and 'system_overrides' ? -- vila 2011-04-05
2720
# FIXME: Moreover, we shouldn't need classes for these stores either, factory
2721
# functions or a registry will make it easier and clearer for tests, focusing
2722
# on the relevant parts of the API that needs testing -- vila 20110503 (based
2723
# on a poolie's remark)
2724
class GlobalStore(LockableIniFileStore):
2726
def __init__(self, possible_transports=None):
2727
t = transport.get_transport_from_path(
2728
config_dir(), possible_transports=possible_transports)
2729
super(GlobalStore, self).__init__(t, 'bazaar.conf')
2732
class LocationStore(LockableIniFileStore):
2734
def __init__(self, possible_transports=None):
2735
t = transport.get_transport_from_path(
2736
config_dir(), possible_transports=possible_transports)
2737
super(LocationStore, self).__init__(t, 'locations.conf')
2740
class BranchStore(IniFileStore):
2742
def __init__(self, branch):
2743
super(BranchStore, self).__init__(branch.control_transport,
2745
self.branch = branch
2747
def lock_write(self, token=None):
2748
return self.branch.lock_write(token)
2751
return self.branch.unlock()
2755
# We need to be able to override the undecorated implementation
2756
self.save_without_locking()
2758
def save_without_locking(self):
2759
super(BranchStore, self).save()
2762
class SectionMatcher(object):
2763
"""Select sections into a given Store.
2765
This intended to be used to postpone getting an iterable of sections from a
2769
def __init__(self, store):
2772
def get_sections(self):
2773
# This is where we require loading the store so we can see all defined
2775
sections = self.store.get_sections()
2776
# Walk the revisions in the order provided
2781
def match(self, secion):
2782
raise NotImplementedError(self.match)
2785
class LocationSection(Section):
2787
def __init__(self, section, length, extra_path):
2788
super(LocationSection, self).__init__(section.id, section.options)
2789
self.length = length
2790
self.extra_path = extra_path
2792
def get(self, name, default=None):
2793
value = super(LocationSection, self).get(name, default)
2794
if value is not None:
2795
policy_name = self.get(name + ':policy', None)
2796
policy = _policy_value.get(policy_name, POLICY_NONE)
2797
if policy == POLICY_APPENDPATH:
2798
value = urlutils.join(value, self.extra_path)
2802
class LocationMatcher(SectionMatcher):
2804
def __init__(self, store, location):
2805
super(LocationMatcher, self).__init__(store)
2806
if location.startswith('file://'):
2807
location = urlutils.local_path_from_url(location)
2808
self.location = location
2810
def _get_matching_sections(self):
2811
"""Get all sections matching ``location``."""
2812
# We slightly diverge from LocalConfig here by allowing the no-name
2813
# section as the most generic one and the lower priority.
2814
no_name_section = None
2816
# Filter out the no_name_section so _iter_for_location_by_parts can be
2817
# used (it assumes all sections have a name).
2818
for section in self.store.get_sections():
2819
if section.id is None:
2820
no_name_section = section
2822
sections.append(section)
2823
# Unfortunately _iter_for_location_by_parts deals with section names so
2824
# we have to resync.
2825
filtered_sections = _iter_for_location_by_parts(
2826
[s.id for s in sections], self.location)
2827
iter_sections = iter(sections)
2828
matching_sections = []
2829
if no_name_section is not None:
2830
matching_sections.append(
2831
LocationSection(no_name_section, 0, self.location))
2832
for section_id, extra_path, length in filtered_sections:
2833
# a section id is unique for a given store so it's safe to iterate
2835
section = iter_sections.next()
2836
if section_id == section.id:
2837
matching_sections.append(
2838
LocationSection(section, length, extra_path))
2839
return matching_sections
2841
def get_sections(self):
2842
# Override the default implementation as we want to change the order
2843
matching_sections = self._get_matching_sections()
2844
# We want the longest (aka more specific) locations first
2845
sections = sorted(matching_sections,
2846
key=lambda section: (section.length, section.id),
2848
# Sections mentioning 'ignore_parents' restrict the selection
2849
for section in sections:
2850
# FIXME: We really want to use as_bool below -- vila 2011-04-07
2851
ignore = section.get('ignore_parents', None)
2852
if ignore is not None:
2853
ignore = ui.bool_from_string(ignore)
2856
# Finally, we have a valid section
2860
class Stack(object):
2861
"""A stack of configurations where an option can be defined"""
2863
def __init__(self, sections_def, store=None, mutable_section_name=None):
2864
"""Creates a stack of sections with an optional store for changes.
2866
:param sections_def: A list of Section or callables that returns an
2867
iterable of Section. This defines the Sections for the Stack and
2868
can be called repeatedly if needed.
2870
:param store: The optional Store where modifications will be
2871
recorded. If none is specified, no modifications can be done.
2873
:param mutable_section_name: The name of the MutableSection where
2874
changes are recorded. This requires the ``store`` parameter to be
2877
self.sections_def = sections_def
2879
self.mutable_section_name = mutable_section_name
2881
def get(self, name):
2882
"""Return the *first* option value found in the sections.
2884
This is where we guarantee that sections coming from Store are loaded
2885
lazily: the loading is delayed until we need to either check that an
2886
option exists or get its value, which in turn may require to discover
2887
in which sections it can be defined. Both of these (section and option
2888
existence) require loading the store (even partially).
2890
# FIXME: No caching of options nor sections yet -- vila 20110503
2892
# Ensuring lazy loading is achieved by delaying section matching (which
2893
# implies querying the persistent storage) until it can't be avoided
2894
# anymore by using callables to describe (possibly empty) section
2896
for section_or_callable in self.sections_def:
2897
# Each section can expand to multiple ones when a callable is used
2898
if callable(section_or_callable):
2899
sections = section_or_callable()
2901
sections = [section_or_callable]
2902
for section in sections:
2903
value = section.get(name)
2904
if value is not None:
2906
if value is not None:
2908
# If the option is registered, it may provide additional info about
2911
opt = option_registry.get(name)
2915
if (opt is not None and opt.from_unicode is not None
2916
and value is not None):
2917
# If a value exists and the option provides a converter, use it
2919
converted = opt.from_unicode(value)
2920
except (ValueError, TypeError):
2921
# Invalid values are ignored
2923
if converted is None and opt.invalid is not None:
2924
# The conversion failed
2925
if opt.invalid == 'warning':
2926
trace.warning('Value "%s" is not valid for "%s"',
2928
elif opt.invalid == 'error':
2929
raise errors.ConfigOptionValueError(name, value)
2932
# If the option is registered, it may provide a default value
2934
value = opt.get_default()
2935
for hook in ConfigHooks['get']:
2936
hook(self, name, value)
2939
def _get_mutable_section(self):
2940
"""Get the MutableSection for the Stack.
2942
This is where we guarantee that the mutable section is lazily loaded:
2943
this means we won't load the corresponding store before setting a value
2944
or deleting an option. In practice the store will often be loaded but
2945
this allows helps catching some programming errors.
2947
section = self.store.get_mutable_section(self.mutable_section_name)
2950
def set(self, name, value):
2951
"""Set a new value for the option."""
2952
section = self._get_mutable_section()
2953
section.set(name, value)
2954
for hook in ConfigHooks['set']:
2955
hook(self, name, value)
2957
def remove(self, name):
2958
"""Remove an existing option."""
2959
section = self._get_mutable_section()
2960
section.remove(name)
2961
for hook in ConfigHooks['remove']:
2965
# Mostly for debugging use
2966
return "<config.%s(%s)>" % (self.__class__.__name__, id(self))
2969
class _CompatibleStack(Stack):
2970
"""Place holder for compatibility with previous design.
2972
This is intended to ease the transition from the Config-based design to the
2973
Stack-based design and should not be used nor relied upon by plugins.
2975
One assumption made here is that the daughter classes will all use Stores
2976
derived from LockableIniFileStore).
2978
It implements set() by re-loading the store before applying the
2979
modification and saving it.
2981
The long term plan being to implement a single write by store to save
2982
all modifications, this class should not be used in the interim.
2985
def set(self, name, value):
2988
super(_CompatibleStack, self).set(name, value)
2989
# Force a write to persistent storage
2993
class GlobalStack(_CompatibleStack):
2997
gstore = GlobalStore()
2998
super(GlobalStack, self).__init__([gstore.get_sections], gstore)
3001
class LocationStack(_CompatibleStack):
3003
def __init__(self, location):
3004
"""Make a new stack for a location and global configuration.
3006
:param location: A URL prefix to """
3007
lstore = LocationStore()
3008
matcher = LocationMatcher(lstore, location)
3009
gstore = GlobalStore()
3010
super(LocationStack, self).__init__(
3011
[matcher.get_sections, gstore.get_sections], lstore)
3013
class BranchStack(_CompatibleStack):
3015
def __init__(self, branch):
3016
bstore = BranchStore(branch)
3017
lstore = LocationStore()
3018
matcher = LocationMatcher(lstore, branch.base)
3019
gstore = GlobalStore()
3020
super(BranchStack, self).__init__(
3021
[matcher.get_sections, bstore.get_sections, gstore.get_sections],
3023
self.branch = branch
3026
class cmd_config(commands.Command):
3027
__doc__ = """Display, set or remove a configuration option.
3029
Display the active value for a given option.
3031
If --all is specified, NAME is interpreted as a regular expression and all
3032
matching options are displayed mentioning their scope. The active value
3033
that bzr will take into account is the first one displayed for each option.
3035
If no NAME is given, --all .* is implied.
3037
Setting a value is achieved by using name=value without spaces. The value
3038
is set in the most relevant scope and can be checked by displaying the
3042
takes_args = ['name?']
3046
# FIXME: This should be a registry option so that plugins can register
3047
# their own config files (or not) -- vila 20101002
3048
commands.Option('scope', help='Reduce the scope to the specified'
3049
' configuration file',
3051
commands.Option('all',
3052
help='Display all the defined values for the matching options.',
3054
commands.Option('remove', help='Remove the option from'
3055
' the configuration file'),
3058
_see_also = ['configuration']
3060
@commands.display_command
3061
def run(self, name=None, all=False, directory=None, scope=None,
3063
if directory is None:
3065
directory = urlutils.normalize_url(directory)
3067
raise errors.BzrError(
3068
'--all and --remove are mutually exclusive.')
3070
# Delete the option in the given scope
3071
self._remove_config_option(name, directory, scope)
3073
# Defaults to all options
3074
self._show_matching_options('.*', directory, scope)
3077
name, value = name.split('=', 1)
3079
# Display the option(s) value(s)
3081
self._show_matching_options(name, directory, scope)
3083
self._show_value(name, directory, scope)
3086
raise errors.BzrError(
3087
'Only one option can be set.')
3088
# Set the option value
3089
self._set_config_option(name, value, directory, scope)
3091
def _get_configs(self, directory, scope=None):
3092
"""Iterate the configurations specified by ``directory`` and ``scope``.
3094
:param directory: Where the configurations are derived from.
3096
:param scope: A specific config to start from.
3098
if scope is not None:
3099
if scope == 'bazaar':
3100
yield GlobalConfig()
3101
elif scope == 'locations':
3102
yield LocationConfig(directory)
3103
elif scope == 'branch':
3104
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
3106
yield br.get_config()
3109
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
3111
yield br.get_config()
3112
except errors.NotBranchError:
3113
yield LocationConfig(directory)
3114
yield GlobalConfig()
3116
def _show_value(self, name, directory, scope):
3118
for c in self._get_configs(directory, scope):
3121
for (oname, value, section, conf_id, parser) in c._get_options():
3123
# Display only the first value and exit
3125
# FIXME: We need to use get_user_option to take policies
3126
# into account and we need to make sure the option exists
3127
# too (hence the two for loops), this needs a better API
3129
value = c.get_user_option(name)
3130
# Quote the value appropriately
3131
value = parser._quote(value)
3132
self.outf.write('%s\n' % (value,))
3136
raise errors.NoSuchConfigOption(name)
3138
def _show_matching_options(self, name, directory, scope):
3139
name = lazy_regex.lazy_compile(name)
3140
# We want any error in the regexp to be raised *now* so we need to
3141
# avoid the delay introduced by the lazy regexp. But, we still do
3142
# want the nicer errors raised by lazy_regex.
3143
name._compile_and_collapse()
3146
for c in self._get_configs(directory, scope):
3147
for (oname, value, section, conf_id, parser) in c._get_options():
3148
if name.search(oname):
3149
if cur_conf_id != conf_id:
3150
# Explain where the options are defined
3151
self.outf.write('%s:\n' % (conf_id,))
3152
cur_conf_id = conf_id
3154
if (section not in (None, 'DEFAULT')
3155
and cur_section != section):
3156
# Display the section if it's not the default (or only)
3158
self.outf.write(' [%s]\n' % (section,))
3159
cur_section = section
3160
self.outf.write(' %s = %s\n' % (oname, value))
3162
def _set_config_option(self, name, value, directory, scope):
3163
for conf in self._get_configs(directory, scope):
3164
conf.set_user_option(name, value)
3167
raise errors.NoSuchConfig(scope)
3169
def _remove_config_option(self, name, directory, scope):
3171
raise errors.BzrCommandError(
3172
'--remove expects an option to remove.')
3174
for conf in self._get_configs(directory, scope):
3175
for (section_name, section, conf_id) in conf._get_sections():
3176
if scope is not None and conf_id != scope:
3177
# Not the right configuration file
3180
if conf_id != conf.config_id():
3181
conf = self._get_configs(directory, conf_id).next()
3182
# We use the first section in the first config where the
3183
# option is defined to remove it
3184
conf.remove_user_option(name, section_name)
3189
raise errors.NoSuchConfig(scope)
3191
raise errors.NoSuchConfigOption(name)
3195
# We need adapters that can build a Store or a Stack in a test context. Test
3196
# classes, based on TestCaseWithTransport, can use the registry to parametrize
3197
# themselves. The builder will receive a test instance and should return a
3198
# ready-to-use store or stack. Plugins that define new store/stacks can also
3199
# register themselves here to be tested against the tests defined in
3200
# bzrlib.tests.test_config. Note that the builder can be called multiple times
3201
# for the same tests.
3203
# The registered object should be a callable receiving a test instance
3204
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Store
3206
test_store_builder_registry = registry.Registry()
3208
# The registered object should be a callable receiving a test instance
3209
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Stack
3211
test_stack_builder_registry = registry.Registry()
865
self.branch.lock_write()
867
cfg_obj = self._get_config()
872
obj = cfg_obj[section]
874
cfg_obj[section] = {}
875
obj = cfg_obj[section]
877
out_file = StringIO()
878
cfg_obj.write(out_file)
880
self.branch.control_files.put('branch.conf', out_file)