126
def ConfigObj(*args, **kwargs):
128
if _ConfigObj is None:
129
class ConfigObj(configobj.ConfigObj):
131
def get_bool(self, section, key):
132
return self[section].as_bool(key)
134
def get_value(self, section, name):
135
# Try [] for the old DEFAULT section.
136
if section == "DEFAULT":
141
return self[section][name]
142
_ConfigObj = ConfigObj
143
return _ConfigObj(*args, **kwargs)
134
class ConfigObj(configobj.ConfigObj):
136
def __init__(self, infile=None, **kwargs):
137
# We define our own interpolation mechanism calling it option expansion
138
super(ConfigObj, self).__init__(infile=infile,
143
def get_bool(self, section, key):
144
return self[section].as_bool(key)
146
def get_value(self, section, name):
147
# Try [] for the old DEFAULT section.
148
if section == "DEFAULT":
153
return self[section][name]
156
# FIXME: Until we can guarantee that each config file is loaded once and and
157
# only once for a given bzrlib session, we don't want to re-read the file every
158
# time we query for an option so we cache the value (bad ! watch out for tests
159
# needing to restore the proper value).This shouldn't be part of 2.4.0 final,
160
# yell at mgz^W vila and the RM if this is still present at that time
162
_expand_default_value = None
163
def _get_expand_default_value():
164
global _expand_default_value
165
if _expand_default_value is not None:
166
return _expand_default_value
167
conf = GlobalConfig()
168
# Note that we must not use None for the expand value below or we'll run
169
# into infinite recursion. Using False really would be quite silly ;)
170
expand = conf.get_user_option_as_bool('bzr.config.expand', expand=True)
172
# This is an opt-in feature, you *really* need to clearly say you want
175
_expand_default_value = expand
146
179
class Config(object):
178
215
def _get_signing_policy(self):
179
216
"""Template method to override signature creation policy."""
220
def expand_options(self, string, env=None):
221
"""Expand option references in the string in the configuration context.
223
:param string: The string containing option to expand.
225
:param env: An option dict defining additional configuration options or
226
overriding existing ones.
228
:returns: The expanded string.
230
return self._expand_options_in_string(string, env)
232
def _expand_options_in_list(self, slist, env=None, _ref_stack=None):
233
"""Expand options in a list of strings in the configuration context.
235
:param slist: A list of strings.
237
:param env: An option dict defining additional configuration options or
238
overriding existing ones.
240
:param _ref_stack: Private list containing the options being
241
expanded to detect loops.
243
:returns: The flatten list of expanded strings.
245
# expand options in each value separately flattening lists
248
value = self._expand_options_in_string(s, env, _ref_stack)
249
if isinstance(value, list):
255
def _expand_options_in_string(self, string, env=None, _ref_stack=None):
256
"""Expand options in the string in the configuration context.
258
:param string: The string to be expanded.
260
:param env: An option dict defining additional configuration options or
261
overriding existing ones.
263
:param _ref_stack: Private list containing the options being
264
expanded to detect loops.
266
:returns: The expanded string.
269
# Not much to expand there
271
if _ref_stack is None:
272
# What references are currently resolved (to detect loops)
274
if self.option_ref_re is None:
275
# We want to match the most embedded reference first (i.e. for
276
# '{{foo}}' we will get '{foo}',
277
# for '{bar{baz}}' we will get '{baz}'
278
self.option_ref_re = re.compile('({[^{}]+})')
280
# We need to iterate until no more refs appear ({{foo}} will need two
281
# iterations for example).
283
raw_chunks = self.option_ref_re.split(result)
284
if len(raw_chunks) == 1:
285
# Shorcut the trivial case: no refs
289
# Split will isolate refs so that every other chunk is a ref
291
for chunk in raw_chunks:
294
# Keep only non-empty strings (or we get bogus empty
295
# slots when a list value is involved).
300
if name in _ref_stack:
301
raise errors.OptionExpansionLoop(string, _ref_stack)
302
_ref_stack.append(name)
303
value = self._expand_option(name, env, _ref_stack)
305
raise errors.ExpandingUnknownOption(name, string)
306
if isinstance(value, list):
314
# Once a list appears as the result of an expansion, all
315
# callers will get a list result. This allows a consistent
316
# behavior even when some options in the expansion chain
317
# defined as strings (no comma in their value) but their
318
# expanded value is a list.
319
return self._expand_options_in_list(chunks, env, _ref_stack)
321
result = ''.join(chunks)
324
def _expand_option(self, name, env, _ref_stack):
325
if env is not None and name in env:
326
# Special case, values provided in env takes precedence over
330
# FIXME: This is a limited implementation, what we really need is a
331
# way to query the bzr config for the value of an option,
332
# respecting the scope rules (That is, once we implement fallback
333
# configs, getting the option value should restart from the top
334
# config, not the current one) -- vila 20101222
335
value = self.get_user_option(name, expand=False)
336
if isinstance(value, list):
337
value = self._expand_options_in_list(value, env, _ref_stack)
339
value = self._expand_options_in_string(value, env, _ref_stack)
181
342
def _get_user_option(self, option_name):
182
343
"""Template method to provide a user option."""
185
def get_user_option(self, option_name):
186
"""Get a generic option - no special process, no default."""
187
return self._get_user_option(option_name)
189
def get_user_option_as_bool(self, option_name):
346
def get_user_option(self, option_name, expand=None):
347
"""Get a generic option - no special process, no default.
349
:param option_name: The queried option.
351
:param expand: Whether options references should be expanded.
353
:returns: The value of the option.
356
expand = _get_expand_default_value()
357
value = self._get_user_option(option_name)
359
if isinstance(value, list):
360
value = self._expand_options_in_list(value)
361
elif isinstance(value, dict):
362
trace.warning('Cannot expand "%s":'
363
' Dicts do not support option expansion'
366
value = self._expand_options_in_string(value)
369
def get_user_option_as_bool(self, option_name, expand=None):
190
370
"""Get a generic option as a boolean - no special process, no default.
192
372
:return None if the option doesn't exist or its value can't be
193
373
interpreted as a boolean. Returns True or False otherwise.
195
s = self._get_user_option(option_name)
375
s = self.get_user_option(option_name, expand=expand)
197
377
# The option doesn't exist
258
439
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
260
$BZR_EMAIL can be set to override this (as well as the
261
deprecated $BZREMAIL), then
441
$BZR_EMAIL can be set to override this, then
262
442
the concrete policy type is checked, and finally
263
443
$EMAIL is examined.
264
If none is found, a reasonable default is (hopefully)
267
TODO: Check it's reasonably well-formed.
444
If no username can be found, errors.NoWhoami exception is raised.
269
446
v = os.environ.get('BZR_EMAIL')
271
448
return v.decode(osutils.get_user_encoding())
273
449
v = self._get_user_id()
277
452
v = os.environ.get('EMAIL')
279
454
return v.decode(osutils.get_user_encoding())
281
455
name, email = _auto_user_id()
283
457
return '%s <%s>' % (name, email)
460
raise errors.NoWhoami()
462
def ensure_username(self):
463
"""Raise errors.NoWhoami if username is not set.
465
This method relies on the username() function raising the error.
287
469
def signature_checking(self):
288
470
"""What is the current policy for signature checking?."""
531
def get_merge_tools(self):
533
for (oname, value, section, conf_id, parser) in self._get_options():
534
if oname.startswith('bzr.mergetool.'):
535
tool_name = oname[len('bzr.mergetool.'):]
536
tools[tool_name] = value
537
trace.mutter('loaded merge tools: %r' % tools)
540
def find_merge_tool(self, name):
541
# We fake a defaults mechanism here by checking if the given name can
542
# be found in the known_merge_tools if it's not found in the config.
543
# This should be done through the proposed config defaults mechanism
544
# when it becomes available in the future.
545
command_line = (self.get_user_option('bzr.mergetool.%s' % name,
547
or mergetools.known_merge_tools.get(name, None))
350
551
class IniBasedConfig(Config):
351
552
"""A configuration policy that draws from ini files."""
353
def __init__(self, get_filename):
554
def __init__(self, get_filename=symbol_versioning.DEPRECATED_PARAMETER,
556
"""Base class for configuration files using an ini-like syntax.
558
:param file_name: The configuration file path.
354
560
super(IniBasedConfig, self).__init__()
355
self._get_filename = get_filename
561
self.file_name = file_name
562
if symbol_versioning.deprecated_passed(get_filename):
563
symbol_versioning.warn(
564
'IniBasedConfig.__init__(get_filename) was deprecated in 2.3.'
565
' Use file_name instead.',
568
if get_filename is not None:
569
self.file_name = get_filename()
571
self.file_name = file_name
356
573
self._parser = None
358
def _get_parser(self, file=None):
576
def from_string(cls, str_or_unicode, file_name=None, save=False):
577
"""Create a config object from a string.
579
:param str_or_unicode: A string representing the file content. This will
582
:param file_name: The configuration file path.
584
:param _save: Whether the file should be saved upon creation.
586
conf = cls(file_name=file_name)
587
conf._create_from_string(str_or_unicode, save)
590
def _create_from_string(self, str_or_unicode, save):
591
self._content = StringIO(str_or_unicode.encode('utf-8'))
592
# Some tests use in-memory configs, some other always need the config
593
# file to exist on disk.
595
self._write_config_file()
597
def _get_parser(self, file=symbol_versioning.DEPRECATED_PARAMETER):
359
598
if self._parser is not None:
360
599
return self._parser
362
input = self._get_filename()
600
if symbol_versioning.deprecated_passed(file):
601
symbol_versioning.warn(
602
'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
603
' Use IniBasedConfig(_content=xxx) instead.',
606
if self._content is not None:
607
co_input = self._content
608
elif self.file_name is None:
609
raise AssertionError('We have no content to create the config')
611
co_input = self.file_name
366
self._parser = ConfigObj(input, encoding='utf-8')
613
self._parser = ConfigObj(co_input, encoding='utf-8')
367
614
except configobj.ConfigObjError, e:
368
615
raise errors.ParseConfigError(e.errors, e.config.filename)
616
# Make sure self.reload() will use the right file name
617
self._parser.filename = self.file_name
369
618
return self._parser
621
"""Reload the config file from disk."""
622
if self.file_name is None:
623
raise AssertionError('We need a file name to reload the config')
624
if self._parser is not None:
625
self._parser.reload()
371
627
def _get_matching_sections(self):
372
628
"""Return an ordered list of (section_name, extra_path) pairs.
384
640
"""Override this to define the section used by the config."""
643
def _get_sections(self, name=None):
644
"""Returns an iterator of the sections specified by ``name``.
646
:param name: The section name. If None is supplied, the default
647
configurations are yielded.
649
:return: A tuple (name, section, config_id) for all sections that will
650
be walked by user_get_option() in the 'right' order. The first one
651
is where set_user_option() will update the value.
653
parser = self._get_parser()
655
yield (name, parser[name], self.config_id())
657
# No section name has been given so we fallback to the configobj
658
# itself which holds the variables defined outside of any section.
659
yield (None, parser, self.config_id())
661
def _get_options(self, sections=None):
662
"""Return an ordered list of (name, value, section, config_id) tuples.
664
All options are returned with their associated value and the section
665
they appeared in. ``config_id`` is a unique identifier for the
666
configuration file the option is defined in.
668
:param sections: Default to ``_get_matching_sections`` if not
669
specified. This gives a better control to daughter classes about
670
which sections should be searched. This is a list of (name,
675
parser = self._get_parser()
677
for (section_name, _) in self._get_matching_sections():
679
section = parser[section_name]
681
# This could happen for an empty file for which we define a
682
# DEFAULT section. FIXME: Force callers to provide sections
683
# instead ? -- vila 20100930
685
sections.append((section_name, section))
686
config_id = self.config_id()
687
for (section_name, section) in sections:
688
for (name, value) in section.iteritems():
689
yield (name, parser._quote(value), section_name,
387
692
def _get_option_policy(self, section, option_name):
388
693
"""Return the policy for the given (section, option_name) pair."""
389
694
return POLICY_NONE
476
781
def _get_nickname(self):
477
782
return self.get_user_option('nickname')
480
class GlobalConfig(IniBasedConfig):
784
def remove_user_option(self, option_name, section_name=None):
785
"""Remove a user option and save the configuration file.
787
:param option_name: The option to be removed.
789
:param section_name: The section the option is defined in, default to
793
parser = self._get_parser()
794
if section_name is None:
797
section = parser[section_name]
799
del section[option_name]
801
raise errors.NoSuchConfigOption(option_name)
802
self._write_config_file()
804
def _write_config_file(self):
805
if self.file_name is None:
806
raise AssertionError('We cannot save, self.file_name is None')
807
conf_dir = os.path.dirname(self.file_name)
808
ensure_config_dir_exists(conf_dir)
809
atomic_file = atomicfile.AtomicFile(self.file_name)
810
self._get_parser().write(atomic_file)
813
osutils.copy_ownership_from_path(self.file_name)
816
class LockableConfig(IniBasedConfig):
817
"""A configuration needing explicit locking for access.
819
If several processes try to write the config file, the accesses need to be
822
Daughter classes should decorate all methods that update a config with the
823
``@needs_write_lock`` decorator (they call, directly or indirectly, the
824
``_write_config_file()`` method. These methods (typically ``set_option()``
825
and variants must reload the config file from disk before calling
826
``_write_config_file()``), this can be achieved by calling the
827
``self.reload()`` method. Note that the lock scope should cover both the
828
reading and the writing of the config file which is why the decorator can't
829
be applied to ``_write_config_file()`` only.
831
This should be enough to implement the following logic:
832
- lock for exclusive write access,
833
- reload the config file from disk,
837
This logic guarantees that a writer can update a value without erasing an
838
update made by another writer.
843
def __init__(self, file_name):
844
super(LockableConfig, self).__init__(file_name=file_name)
845
self.dir = osutils.dirname(osutils.safe_unicode(self.file_name))
846
# FIXME: It doesn't matter that we don't provide possible_transports
847
# below since this is currently used only for local config files ;
848
# local transports are not shared. But if/when we start using
849
# LockableConfig for other kind of transports, we will need to reuse
850
# whatever connection is already established -- vila 20100929
851
self.transport = transport.get_transport(self.dir)
852
self._lock = lockdir.LockDir(self.transport, 'lock')
854
def _create_from_string(self, unicode_bytes, save):
855
super(LockableConfig, self)._create_from_string(unicode_bytes, False)
857
# We need to handle the saving here (as opposed to IniBasedConfig)
860
self._write_config_file()
863
def lock_write(self, token=None):
864
"""Takes a write lock in the directory containing the config file.
866
If the directory doesn't exist it is created.
868
ensure_config_dir_exists(self.dir)
869
return self._lock.lock_write(token)
874
def break_lock(self):
875
self._lock.break_lock()
878
def remove_user_option(self, option_name, section_name=None):
879
super(LockableConfig, self).remove_user_option(option_name,
882
def _write_config_file(self):
883
if self._lock is None or not self._lock.is_held:
884
# NB: if the following exception is raised it probably means a
885
# missing @needs_write_lock decorator on one of the callers.
886
raise errors.ObjectNotLocked(self)
887
super(LockableConfig, self)._write_config_file()
890
class GlobalConfig(LockableConfig):
481
891
"""The configuration that should be used for a specific location."""
894
super(GlobalConfig, self).__init__(file_name=config_filename())
900
def from_string(cls, str_or_unicode, save=False):
901
"""Create a config object from a string.
903
:param str_or_unicode: A string representing the file content. This
904
will be utf-8 encoded.
906
:param save: Whether the file should be saved upon creation.
909
conf._create_from_string(str_or_unicode, save)
483
912
def get_editor(self):
484
913
return self._get_user_option('editor')
487
super(GlobalConfig, self).__init__(config_filename)
489
916
def set_user_option(self, option, value):
490
917
"""Save option and its value in the configuration."""
491
918
self._set_option(option, value, 'DEFAULT')
510
940
self._write_config_file()
512
942
def _set_option(self, option, value, section):
513
# FIXME: RBC 20051029 This should refresh the parser and also take a
514
# file lock on bazaar.conf.
515
conf_dir = os.path.dirname(self._get_filename())
516
ensure_config_dir_exists(conf_dir)
517
944
self._get_parser().setdefault(section, {})[option] = value
518
945
self._write_config_file()
520
def _write_config_file(self):
521
path = self._get_filename()
523
osutils.copy_ownership_from_path(path)
524
self._get_parser().write(f)
528
class LocationConfig(IniBasedConfig):
948
def _get_sections(self, name=None):
949
"""See IniBasedConfig._get_sections()."""
950
parser = self._get_parser()
951
# We don't give access to options defined outside of any section, we
952
# used the DEFAULT section by... default.
953
if name in (None, 'DEFAULT'):
954
# This could happen for an empty file where the DEFAULT section
955
# doesn't exist yet. So we force DEFAULT when yielding
957
if 'DEFAULT' not in parser:
958
parser['DEFAULT']= {}
959
yield (name, parser[name], self.config_id())
962
def remove_user_option(self, option_name, section_name=None):
963
if section_name is None:
964
# We need to force the default section.
965
section_name = 'DEFAULT'
966
# We need to avoid the LockableConfig implementation or we'll lock
968
super(LockableConfig, self).remove_user_option(option_name,
972
class LocationConfig(LockableConfig):
529
973
"""A configuration object that gives the policy for a location."""
531
975
def __init__(self, location):
532
name_generator = locations_config_filename
533
if (not os.path.exists(name_generator()) and
534
os.path.exists(branches_config_filename())):
535
if sys.platform == 'win32':
536
trace.warning('Please rename %s to %s'
537
% (branches_config_filename(),
538
locations_config_filename()))
540
trace.warning('Please rename ~/.bazaar/branches.conf'
541
' to ~/.bazaar/locations.conf')
542
name_generator = branches_config_filename
543
super(LocationConfig, self).__init__(name_generator)
976
super(LocationConfig, self).__init__(
977
file_name=locations_config_filename())
544
978
# local file locations are looked up by local path, rather than
545
979
# by file url. This is because the config file is a user
546
980
# file, and we would rather not expose the user to file urls.
648
1110
STORE_LOCATION_APPENDPATH]:
649
1111
raise ValueError('bad storage policy %r for %r' %
650
1112
(store, option))
651
# FIXME: RBC 20051029 This should refresh the parser and also take a
652
# file lock on locations.conf.
653
conf_dir = os.path.dirname(self._get_filename())
654
ensure_config_dir_exists(conf_dir)
655
1114
location = self.location
656
1115
if location.endswith('/'):
657
1116
location = location[:-1]
658
if (not location in self._get_parser() and
659
not location + '/' in self._get_parser()):
660
self._get_parser()[location]={}
661
elif location + '/' in self._get_parser():
1117
parser = self._get_parser()
1118
if not location in parser and not location + '/' in parser:
1119
parser[location] = {}
1120
elif location + '/' in parser:
662
1121
location = location + '/'
663
self._get_parser()[location][option]=value
1122
parser[location][option]=value
664
1123
# the allowed values of store match the config policies
665
1124
self._set_option_policy(location, option, store)
666
self._get_parser().write(file(self._get_filename(), 'wb'))
1125
self._write_config_file()
669
1128
class BranchConfig(Config):
670
1129
"""A configuration object giving the policy for a branch."""
1131
def __init__(self, branch):
1132
super(BranchConfig, self).__init__()
1133
self._location_config = None
1134
self._branch_data_config = None
1135
self._global_config = None
1136
self.branch = branch
1137
self.option_sources = (self._get_location_config,
1138
self._get_branch_data_config,
1139
self._get_global_config)
1141
def config_id(self):
672
1144
def _get_branch_data_config(self):
673
1145
if self._branch_data_config is None:
674
1146
self._branch_data_config = TreeConfig(self.branch)
1147
self._branch_data_config.config_id = self.config_id
675
1148
return self._branch_data_config
677
1150
def _get_location_config(self):
1221
def _get_sections(self, name=None):
1222
"""See IniBasedConfig.get_sections()."""
1223
for source in self.option_sources:
1224
for section in source()._get_sections(name):
1227
def _get_options(self, sections=None):
1229
# First the locations options
1230
for option in self._get_location_config()._get_options():
1232
# Then the branch options
1233
branch_config = self._get_branch_data_config()
1234
if sections is None:
1235
sections = [('DEFAULT', branch_config._get_parser())]
1236
# FIXME: We shouldn't have to duplicate the code in IniBasedConfig but
1237
# Config itself has no notion of sections :( -- vila 20101001
1238
config_id = self.config_id()
1239
for (section_name, section) in sections:
1240
for (name, value) in section.iteritems():
1241
yield (name, value, section_name,
1242
config_id, branch_config._get_parser())
1243
# Then the global options
1244
for option in self._get_global_config()._get_options():
748
1247
def set_user_option(self, name, value, store=STORE_BRANCH,
749
1248
warn_masked=False):
750
1249
if store == STORE_BRANCH:
826
1318
def config_dir():
827
1319
"""Return per-user configuration directory.
829
By default this is ~/.bazaar/
1321
By default this is %APPDATA%/bazaar/2.0 on Windows, ~/.bazaar on Mac OS X
1322
and Linux. On Linux, if there is a $XDG_CONFIG_HOME/bazaar directory,
1323
that will be used instead.
831
1325
TODO: Global option --config-dir to override this.
833
1327
base = os.environ.get('BZR_HOME', None)
834
1328
if sys.platform == 'win32':
1329
# environ variables on Windows are in user encoding/mbcs. So decode
1331
if base is not None:
1332
base = base.decode('mbcs')
835
1333
if base is None:
836
1334
base = win32utils.get_appdata_location_unicode()
837
1335
if base is None:
838
1336
base = os.environ.get('HOME', None)
1337
if base is not None:
1338
base = base.decode('mbcs')
839
1339
if base is None:
840
1340
raise errors.BzrError('You must have one of BZR_HOME, APPDATA,'
842
1342
return osutils.pathjoin(base, 'bazaar', '2.0')
1343
elif sys.platform == 'darwin':
1345
# this takes into account $HOME
1346
base = os.path.expanduser("~")
1347
return osutils.pathjoin(base, '.bazaar')
844
# cygwin, linux, and darwin all have a $HOME directory
845
1349
if base is None:
1351
xdg_dir = os.environ.get('XDG_CONFIG_HOME', None)
1353
xdg_dir = osutils.pathjoin(os.path.expanduser("~"), ".config")
1354
xdg_dir = osutils.pathjoin(xdg_dir, 'bazaar')
1355
if osutils.isdir(xdg_dir):
1357
"Using configuration in XDG directory %s." % xdg_dir)
846
1360
base = os.path.expanduser("~")
847
1361
return osutils.pathjoin(base, ".bazaar")
899
1408
return os.path.expanduser('~/.cache')
1411
def _get_default_mail_domain():
1412
"""If possible, return the assumed default email domain.
1414
:returns: string mail domain, or None.
1416
if sys.platform == 'win32':
1417
# No implementation yet; patches welcome
1420
f = open('/etc/mailname')
1421
except (IOError, OSError), e:
1424
domain = f.read().strip()
902
1430
def _auto_user_id():
903
1431
"""Calculate automatic user identification.
905
Returns (realname, email).
1433
:returns: (realname, email), either of which may be None if they can't be
907
1436
Only used when none is set in the environment or the id file.
909
This previously used the FQDN as the default domain, but that can
910
be very slow on machines where DNS is broken. So now we simply
1438
This only returns an email address if we can be fairly sure the
1439
address is reasonable, ie if /etc/mailname is set on unix.
1441
This doesn't use the FQDN as the default domain because that may be
1442
slow, and it doesn't use the hostname alone because that's not normally
1443
a reasonable address.
915
1445
if sys.platform == 'win32':
916
name = win32utils.get_user_name_unicode()
918
raise errors.BzrError("Cannot autodetect user name.\n"
919
"Please, set your name with command like:\n"
920
'bzr whoami "Your Name <name@domain.com>"')
921
host = win32utils.get_host_name_unicode()
923
host = socket.gethostname()
924
return name, (name + '@' + host)
930
w = pwd.getpwuid(uid)
932
raise errors.BzrCommandError('Unable to determine your name. '
933
'Please use "bzr whoami" to set it.')
935
# we try utf-8 first, because on many variants (like Linux),
936
# /etc/passwd "should" be in utf-8, and because it's unlikely to give
937
# false positives. (many users will have their user encoding set to
938
# latin-1, which cannot raise UnicodeError.)
940
gecos = w.pw_gecos.decode('utf-8')
944
encoding = osutils.get_user_encoding()
945
gecos = w.pw_gecos.decode(encoding)
947
raise errors.BzrCommandError('Unable to determine your name. '
948
'Use "bzr whoami" to set it.')
950
username = w.pw_name.decode(encoding)
952
raise errors.BzrCommandError('Unable to determine your name. '
953
'Use "bzr whoami" to set it.')
955
comma = gecos.find(',')
959
realname = gecos[:comma]
966
user_encoding = osutils.get_user_encoding()
967
realname = username = getpass.getuser().decode(user_encoding)
968
except UnicodeDecodeError:
969
raise errors.BzrError("Can't decode username as %s." % \
972
return realname, (username + '@' + socket.gethostname())
1446
# No implementation to reliably determine Windows default mail
1447
# address; please add one.
1450
default_mail_domain = _get_default_mail_domain()
1451
if not default_mail_domain:
1457
w = pwd.getpwuid(uid)
1459
mutter('no passwd entry for uid %d?' % uid)
1462
# we try utf-8 first, because on many variants (like Linux),
1463
# /etc/passwd "should" be in utf-8, and because it's unlikely to give
1464
# false positives. (many users will have their user encoding set to
1465
# latin-1, which cannot raise UnicodeError.)
1467
gecos = w.pw_gecos.decode('utf-8')
1469
except UnicodeError:
1471
encoding = osutils.get_user_encoding()
1472
gecos = w.pw_gecos.decode(encoding)
1473
except UnicodeError, e:
1474
mutter("cannot decode passwd entry %s" % w)
1477
username = w.pw_name.decode(encoding)
1478
except UnicodeError, e:
1479
mutter("cannot decode passwd entry %s" % w)
1482
comma = gecos.find(',')
1486
realname = gecos[:comma]
1488
return realname, (username + '@' + default_mail_domain)
975
1491
def parse_username(username):
1517
2056
return StringIO()
1519
2058
def _get_configobj(self):
1520
return ConfigObj(self._get_config_file(), encoding='utf-8')
2059
f = self._get_config_file()
2061
return ConfigObj(f, encoding='utf-8')
1522
2065
def _set_configobj(self, configobj):
1523
2066
out_file = StringIO()
1524
2067
configobj.write(out_file)
1525
2068
out_file.seek(0)
1526
2069
self._transport.put_file(self._filename, out_file)
2072
class cmd_config(commands.Command):
2073
__doc__ = """Display, set or remove a configuration option.
2075
Display the active value for a given option.
2077
If --all is specified, NAME is interpreted as a regular expression and all
2078
matching options are displayed mentioning their scope. The active value
2079
that bzr will take into account is the first one displayed for each option.
2081
If no NAME is given, --all .* is implied.
2083
Setting a value is achieved by using name=value without spaces. The value
2084
is set in the most relevant scope and can be checked by displaying the
2088
takes_args = ['name?']
2092
# FIXME: This should be a registry option so that plugins can register
2093
# their own config files (or not) -- vila 20101002
2094
commands.Option('scope', help='Reduce the scope to the specified'
2095
' configuration file',
2097
commands.Option('all',
2098
help='Display all the defined values for the matching options.',
2100
commands.Option('remove', help='Remove the option from'
2101
' the configuration file'),
2104
@commands.display_command
2105
def run(self, name=None, all=False, directory=None, scope=None,
2107
if directory is None:
2109
directory = urlutils.normalize_url(directory)
2111
raise errors.BzrError(
2112
'--all and --remove are mutually exclusive.')
2114
# Delete the option in the given scope
2115
self._remove_config_option(name, directory, scope)
2117
# Defaults to all options
2118
self._show_matching_options('.*', directory, scope)
2121
name, value = name.split('=', 1)
2123
# Display the option(s) value(s)
2125
self._show_matching_options(name, directory, scope)
2127
self._show_value(name, directory, scope)
2130
raise errors.BzrError(
2131
'Only one option can be set.')
2132
# Set the option value
2133
self._set_config_option(name, value, directory, scope)
2135
def _get_configs(self, directory, scope=None):
2136
"""Iterate the configurations specified by ``directory`` and ``scope``.
2138
:param directory: Where the configurations are derived from.
2140
:param scope: A specific config to start from.
2142
if scope is not None:
2143
if scope == 'bazaar':
2144
yield GlobalConfig()
2145
elif scope == 'locations':
2146
yield LocationConfig(directory)
2147
elif scope == 'branch':
2148
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
2150
yield br.get_config()
2153
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
2155
yield br.get_config()
2156
except errors.NotBranchError:
2157
yield LocationConfig(directory)
2158
yield GlobalConfig()
2160
def _show_value(self, name, directory, scope):
2162
for c in self._get_configs(directory, scope):
2165
for (oname, value, section, conf_id, parser) in c._get_options():
2167
# Display only the first value and exit
2169
# FIXME: We need to use get_user_option to take policies
2170
# into account and we need to make sure the option exists
2171
# too (hence the two for loops), this needs a better API
2173
value = c.get_user_option(name)
2174
# Quote the value appropriately
2175
value = parser._quote(value)
2176
self.outf.write('%s\n' % (value,))
2180
raise errors.NoSuchConfigOption(name)
2182
def _show_matching_options(self, name, directory, scope):
2183
name = re.compile(name)
2184
# We want any error in the regexp to be raised *now* so we need to
2185
# avoid the delay introduced by the lazy regexp.
2186
name._compile_and_collapse()
2189
for c in self._get_configs(directory, scope):
2190
for (oname, value, section, conf_id, parser) in c._get_options():
2191
if name.search(oname):
2192
if cur_conf_id != conf_id:
2193
# Explain where the options are defined
2194
self.outf.write('%s:\n' % (conf_id,))
2195
cur_conf_id = conf_id
2197
if (section not in (None, 'DEFAULT')
2198
and cur_section != section):
2199
# Display the section if it's not the default (or only)
2201
self.outf.write(' [%s]\n' % (section,))
2202
cur_section = section
2203
self.outf.write(' %s = %s\n' % (oname, value))
2205
def _set_config_option(self, name, value, directory, scope):
2206
for conf in self._get_configs(directory, scope):
2207
conf.set_user_option(name, value)
2210
raise errors.NoSuchConfig(scope)
2212
def _remove_config_option(self, name, directory, scope):
2214
raise errors.BzrCommandError(
2215
'--remove expects an option to remove.')
2217
for conf in self._get_configs(directory, scope):
2218
for (section_name, section, conf_id) in conf._get_sections():
2219
if scope is not None and conf_id != scope:
2220
# Not the right configuration file
2223
if conf_id != conf.config_id():
2224
conf = self._get_configs(directory, conf_id).next()
2225
# We use the first section in the first config where the
2226
# option is defined to remove it
2227
conf.remove_user_option(name, section_name)
2232
raise errors.NoSuchConfig(scope)
2234
raise errors.NoSuchConfigOption(name)