258
259
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
261
$BZR_EMAIL can be set to override this, then
262
262
the concrete policy type is checked, and finally
263
263
$EMAIL is examined.
264
If none is found, a reasonable default is (hopefully)
264
If no username can be found, errors.NoWhoami exception is raised.
267
266
TODO: Check it's reasonably well-formed.
279
278
return v.decode(osutils.get_user_encoding())
281
name, email = _auto_user_id()
283
return '%s <%s>' % (name, email)
280
raise errors.NoWhoami()
282
def ensure_username(self):
283
"""Raise errors.NoWhoami if username is not set.
285
This method relies on the username() function raising the error.
287
289
def signature_checking(self):
288
290
"""What is the current policy for signature checking?."""
476
478
def _get_nickname(self):
477
479
return self.get_user_option('nickname')
481
def _write_config_file(self):
482
filename = self._get_filename()
483
file_existed = os.path.isfile(filename)
484
atomic_file = atomicfile.AtomicFile(filename)
485
self._get_parser().write(atomic_file)
489
osutils.copy_ownership_from_path(filename)
480
492
class GlobalConfig(IniBasedConfig):
481
493
"""The configuration that should be used for a specific location."""
517
529
self._get_parser().setdefault(section, {})[option] = value
518
530
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
533
class LocationConfig(IniBasedConfig):
529
534
"""A configuration object that gives the policy for a location."""
663
668
self._get_parser()[location][option]=value
664
669
# the allowed values of store match the config policies
665
670
self._set_option_policy(location, option, store)
666
self._get_parser().write(file(self._get_filename(), 'wb'))
671
self._write_config_file()
669
674
class BranchConfig(Config):
842
847
return osutils.pathjoin(base, 'bazaar', '2.0')
844
# cygwin, linux, and darwin all have a $HOME directory
846
850
base = os.path.expanduser("~")
847
851
return osutils.pathjoin(base, ".bazaar")
899
903
return os.path.expanduser('~/.cache')
903
"""Calculate automatic user identification.
905
Returns (realname, email).
907
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
915
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())
975
906
def parse_username(username):
976
907
"""Parse e-mail username and return a (name, address) tuple."""
977
908
match = re.match(r'(.*?)\s*<?([\w+.-]+@[\w+.-]+)>?', username)
1063
994
"""Save the config file, only tests should use it for now."""
1064
995
conf_dir = os.path.dirname(self._filename)
1065
996
ensure_config_dir_exists(conf_dir)
1066
self._get_config().write(file(self._filename, 'wb'))
997
f = file(self._filename, 'wb')
999
self._get_config().write(f)
1068
1003
def _set_option(self, section_name, option_name, value):
1069
1004
"""Set an authentication configuration option"""
1517
1452
return StringIO()
1519
1454
def _get_configobj(self):
1520
return ConfigObj(self._get_config_file(), encoding='utf-8')
1455
f = self._get_config_file()
1457
return ConfigObj(f, encoding='utf-8')
1522
1461
def _set_configobj(self, configobj):
1523
1462
out_file = StringIO()