893
202
raise errors.BzrError("Invalid signatures policy '%s'"
894
203
% signature_string)
896
def _string_to_signing_policy(self, signature_string):
897
"""Convert a string to a signing policy."""
898
if signature_string.lower() == 'when-required':
899
return SIGN_WHEN_REQUIRED
900
if signature_string.lower() == 'never':
902
if signature_string.lower() == 'always':
904
raise errors.BzrError("Invalid signing policy '%s'"
907
def _get_alias(self, value):
909
return self._get_parser().get_value("ALIASES",
914
def _get_nickname(self):
915
return self.get_user_option('nickname')
917
def remove_user_option(self, option_name, section_name=None):
918
"""Remove a user option and save the configuration file.
920
:param option_name: The option to be removed.
922
:param section_name: The section the option is defined in, default to
926
parser = self._get_parser()
927
if section_name is None:
930
section = parser[section_name]
932
del section[option_name]
934
raise errors.NoSuchConfigOption(option_name)
935
self._write_config_file()
936
for hook in OldConfigHooks['remove']:
937
hook(self, option_name)
939
def _write_config_file(self):
940
if self.file_name is None:
941
raise AssertionError('We cannot save, self.file_name is None')
942
conf_dir = os.path.dirname(self.file_name)
943
ensure_config_dir_exists(conf_dir)
944
atomic_file = atomicfile.AtomicFile(self.file_name)
945
self._get_parser().write(atomic_file)
948
osutils.copy_ownership_from_path(self.file_name)
949
for hook in OldConfigHooks['save']:
953
class LockableConfig(IniBasedConfig):
954
"""A configuration needing explicit locking for access.
956
If several processes try to write the config file, the accesses need to be
959
Daughter classes should decorate all methods that update a config with the
960
``@needs_write_lock`` decorator (they call, directly or indirectly, the
961
``_write_config_file()`` method. These methods (typically ``set_option()``
962
and variants must reload the config file from disk before calling
963
``_write_config_file()``), this can be achieved by calling the
964
``self.reload()`` method. Note that the lock scope should cover both the
965
reading and the writing of the config file which is why the decorator can't
966
be applied to ``_write_config_file()`` only.
968
This should be enough to implement the following logic:
969
- lock for exclusive write access,
970
- reload the config file from disk,
974
This logic guarantees that a writer can update a value without erasing an
975
update made by another writer.
980
def __init__(self, file_name):
981
super(LockableConfig, self).__init__(file_name=file_name)
982
self.dir = osutils.dirname(osutils.safe_unicode(self.file_name))
983
# FIXME: It doesn't matter that we don't provide possible_transports
984
# below since this is currently used only for local config files ;
985
# local transports are not shared. But if/when we start using
986
# LockableConfig for other kind of transports, we will need to reuse
987
# whatever connection is already established -- vila 20100929
988
self.transport = transport.get_transport(self.dir)
989
self._lock = lockdir.LockDir(self.transport, self.lock_name)
991
def _create_from_string(self, unicode_bytes, save):
992
super(LockableConfig, self)._create_from_string(unicode_bytes, False)
994
# We need to handle the saving here (as opposed to IniBasedConfig)
997
self._write_config_file()
1000
def lock_write(self, token=None):
1001
"""Takes a write lock in the directory containing the config file.
1003
If the directory doesn't exist it is created.
1005
ensure_config_dir_exists(self.dir)
1006
return self._lock.lock_write(token)
1011
def break_lock(self):
1012
self._lock.break_lock()
1015
def remove_user_option(self, option_name, section_name=None):
1016
super(LockableConfig, self).remove_user_option(option_name,
1019
def _write_config_file(self):
1020
if self._lock is None or not self._lock.is_held:
1021
# NB: if the following exception is raised it probably means a
1022
# missing @needs_write_lock decorator on one of the callers.
1023
raise errors.ObjectNotLocked(self)
1024
super(LockableConfig, self)._write_config_file()
1027
class GlobalConfig(LockableConfig):
206
class GlobalConfig(IniBasedConfig):
1028
207
"""The configuration that should be used for a specific location."""
209
def get_editor(self):
210
if self._get_parser().has_option(self._get_section(), 'editor'):
211
return self._get_parser().get(self._get_section(), 'editor')
1030
213
def __init__(self):
1031
super(GlobalConfig, self).__init__(file_name=config_filename())
1033
def config_id(self):
1037
def from_string(cls, str_or_unicode, save=False):
1038
"""Create a config object from a string.
1040
:param str_or_unicode: A string representing the file content. This
1041
will be utf-8 encoded.
1043
:param save: Whether the file should be saved upon creation.
214
super(GlobalConfig, self).__init__(config_filename)
217
class LocationConfig(IniBasedConfig):
218
"""A configuration object that gives the policy for a location."""
220
def __init__(self, location):
221
super(LocationConfig, self).__init__(branches_config_filename)
222
self._global_config = None
223
self.location = location
225
def _get_global_config(self):
226
if self._global_config is None:
227
self._global_config = GlobalConfig()
228
return self._global_config
230
def _get_section(self):
231
"""Get the section we should look in for config items.
233
Returns None if none exists.
234
TODO: perhaps return a NullSection that thunks through to the
1046
conf._create_from_string(str_or_unicode, save)
1049
@deprecated_method(deprecated_in((2, 4, 0)))
1050
def get_editor(self):
1051
return self._get_user_option('editor')
1054
def set_user_option(self, option, value):
1055
"""Save option and its value in the configuration."""
1056
self._set_option(option, value, 'DEFAULT')
1058
def get_aliases(self):
1059
"""Return the aliases section."""
1060
if 'ALIASES' in self._get_parser():
1061
return self._get_parser()['ALIASES']
1066
def set_alias(self, alias_name, alias_command):
1067
"""Save the alias in the configuration."""
1068
self._set_option(alias_name, alias_command, 'ALIASES')
1071
def unset_alias(self, alias_name):
1072
"""Unset an existing alias."""
1074
aliases = self._get_parser().get('ALIASES')
1075
if not aliases or alias_name not in aliases:
1076
raise errors.NoSuchAlias(alias_name)
1077
del aliases[alias_name]
1078
self._write_config_file()
1080
def _set_option(self, option, value, section):
1082
self._get_parser().setdefault(section, {})[option] = value
1083
self._write_config_file()
1084
for hook in OldConfigHooks['set']:
1085
hook(self, option, value)
1087
def _get_sections(self, name=None):
1088
"""See IniBasedConfig._get_sections()."""
1089
parser = self._get_parser()
1090
# We don't give access to options defined outside of any section, we
1091
# used the DEFAULT section by... default.
1092
if name in (None, 'DEFAULT'):
1093
# This could happen for an empty file where the DEFAULT section
1094
# doesn't exist yet. So we force DEFAULT when yielding
1096
if 'DEFAULT' not in parser:
1097
parser['DEFAULT']= {}
1098
yield (name, parser[name], self.config_id())
1101
def remove_user_option(self, option_name, section_name=None):
1102
if section_name is None:
1103
# We need to force the default section.
1104
section_name = 'DEFAULT'
1105
# We need to avoid the LockableConfig implementation or we'll lock
1107
super(LockableConfig, self).remove_user_option(option_name,
1110
def _iter_for_location_by_parts(sections, location):
1111
"""Keep only the sessions matching the specified location.
1113
:param sections: An iterable of section names.
1115
:param location: An url or a local path to match against.
1117
:returns: An iterator of (section, extra_path, nb_parts) where nb is the
1118
number of path components in the section name, section is the section
1119
name and extra_path is the difference between location and the section
1122
``location`` will always be a local path and never a 'file://' url but the
1123
section names themselves can be in either form.
1125
location_parts = location.rstrip('/').split('/')
1127
for section in sections:
1128
# location is a local path if possible, so we need to convert 'file://'
1129
# urls in section names to local paths if necessary.
1131
# This also avoids having file:///path be a more exact
1132
# match than '/path'.
1134
# FIXME: This still raises an issue if a user defines both file:///path
1135
# *and* /path. Should we raise an error in this case -- vila 20110505
1137
if section.startswith('file://'):
1138
section_path = urlutils.local_path_from_url(section)
1140
section_path = section
1141
section_parts = section_path.rstrip('/').split('/')
1144
if len(section_parts) > len(location_parts):
1145
# More path components in the section, they can't match
1148
# Rely on zip truncating in length to the length of the shortest
1149
# argument sequence.
1150
names = zip(location_parts, section_parts)
237
sections = self._get_parser().sections()
238
location_names = self.location.split('/')
239
if self.location.endswith('/'):
240
del location_names[-1]
242
for section in sections:
243
section_names = section.split('/')
244
if section.endswith('/'):
245
del section_names[-1]
246
names = zip(location_names, section_names)
1151
248
for name in names:
1152
if not fnmatch.fnmatch(name[0], name[1]):
249
if not fnmatch(name[0], name[1]):
1157
# build the path difference between the section and the location
1158
extra_path = '/'.join(location_parts[len(section_parts):])
1159
yield section, extra_path, len(section_parts)
1162
class LocationConfig(LockableConfig):
1163
"""A configuration object that gives the policy for a location."""
1165
def __init__(self, location):
1166
super(LocationConfig, self).__init__(
1167
file_name=locations_config_filename())
1168
# local file locations are looked up by local path, rather than
1169
# by file url. This is because the config file is a user
1170
# file, and we would rather not expose the user to file urls.
1171
if location.startswith('file://'):
1172
location = urlutils.local_path_from_url(location)
1173
self.location = location
1175
def config_id(self):
1179
def from_string(cls, str_or_unicode, location, save=False):
1180
"""Create a config object from a string.
1182
:param str_or_unicode: A string representing the file content. This will
1185
:param location: The location url to filter the configuration.
1187
:param save: Whether the file should be saved upon creation.
1189
conf = cls(location)
1190
conf._create_from_string(str_or_unicode, save)
1193
def _get_matching_sections(self):
1194
"""Return an ordered list of section names matching this location."""
1195
matches = list(_iter_for_location_by_parts(self._get_parser(),
1197
# put the longest (aka more specific) locations first
1199
key=lambda (section, extra_path, length): (length, section),
1201
for (section, extra_path, length) in matches:
1202
yield section, extra_path
1203
# should we stop looking for parent configs here?
1205
if self._get_parser()[section].as_bool('ignore_parents'):
1210
def _get_sections(self, name=None):
1211
"""See IniBasedConfig._get_sections()."""
1212
# We ignore the name here as the only sections handled are named with
1213
# the location path and we don't expose embedded sections either.
1214
parser = self._get_parser()
1215
for name, extra_path in self._get_matching_sections():
1216
yield (name, parser[name], self.config_id())
1218
def _get_option_policy(self, section, option_name):
1219
"""Return the policy for the given (section, option_name) pair."""
1220
# check for the old 'recurse=False' flag
1222
recurse = self._get_parser()[section].as_bool('recurse')
1226
return POLICY_NORECURSE
1228
policy_key = option_name + ':policy'
1230
policy_name = self._get_parser()[section][policy_key]
1234
return _policy_value[policy_name]
1236
def _set_option_policy(self, section, option_name, option_policy):
1237
"""Set the policy for the given option name in the given section."""
1238
# The old recurse=False option affects all options in the
1239
# section. To handle multiple policies in the section, we
1240
# need to convert it to a policy_norecurse key.
1242
recurse = self._get_parser()[section].as_bool('recurse')
1246
symbol_versioning.warn(
1247
'The recurse option is deprecated as of 0.14. '
1248
'The section "%s" has been converted to use policies.'
1251
del self._get_parser()[section]['recurse']
1253
for key in self._get_parser()[section].keys():
1254
if not key.endswith(':policy'):
1255
self._get_parser()[section][key +
1256
':policy'] = 'norecurse'
1258
policy_key = option_name + ':policy'
1259
policy_name = _policy_name[option_policy]
1260
if policy_name is not None:
1261
self._get_parser()[section][policy_key] = policy_name
1263
if policy_key in self._get_parser()[section]:
1264
del self._get_parser()[section][policy_key]
1267
def set_user_option(self, option, value, store=STORE_LOCATION):
1268
"""Save option and its value in the configuration."""
1269
if store not in [STORE_LOCATION,
1270
STORE_LOCATION_NORECURSE,
1271
STORE_LOCATION_APPENDPATH]:
1272
raise ValueError('bad storage policy %r for %r' %
1275
location = self.location
1276
if location.endswith('/'):
1277
location = location[:-1]
1278
parser = self._get_parser()
1279
if not location in parser and not location + '/' in parser:
1280
parser[location] = {}
1281
elif location + '/' in parser:
1282
location = location + '/'
1283
parser[location][option]=value
1284
# the allowed values of store match the config policies
1285
self._set_option_policy(location, option, store)
1286
self._write_config_file()
1287
for hook in OldConfigHooks['set']:
1288
hook(self, option, value)
254
# so, for the common prefix they matched.
255
# if section is longer, no match.
256
if len(section_names) > len(location_names):
258
# if path is longer, and recurse is not true, no match
259
if len(section_names) < len(location_names):
260
if (self._get_parser().has_option(section, 'recurse')
261
and not self._get_parser().getboolean(section, 'recurse')):
263
matches.append((len(section_names), section))
266
matches.sort(reverse=True)
269
def _gpg_signing_command(self):
270
"""See Config.gpg_signing_command."""
271
command = super(LocationConfig, self)._gpg_signing_command()
272
if command is not None:
274
return self._get_global_config()._gpg_signing_command()
276
def _get_user_id(self):
277
user_id = super(LocationConfig, self)._get_user_id()
278
if user_id is not None:
280
return self._get_global_config()._get_user_id()
282
def _get_signature_checking(self):
283
"""See Config._get_signature_checking."""
284
check = super(LocationConfig, self)._get_signature_checking()
285
if check is not None:
287
return self._get_global_config()._get_signature_checking()
1291
290
class BranchConfig(Config):
1292
291
"""A configuration object giving the policy for a branch."""
1294
def __init__(self, branch):
1295
super(BranchConfig, self).__init__()
1296
self._location_config = None
1297
self._branch_data_config = None
1298
self._global_config = None
1299
self.branch = branch
1300
self.option_sources = (self._get_location_config,
1301
self._get_branch_data_config,
1302
self._get_global_config)
1304
def config_id(self):
1307
def _get_branch_data_config(self):
1308
if self._branch_data_config is None:
1309
self._branch_data_config = TreeConfig(self.branch)
1310
self._branch_data_config.config_id = self.config_id
1311
return self._branch_data_config
1313
293
def _get_location_config(self):
1314
294
if self._location_config is None:
1315
295
self._location_config = LocationConfig(self.branch.base)
1316
296
return self._location_config
1318
def _get_global_config(self):
1319
if self._global_config is None:
1320
self._global_config = GlobalConfig()
1321
return self._global_config
1323
def _get_best_value(self, option_name):
1324
"""This returns a user option from local, tree or global config.
1326
They are tried in that order. Use get_safe_value if trusted values
1329
for source in self.option_sources:
1330
value = getattr(source(), option_name)()
1331
if value is not None:
1335
def _get_safe_value(self, option_name):
1336
"""This variant of get_best_value never returns untrusted values.
1338
It does not return values from the branch data, because the branch may
1339
not be controlled by the user.
1341
We may wish to allow locations.conf to control whether branches are
1342
trusted in the future.
1344
for source in (self._get_location_config, self._get_global_config):
1345
value = getattr(source(), option_name)()
1346
if value is not None:
1350
298
def _get_user_id(self):
1351
299
"""Return the full user id for the branch.
1353
e.g. "John Hacker <jhacker@example.com>"
301
e.g. "John Hacker <jhacker@foo.org>"
1354
302
This is looked up in the email controlfile for the branch.
1357
return (self.branch._transport.get_bytes("email")
1358
.decode(osutils.get_user_encoding())
305
return (self.branch.controlfile("email", "r")
307
.decode(bzrlib.user_encoding)
1359
308
.rstrip("\r\n"))
1360
309
except errors.NoSuchFile, e:
1363
return self._get_best_value('_get_user_id')
1365
def _get_change_editor(self):
1366
return self._get_best_value('_get_change_editor')
312
return self._get_location_config()._get_user_id()
1368
314
def _get_signature_checking(self):
1369
315
"""See Config._get_signature_checking."""
1370
return self._get_best_value('_get_signature_checking')
1372
def _get_signing_policy(self):
1373
"""See Config._get_signing_policy."""
1374
return self._get_best_value('_get_signing_policy')
1376
def _get_user_option(self, option_name):
1377
"""See Config._get_user_option."""
1378
for source in self.option_sources:
1379
value = source()._get_user_option(option_name)
1380
if value is not None:
1384
def _get_sections(self, name=None):
1385
"""See IniBasedConfig.get_sections()."""
1386
for source in self.option_sources:
1387
for section in source()._get_sections(name):
1390
def _get_options(self, sections=None):
1392
# First the locations options
1393
for option in self._get_location_config()._get_options():
1395
# Then the branch options
1396
branch_config = self._get_branch_data_config()
1397
if sections is None:
1398
sections = [('DEFAULT', branch_config._get_parser())]
1399
# FIXME: We shouldn't have to duplicate the code in IniBasedConfig but
1400
# Config itself has no notion of sections :( -- vila 20101001
1401
config_id = self.config_id()
1402
for (section_name, section) in sections:
1403
for (name, value) in section.iteritems():
1404
yield (name, value, section_name,
1405
config_id, branch_config._get_parser())
1406
# Then the global options
1407
for option in self._get_global_config()._get_options():
1410
def set_user_option(self, name, value, store=STORE_BRANCH,
1412
if store == STORE_BRANCH:
1413
self._get_branch_data_config().set_option(value, name)
1414
elif store == STORE_GLOBAL:
1415
self._get_global_config().set_user_option(name, value)
1417
self._get_location_config().set_user_option(name, value, store)
1420
if store in (STORE_GLOBAL, STORE_BRANCH):
1421
mask_value = self._get_location_config().get_user_option(name)
1422
if mask_value is not None:
1423
trace.warning('Value "%s" is masked by "%s" from'
1424
' locations.conf', value, mask_value)
1426
if store == STORE_GLOBAL:
1427
branch_config = self._get_branch_data_config()
1428
mask_value = branch_config.get_user_option(name)
1429
if mask_value is not None:
1430
trace.warning('Value "%s" is masked by "%s" from'
1431
' branch.conf', value, mask_value)
1433
def remove_user_option(self, option_name, section_name=None):
1434
self._get_branch_data_config().remove_option(option_name, section_name)
316
return self._get_location_config()._get_signature_checking()
1436
318
def _gpg_signing_command(self):
1437
319
"""See Config.gpg_signing_command."""
1438
return self._get_safe_value('_gpg_signing_command')
1440
def _post_commit(self):
1441
"""See Config.post_commit."""
1442
return self._get_safe_value('_post_commit')
1444
def _get_nickname(self):
1445
value = self._get_explicit_nickname()
1446
if value is not None:
1448
return urlutils.unescape(self.branch.base.split('/')[-2])
1450
def has_explicit_nickname(self):
1451
"""Return true if a nickname has been explicitly assigned."""
1452
return self._get_explicit_nickname() is not None
1454
def _get_explicit_nickname(self):
1455
return self._get_best_value('_get_nickname')
1457
def _log_format(self):
1458
"""See Config.log_format."""
1459
return self._get_best_value('_log_format')
1461
def _validate_signatures_in_log(self):
1462
"""See Config.validate_signatures_in_log."""
1463
return self._get_best_value('_validate_signatures_in_log')
1465
def _acceptable_keys(self):
1466
"""See Config.acceptable_keys."""
1467
return self._get_best_value('_acceptable_keys')
1470
def ensure_config_dir_exists(path=None):
1471
"""Make sure a configuration directory exists.
1472
This makes sure that the directory exists.
1473
On windows, since configuration directories are 2 levels deep,
1474
it makes sure both the directory and the parent directory exists.
1478
if not os.path.isdir(path):
1479
if sys.platform == 'win32':
1480
parent_dir = os.path.dirname(path)
1481
if not os.path.isdir(parent_dir):
1482
trace.mutter('creating config parent directory: %r', parent_dir)
1483
os.mkdir(parent_dir)
1484
trace.mutter('creating config directory: %r', path)
1486
osutils.copy_ownership_from_path(path)
320
return self._get_location_config()._gpg_signing_command()
322
def __init__(self, branch):
323
super(BranchConfig, self).__init__()
324
self._location_config = None
1489
328
def config_dir():
1490
329
"""Return per-user configuration directory.
1492
By default this is %APPDATA%/bazaar/2.0 on Windows, ~/.bazaar on Mac OS X
1493
and Linux. On Linux, if there is a $XDG_CONFIG_HOME/bazaar directory,
1494
that will be used instead.
331
By default this is ~/.bazaar/
1496
333
TODO: Global option --config-dir to override this.
1498
base = os.environ.get('BZR_HOME', None)
1499
if sys.platform == 'win32':
1500
# environ variables on Windows are in user encoding/mbcs. So decode
1502
if base is not None:
1503
base = base.decode('mbcs')
1505
base = win32utils.get_appdata_location_unicode()
1507
base = os.environ.get('HOME', None)
1508
if base is not None:
1509
base = base.decode('mbcs')
1511
raise errors.BzrError('You must have one of BZR_HOME, APPDATA,'
1513
return osutils.pathjoin(base, 'bazaar', '2.0')
1515
if base is not None:
1516
base = base.decode(osutils._fs_enc)
1517
if sys.platform == 'darwin':
1519
# this takes into account $HOME
1520
base = os.path.expanduser("~")
1521
return osutils.pathjoin(base, '.bazaar')
1524
xdg_dir = os.environ.get('XDG_CONFIG_HOME', None)
1526
xdg_dir = osutils.pathjoin(os.path.expanduser("~"), ".config")
1527
xdg_dir = osutils.pathjoin(xdg_dir, 'bazaar')
1528
if osutils.isdir(xdg_dir):
1530
"Using configuration in XDG directory %s." % xdg_dir)
1532
base = os.path.expanduser("~")
1533
return osutils.pathjoin(base, ".bazaar")
335
return os.path.join(os.path.expanduser("~"), ".bazaar")
1536
338
def config_filename():
1537
339
"""Return per-user configuration ini file filename."""
1538
return osutils.pathjoin(config_dir(), 'bazaar.conf')
1541
def locations_config_filename():
340
return os.path.join(config_dir(), 'bazaar.conf')
343
def branches_config_filename():
1542
344
"""Return per-user configuration ini file filename."""
1543
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
def user_ignore_config_filename():
1552
"""Return the user default ignore filename"""
1553
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()
345
return os.path.join(config_dir(), 'branches.conf')
1602
348
def _auto_user_id():
1603
349
"""Calculate automatic user identification.
1605
:returns: (realname, email), either of which may be None if they can't be
351
Returns (realname, email).
1608
353
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.
355
This previously used the FQDN as the default domain, but that can
356
be very slow on machines where DNS is broken. So now we simply
1617
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:
361
# XXX: Any good way to get real user name on win32?
1629
366
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))
367
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
368
username = w.pw_name.decode(bzrlib.user_encoding)
369
comma = gecos.find(',')
373
realname = gecos[:comma]
379
realname = username = getpass.getuser().decode(bzrlib.user_encoding)
381
return realname, (username + '@' + socket.gethostname())
1672
384
def extract_email_address(e):
1673
385
"""Return just the address part of an email string.
1675
That is just the user@domain part, nothing else.
387
That is just the user@domain part, nothing else.
1676
388
This part is required to contain only ascii characters.
1677
389
If it can't be extracted, raises an error.
1679
391
>>> extract_email_address('Jane Tester <jane@test.com>')
1682
name, email = parse_username(e)
1684
raise errors.NoEmailInUsername(e)
1688
class TreeConfig(IniBasedConfig):
1689
"""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
def __init__(self, branch):
1694
self._config = branch._get_config()
1695
self.branch = branch
1697
def _get_parser(self, file=None):
1698
if file is not None:
1699
return IniBasedConfig._get_parser(file)
1700
return self._config._get_configobj()
1702
def get_option(self, name, section=None, default=None):
1703
self.branch.lock_read()
1705
return self._config.get_option(name, section, default)
1707
self.branch.unlock()
1709
def set_option(self, value, name, section=None):
1710
"""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()
394
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
396
raise BzrError("%r doesn't seem to contain "
397
"a reasonable email address" % e)