/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Vincent Ladeuil
  • Date: 2009-09-02 08:26:27 UTC
  • mto: (4669.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4670.
  • Revision ID: v.ladeuil+lp@free.fr-20090902082627-cit8vm6tefu9hwk2
Cleanup emacs-bzr-send-XXXXXX.el leaks in /tmp during selftest.

* tests/test_mail_client.py:
(TestEmacsMail.test_commandline,
TestEmacsMail.test_commandline_is_8bit): Cleanup the generated tmp
file.

* mail_client.py:
(EmacsMail.__init__, EmacsMail._get_compose_commandline): Keep
track of the tmp file to ease cleanup during testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2007, 2008 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#            and others
4
4
#
153
153
        """Get the users pop up editor."""
154
154
        raise NotImplementedError
155
155
 
156
 
    def get_change_editor(self, old_tree, new_tree):
157
 
        from bzrlib import diff
158
 
        cmd = self._get_change_editor()
159
 
        if cmd is None:
160
 
            return None
161
 
        return diff.DiffFromTool.from_string(cmd, old_tree, new_tree,
162
 
                                             sys.stdout)
163
 
 
164
 
 
165
156
    def get_mail_client(self):
166
157
        """Get a mail client to use"""
167
158
        selected_client = self.get_user_option('mail_client')
190
181
        """Get a generic option as a boolean - no special process, no default.
191
182
 
192
183
        :return None if the option doesn't exist or its value can't be
193
 
            interpreted as a boolean. Returns True or False otherwise.
 
184
            interpreted as a boolean. Returns True or False ortherwise.
194
185
        """
195
186
        s = self._get_user_option(option_name)
196
 
        if s is None:
197
 
            # The option doesn't exist
198
 
            return None
199
 
        val = ui.bool_from_string(s)
200
 
        if val is None:
201
 
            # The value can't be interpreted as a boolean
202
 
            trace.warning('Value "%s" is not a boolean for "%s"',
203
 
                          s, option_name)
204
 
        return val
205
 
 
206
 
    def get_user_option_as_list(self, option_name):
207
 
        """Get a generic option as a list - no special process, no default.
208
 
 
209
 
        :return None if the option doesn't exist. Returns the value as a list
210
 
            otherwise.
211
 
        """
212
 
        l = self._get_user_option(option_name)
213
 
        if isinstance(l, (str, unicode)):
214
 
            # A single value, most probably the user forgot the final ','
215
 
            l = [l]
216
 
        return l
 
187
        return ui.bool_from_string(s)
217
188
 
218
189
    def gpg_signing_command(self):
219
190
        """What program should be used to sign signatures?"""
257
228
 
258
229
        Something similar to 'Martin Pool <mbp@sourcefrog.net>'
259
230
 
260
 
        $BZR_EMAIL can be set to override this, then
 
231
        $BZR_EMAIL can be set to override this (as well as the
 
232
        deprecated $BZREMAIL), then
261
233
        the concrete policy type is checked, and finally
262
234
        $EMAIL is examined.
263
 
        If no username can be found, errors.NoWhoami exception is raised.
 
235
        If none is found, a reasonable default is (hopefully)
 
236
        created.
264
237
 
265
238
        TODO: Check it's reasonably well-formed.
266
239
        """
276
249
        if v:
277
250
            return v.decode(osutils.get_user_encoding())
278
251
 
279
 
        raise errors.NoWhoami()
280
 
 
281
 
    def ensure_username(self):
282
 
        """Raise errors.NoWhoami if username is not set.
283
 
 
284
 
        This method relies on the username() function raising the error.
285
 
        """
286
 
        self.username()
 
252
        name, email = _auto_user_id()
 
253
        if name:
 
254
            return '%s <%s>' % (name, email)
 
255
        else:
 
256
            return email
287
257
 
288
258
    def signature_checking(self):
289
259
        """What is the current policy for signature checking?."""
334
304
                path = 'bzr'
335
305
            return path
336
306
 
337
 
    def suppress_warning(self, warning):
338
 
        """Should the warning be suppressed or emitted.
339
 
 
340
 
        :param warning: The name of the warning being tested.
341
 
 
342
 
        :returns: True if the warning should be suppressed, False otherwise.
343
 
        """
344
 
        warnings = self.get_user_option_as_list('suppress_warnings')
345
 
        if warnings is None or warning not in warnings:
346
 
            return False
347
 
        else:
348
 
            return True
349
 
 
350
307
 
351
308
class IniBasedConfig(Config):
352
309
    """A configuration policy that draws from ini files."""
389
346
        """Return the policy for the given (section, option_name) pair."""
390
347
        return POLICY_NONE
391
348
 
392
 
    def _get_change_editor(self):
393
 
        return self.get_user_option('change_editor')
394
 
 
395
349
    def _get_signature_checking(self):
396
350
        """See Config._get_signature_checking."""
397
351
        policy = self._get_user_option('check_signatures')
519
473
        self._write_config_file()
520
474
 
521
475
    def _write_config_file(self):
522
 
        path = self._get_filename()
523
 
        f = open(path, 'wb')
524
 
        osutils.copy_ownership_from_path(path)
 
476
        f = open(self._get_filename(), 'wb')
525
477
        self._get_parser().write(f)
526
478
        f.close()
527
479
 
727
679
 
728
680
        return self._get_best_value('_get_user_id')
729
681
 
730
 
    def _get_change_editor(self):
731
 
        return self._get_best_value('_get_change_editor')
732
 
 
733
682
    def _get_signature_checking(self):
734
683
        """See Config._get_signature_checking."""
735
684
        return self._get_best_value('_get_signature_checking')
821
770
            os.mkdir(parent_dir)
822
771
        trace.mutter('creating config directory: %r', path)
823
772
        os.mkdir(path)
824
 
        osutils.copy_ownership_from_path(path)
825
773
 
826
774
 
827
775
def config_dir():
878
826
 
879
827
    This doesn't implicitly create it.
880
828
 
881
 
    On Windows it's in the config directory; elsewhere it's /var/crash
882
 
    which may be monitored by apport.  It can be overridden by
883
 
    $APPORT_CRASH_DIR.
 
829
    On Windows it's in the config directory; elsewhere in the XDG cache directory.
884
830
    """
885
831
    if sys.platform == 'win32':
886
832
        return osutils.pathjoin(config_dir(), 'Crash')
887
833
    else:
888
 
        # XXX: hardcoded in apport_python_hook.py; therefore here too -- mbp
889
 
        # 2010-01-31
890
 
        return os.environ.get('APPORT_CRASH_DIR', '/var/crash')
 
834
        return osutils.pathjoin(xdg_cache_dir(), 'crash')
891
835
 
892
836
 
893
837
def xdg_cache_dir():
900
844
        return os.path.expanduser('~/.cache')
901
845
 
902
846
 
 
847
def _auto_user_id():
 
848
    """Calculate automatic user identification.
 
849
 
 
850
    Returns (realname, email).
 
851
 
 
852
    Only used when none is set in the environment or the id file.
 
853
 
 
854
    This previously used the FQDN as the default domain, but that can
 
855
    be very slow on machines where DNS is broken.  So now we simply
 
856
    use the hostname.
 
857
    """
 
858
    import socket
 
859
 
 
860
    if sys.platform == 'win32':
 
861
        name = win32utils.get_user_name_unicode()
 
862
        if name is None:
 
863
            raise errors.BzrError("Cannot autodetect user name.\n"
 
864
                                  "Please, set your name with command like:\n"
 
865
                                  'bzr whoami "Your Name <name@domain.com>"')
 
866
        host = win32utils.get_host_name_unicode()
 
867
        if host is None:
 
868
            host = socket.gethostname()
 
869
        return name, (name + '@' + host)
 
870
 
 
871
    try:
 
872
        import pwd
 
873
        uid = os.getuid()
 
874
        try:
 
875
            w = pwd.getpwuid(uid)
 
876
        except KeyError:
 
877
            raise errors.BzrCommandError('Unable to determine your name.  '
 
878
                'Please use "bzr whoami" to set it.')
 
879
 
 
880
        # we try utf-8 first, because on many variants (like Linux),
 
881
        # /etc/passwd "should" be in utf-8, and because it's unlikely to give
 
882
        # false positives.  (many users will have their user encoding set to
 
883
        # latin-1, which cannot raise UnicodeError.)
 
884
        try:
 
885
            gecos = w.pw_gecos.decode('utf-8')
 
886
            encoding = 'utf-8'
 
887
        except UnicodeError:
 
888
            try:
 
889
                encoding = osutils.get_user_encoding()
 
890
                gecos = w.pw_gecos.decode(encoding)
 
891
            except UnicodeError:
 
892
                raise errors.BzrCommandError('Unable to determine your name.  '
 
893
                   'Use "bzr whoami" to set it.')
 
894
        try:
 
895
            username = w.pw_name.decode(encoding)
 
896
        except UnicodeError:
 
897
            raise errors.BzrCommandError('Unable to determine your name.  '
 
898
                'Use "bzr whoami" to set it.')
 
899
 
 
900
        comma = gecos.find(',')
 
901
        if comma == -1:
 
902
            realname = gecos
 
903
        else:
 
904
            realname = gecos[:comma]
 
905
        if not realname:
 
906
            realname = username
 
907
 
 
908
    except ImportError:
 
909
        import getpass
 
910
        try:
 
911
            user_encoding = osutils.get_user_encoding()
 
912
            realname = username = getpass.getuser().decode(user_encoding)
 
913
        except UnicodeDecodeError:
 
914
            raise errors.BzrError("Can't decode username as %s." % \
 
915
                    user_encoding)
 
916
 
 
917
    return realname, (username + '@' + socket.gethostname())
 
918
 
 
919
 
903
920
def parse_username(username):
904
921
    """Parse e-mail username and return a (name, address) tuple."""
905
922
    match = re.match(r'(.*?)\s*<?([\w+.-]+@[\w+.-]+)>?', username)
1345
1362
 
1346
1363
 
1347
1364
class PlainTextCredentialStore(CredentialStore):
1348
 
    __doc__ = """Plain text credential store for the authentication.conf file"""
 
1365
    """Plain text credential store for the authentication.conf file."""
1349
1366
 
1350
1367
    def decode_password(self, credentials):
1351
1368
        """See CredentialStore.decode_password."""
1440
1457
 
1441
1458
    def _get_config_file(self):
1442
1459
        try:
1443
 
            return StringIO(self._transport.get_bytes(self._filename))
 
1460
            return self._transport.get(self._filename)
1444
1461
        except errors.NoSuchFile:
1445
1462
            return StringIO()
1446
1463