~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

Merge prerequisite branch and tweak test to be more compact and faster.

Show diffs side-by-side

added added

removed removed

Lines of Context:
260
260
        $BZR_EMAIL can be set to override this, then
261
261
        the concrete policy type is checked, and finally
262
262
        $EMAIL is examined.
263
 
        If none is found, a reasonable default is (hopefully)
264
 
        created.
 
263
        If no username can be found, errors.NoWhoami exception is raised.
265
264
 
266
265
        TODO: Check it's reasonably well-formed.
267
266
        """
280
279
        raise errors.NoWhoami()
281
280
 
282
281
    def ensure_username(self):
283
 
        """Raise BzrCommandError if username is not set.
 
282
        """Raise errors.NoWhoami if username is not set.
284
283
 
285
284
        This method relies on the username() function raising the error.
286
285
        """
478
477
    def _get_nickname(self):
479
478
        return self.get_user_option('nickname')
480
479
 
 
480
    def _write_config_file(self):
 
481
        f = file(self._get_filename(), "wb")
 
482
        try:
 
483
            osutils.copy_ownership_from_path(f.name)
 
484
            self._get_parser().write(f)
 
485
        finally:
 
486
            f.close()
 
487
 
481
488
 
482
489
class GlobalConfig(IniBasedConfig):
483
490
    """The configuration that should be used for a specific location."""
519
526
        self._get_parser().setdefault(section, {})[option] = value
520
527
        self._write_config_file()
521
528
 
522
 
    def _write_config_file(self):
523
 
        path = self._get_filename()
524
 
        f = open(path, 'wb')
525
 
        osutils.copy_ownership_from_path(path)
526
 
        self._get_parser().write(f)
527
 
        f.close()
528
 
 
529
529
 
530
530
class LocationConfig(IniBasedConfig):
531
531
    """A configuration object that gives the policy for a location."""
665
665
        self._get_parser()[location][option]=value
666
666
        # the allowed values of store match the config policies
667
667
        self._set_option_policy(location, option, store)
668
 
        self._get_parser().write(file(self._get_filename(), 'wb'))
 
668
        self._write_config_file()
669
669
 
670
670
 
671
671
class BranchConfig(Config):
843
843
                                  ' or HOME set')
844
844
        return osutils.pathjoin(base, 'bazaar', '2.0')
845
845
    else:
846
 
        # cygwin, linux, and darwin all have a $HOME directory
847
846
        if base is None:
848
847
            base = os.path.expanduser("~")
849
848
        return osutils.pathjoin(base, ".bazaar")
992
991
        """Save the config file, only tests should use it for now."""
993
992
        conf_dir = os.path.dirname(self._filename)
994
993
        ensure_config_dir_exists(conf_dir)
995
 
        self._get_config().write(file(self._filename, 'wb'))
 
994
        f = file(self._filename, 'wb')
 
995
        try:
 
996
            self._get_config().write(f)
 
997
        finally:
 
998
            f.close()
996
999
 
997
1000
    def _set_option(self, section_name, option_name, value):
998
1001
        """Set an authentication configuration option"""
1446
1449
            return StringIO()
1447
1450
 
1448
1451
    def _get_configobj(self):
1449
 
        return ConfigObj(self._get_config_file(), encoding='utf-8')
 
1452
        f = self._get_config_file()
 
1453
        try:
 
1454
            return ConfigObj(f, encoding='utf-8')
 
1455
        finally:
 
1456
            f.close()
1450
1457
 
1451
1458
    def _set_configobj(self, configobj):
1452
1459
        out_file = StringIO()