58
57
from fnmatch import fnmatch
62
61
import bzrlib.errors as errors
63
from bzrlib.osutils import pathjoin
64
from bzrlib.trace import mutter
65
62
import bzrlib.util.configobj.configobj as configobj
66
63
from StringIO import StringIO
137
134
def user_email(self):
138
135
"""Return just the email component of a username."""
139
return extract_email_address(self.username())
137
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
139
raise BzrError("%r doesn't seem to contain "
140
"a reasonable email address" % e)
141
143
def username(self):
142
144
"""Return email-style username.
146
148
$BZREMAIL can be set to override this, then
147
149
the concrete policy type is checked, and finally
149
If none is found, a reasonable default is (hopefully)
151
but if none is found, a reasonable default is (hopefully)
152
154
TODO: Check it's reasonably well-formed.
350
352
"""Save option and its value in the configuration."""
351
353
# FIXME: RBC 20051029 This should refresh the parser and also take a
352
354
# file lock on branches.conf.
353
conf_dir = os.path.dirname(self._get_filename())
354
ensure_config_dir_exists(conf_dir)
355
if not os.path.isdir(os.path.dirname(self._get_filename())):
356
os.mkdir(os.path.dirname(self._get_filename()))
355
357
location = self.location
356
358
if location.endswith('/'):
357
359
location = location[:-1]
410
412
return self._get_location_config()._post_commit()
413
def ensure_config_dir_exists(path=None):
414
"""Make sure a configuration directory exists.
415
This makes sure that the directory exists.
416
On windows, since configuration directories are 2 levels deep,
417
it makes sure both the directory and the parent directory exists.
421
if not os.path.isdir(path):
422
if sys.platform == 'win32':
423
parent_dir = os.path.dirname(path)
424
if not os.path.isdir(parent_dir):
425
mutter('creating config parent directory: %r', parent_dir)
427
mutter('creating config directory: %r', path)
431
415
def config_dir():
432
416
"""Return per-user configuration directory.
436
420
TODO: Global option --config-dir to override this.
438
base = os.environ.get('BZR_HOME', None)
439
if sys.platform == 'win32':
441
base = os.environ.get('APPDATA', None)
443
base = os.environ.get('HOME', None)
445
raise BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
446
return pathjoin(base, 'bazaar', '2.0')
448
# cygwin, linux, and darwin all have a $HOME directory
450
base = os.path.expanduser("~")
451
return pathjoin(base, ".bazaar")
422
return os.path.join(os.path.expanduser("~"), ".bazaar")
454
425
def config_filename():
455
426
"""Return per-user configuration ini file filename."""
456
return pathjoin(config_dir(), 'bazaar.conf')
427
return os.path.join(config_dir(), 'bazaar.conf')
459
430
def branches_config_filename():
460
431
"""Return per-user configuration ini file filename."""
461
return pathjoin(config_dir(), 'branches.conf')
432
return os.path.join(config_dir(), 'branches.conf')
464
435
def _auto_user_id():
510
481
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
512
raise errors.BzrError("%r doesn't seem to contain "
513
"a reasonable email address" % e)
483
raise BzrError("%r doesn't seem to contain "
484
"a reasonable email address" % e)
514
485
return m.group(0)
516
487
class TreeConfig(object):
555
525
cfg_obj[section] = {}
556
526
obj = cfg_obj[section]
557
527
obj[name] = value
558
cfg_obj.encode('UTF-8')
559
528
out_file = StringIO(''.join([l+'\n' for l in cfg_obj.write()]))
561
self.branch.put_controlfile('branch.conf', out_file, encode=False)
530
self.branch.put_controlfile('branch.conf', out_file, encode=True)
563
532
self.branch.unlock()