~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Matt Nordhoff
  • Date: 2009-04-04 02:50:01 UTC
  • mfrom: (4253 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4256.
  • Revision ID: mnordhoff@mattnordhoff.com-20090404025001-z1403k0tatmc8l91
Merge bzr.dev, fixing conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#
15
15
# You should have received a copy of the GNU General Public License
16
16
# along with this program; if not, write to the Free Software
17
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
 
19
19
"""Configuration that affects the behaviour of Bazaar.
20
20
 
37
37
[/home/robertc/source]
38
38
recurse=False|True(default)
39
39
email= as above
40
 
check_signatures= as above 
 
40
check_signatures= as above
41
41
create_signatures= as above.
42
42
 
43
43
explanation of options
45
45
editor - this option sets the pop up editor to use during commits.
46
46
email - this option sets the user id bzr will use when committing.
47
47
check_signatures - this option controls whether bzr will require good gpg
48
 
                   signatures, ignore them, or check them if they are 
 
48
                   signatures, ignore them, or check them if they are
49
49
                   present.
50
 
create_signatures - this option controls whether bzr will always create 
 
50
create_signatures - this option controls whether bzr will always create
51
51
                    gpg signatures, never create them, or create them if the
52
52
                    branch is configured to require them.
53
53
log_format - this option sets the default log format.  Possible values are
216
216
 
217
217
    def username(self):
218
218
        """Return email-style username.
219
 
    
 
219
 
220
220
        Something similar to 'Martin Pool <mbp@sourcefrog.net>'
221
 
        
 
221
 
222
222
        $BZR_EMAIL can be set to override this (as well as the
223
223
        deprecated $BZREMAIL), then
224
224
        the concrete policy type is checked, and finally
225
225
        $EMAIL is examined.
226
226
        If none is found, a reasonable default is (hopefully)
227
227
        created.
228
 
    
 
228
 
229
229
        TODO: Check it's reasonably well-formed.
230
230
        """
231
231
        v = os.environ.get('BZR_EMAIL')
385
385
        super(IniBasedConfig, self).__init__()
386
386
        self._get_filename = get_filename
387
387
        self._parser = None
388
 
        
 
388
 
389
389
    def _post_commit(self):
390
390
        """See Config.post_commit."""
391
391
        return self._get_user_option('post_commit')
414
414
 
415
415
    def _get_alias(self, value):
416
416
        try:
417
 
            return self._get_parser().get_value("ALIASES", 
 
417
            return self._get_parser().get_value("ALIASES",
418
418
                                                value)
419
419
        except KeyError:
420
420
            pass
642
642
 
643
643
    def _get_safe_value(self, option_name):
644
644
        """This variant of get_best_value never returns untrusted values.
645
 
        
 
645
 
646
646
        It does not return values from the branch data, because the branch may
647
647
        not be controlled by the user.
648
648
 
657
657
 
658
658
    def _get_user_id(self):
659
659
        """Return the full user id for the branch.
660
 
    
 
660
 
661
661
        e.g. "John Hacker <jhacker@example.com>"
662
662
        This is looked up in the email controlfile for the branch.
663
663
        """
667
667
                    .rstrip("\r\n"))
668
668
        except errors.NoSuchFile, e:
669
669
            pass
670
 
        
 
670
 
671
671
        return self._get_best_value('_get_user_id')
672
672
 
673
673
    def _get_signature_checking(self):
709
709
                        trace.warning('Value "%s" is masked by "%s" from'
710
710
                                      ' branch.conf', value, mask_value)
711
711
 
712
 
 
713
712
    def _gpg_signing_command(self):
714
713
        """See Config.gpg_signing_command."""
715
714
        return self._get_safe_value('_gpg_signing_command')
716
 
        
 
715
 
717
716
    def __init__(self, branch):
718
717
        super(BranchConfig, self).__init__()
719
718
        self._location_config = None
720
719
        self._branch_data_config = None
721
720
        self._global_config = None
722
721
        self.branch = branch
723
 
        self.option_sources = (self._get_location_config, 
 
722
        self.option_sources = (self._get_location_config,
724
723
                               self._get_branch_data_config,
725
724
                               self._get_global_config)
726
725
 
768
767
    """Return per-user configuration directory.
769
768
 
770
769
    By default this is ~/.bazaar/
771
 
    
 
770
 
772
771
    TODO: Global option --config-dir to override this.
773
772
    """
774
773
    base = os.environ.get('BZR_HOME', None)
898
897
def extract_email_address(e):
899
898
    """Return just the address part of an email string.
900
899
 
901
 
    That is just the user@domain part, nothing else. 
 
900
    That is just the user@domain part, nothing else.
902
901
    This part is required to contain only ascii characters.
903
902
    If it can't be extracted, raises an error.
904
903
 
917
916
    # XXX: Really needs a better name, as this is not part of the tree! -- mbp 20080507
918
917
 
919
918
    def __init__(self, branch):
920
 
        # XXX: Really this should be asking the branch for its configuration
921
 
        # data, rather than relying on a Transport, so that it can work 
922
 
        # more cleanly with a RemoteBranch that has no transport.
923
 
        self._config = TransportConfig(branch._transport, 'branch.conf')
 
919
        self._config = branch._get_config()
924
920
        self.branch = branch
925
921
 
926
922
    def _get_parser(self, file=None):
993
989
        section[option_name] = value
994
990
        self._save()
995
991
 
996
 
    def get_credentials(self, scheme, host, port=None, user=None, path=None):
 
992
    def get_credentials(self, scheme, host, port=None, user=None, path=None, 
 
993
                        realm=None):
997
994
        """Returns the matching credentials from authentication.conf file.
998
995
 
999
996
        :param scheme: protocol
1005
1002
        :param user: login (optional)
1006
1003
 
1007
1004
        :param path: the absolute path on the server (optional)
 
1005
        
 
1006
        :param realm: the http authentication realm (optional)
1008
1007
 
1009
1008
        :return: A dict containing the matching credentials or None.
1010
1009
           This includes:
1011
1010
           - name: the section name of the credentials in the
1012
1011
             authentication.conf file,
1013
 
           - user: can't de different from the provided user if any,
 
1012
           - user: can't be different from the provided user if any,
 
1013
           - scheme: the server protocol,
 
1014
           - host: the server address,
 
1015
           - port: the server port (can be None),
 
1016
           - path: the absolute server path (can be None),
 
1017
           - realm: the http specific authentication realm (can be None),
1014
1018
           - password: the decoded password, could be None if the credential
1015
1019
             defines only the user
1016
1020
           - verify_certificates: https specific, True if the server
1057
1061
            if a_user is None:
1058
1062
                # Can't find a user
1059
1063
                continue
 
1064
            # Prepare a credentials dictionary with additional keys
 
1065
            # for the credential providers
1060
1066
            credentials = dict(name=auth_def_name,
1061
1067
                               user=a_user,
 
1068
                               scheme=a_scheme,
 
1069
                               host=host,
 
1070
                               port=port,
 
1071
                               path=path,
 
1072
                               realm=realm,
1062
1073
                               password=auth_def.get('password', None),
1063
1074
                               verify_certificates=a_verify_certificates)
 
1075
            # Decode the password in the credentials (or get one)
1064
1076
            self.decode_password(credentials,
1065
1077
                                 auth_def.get('password_encoding', None))
1066
1078
            if 'auth' in debug.debug_flags:
1070
1082
        return credentials
1071
1083
 
1072
1084
    def set_credentials(self, name, host, user, scheme=None, password=None,
1073
 
                        port=None, path=None, verify_certificates=None):
 
1085
                        port=None, path=None, verify_certificates=None,
 
1086
                        realm=None):
1074
1087
        """Set authentication credentials for a host.
1075
1088
 
1076
1089
        Any existing credentials with matching scheme, host, port and path
1087
1100
            apply to.
1088
1101
        :param verify_certificates: On https, verify server certificates if
1089
1102
            True.
 
1103
        :param realm: The http authentication realm (optional).
1090
1104
        """
1091
1105
        values = {'host': host, 'user': user}
1092
1106
        if password is not None:
1099
1113
            values['path'] = path
1100
1114
        if verify_certificates is not None:
1101
1115
            values['verify_certificates'] = str(verify_certificates)
 
1116
        if realm is not None:
 
1117
            values['realm'] = realm
1102
1118
        config = self._get_config()
1103
1119
        for_deletion = []
1104
1120
        for section, existing_values in config.items():
1105
 
            for key in ('scheme', 'host', 'port', 'path'):
 
1121
            for key in ('scheme', 'host', 'port', 'path', 'realm'):
1106
1122
                if existing_values.get(key) != values.get(key):
1107
1123
                    break
1108
1124
            else:
1127
1143
        :return: The found user.
1128
1144
        """
1129
1145
        credentials = self.get_credentials(scheme, host, port, user=None,
1130
 
                                           path=path)
 
1146
                                           path=path, realm=realm)
1131
1147
        if credentials is not None:
1132
1148
            user = credentials['user']
1133
1149
        else:
1152
1168
 
1153
1169
        :return: The found password or the one entered by the user.
1154
1170
        """
1155
 
        credentials = self.get_credentials(scheme, host, port, user, path)
 
1171
        credentials = self.get_credentials(scheme, host, port, user, path,
 
1172
                                           realm)
1156
1173
        if credentials is not None:
1157
1174
            password = credentials['password']
1158
1175
            if password is not None and scheme is 'ssh':