~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/gpg.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-20 23:43:18 UTC
  • mto: This revision was merged to the branch mainline in revision 2027.
  • Revision ID: john@arbash-meinel.com-20060920234318-05de23e98f7e7876
Move out export tests from test_too_much, refactor
and re-enable doc tests for export.get_root_name()

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
#   Authors: Robert Collins <robert.collins@canonical.com>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
"""GPG signing and checking logic."""
 
19
 
 
20
import errno
 
21
import os
 
22
import subprocess
 
23
import sys
 
24
 
 
25
from bzrlib import (
 
26
    errors,
 
27
    trace,
 
28
    ui,
 
29
    )
 
30
 
 
31
 
 
32
class DisabledGPGStrategy(object):
 
33
    """A GPG Strategy that makes everything fail."""
 
34
 
 
35
    def __init__(self, ignored):
 
36
        """Real strategies take a configuration."""
 
37
 
 
38
    def sign(self, content):
 
39
        raise errors.SigningFailed('Signing is disabled.')
 
40
 
 
41
 
 
42
class LoopbackGPGStrategy(object):
 
43
    """A GPG Strategy that acts like 'cat' - data is just passed through."""
 
44
 
 
45
    def __init__(self, ignored):
 
46
        """Real strategies take a configuration."""
 
47
 
 
48
    def sign(self, content):
 
49
        return content
 
50
 
 
51
 
 
52
def _set_gpg_tty():
 
53
    tty = os.environ.get('TTY')
 
54
    if tty is not None:
 
55
        os.environ['GPG_TTY'] = tty
 
56
        trace.mutter('setting GPG_TTY=%s', tty)
 
57
    else:
 
58
        # This is not quite worthy of a warning, because some people
 
59
        # don't need GPG_TTY to be set. But it is worthy of a big mark
 
60
        # in ~/.bzr.log, so that people can debug it if it happens to them
 
61
        trace.mutter('** Env var TTY empty, cannot set GPG_TTY.'
 
62
                     '  Is TTY exported?')
 
63
 
 
64
 
 
65
class GPGStrategy(object):
 
66
    """GPG Signing and checking facilities."""
 
67
        
 
68
    def _command_line(self):
 
69
        return [self._config.gpg_signing_command(), '--clearsign']
 
70
 
 
71
    def __init__(self, config):
 
72
        self._config = config
 
73
 
 
74
    def sign(self, content):
 
75
        ui.ui_factory.clear_term()
 
76
 
 
77
        preexec_fn = _set_gpg_tty
 
78
        if sys.platform == 'win32':
 
79
            # Win32 doesn't support preexec_fn, but wouldn't support TTY anyway.
 
80
            preexec_fn = None
 
81
        try:
 
82
            process = subprocess.Popen(self._command_line(),
 
83
                                       stdin=subprocess.PIPE,
 
84
                                       stdout=subprocess.PIPE,
 
85
                                       preexec_fn=preexec_fn)
 
86
            try:
 
87
                result = process.communicate(content)[0]
 
88
                if process.returncode is None:
 
89
                    process.wait()
 
90
                if process.returncode != 0:
 
91
                    raise errors.SigningFailed(self._command_line())
 
92
                return result
 
93
            except OSError, e:
 
94
                if e.errno == errno.EPIPE:
 
95
                    raise errors.SigningFailed(self._command_line())
 
96
                else:
 
97
                    raise
 
98
        except ValueError:
 
99
            # bad subprocess parameters, should never happen.
 
100
            raise
 
101
        except OSError, e:
 
102
            if e.errno == errno.ENOENT:
 
103
                # gpg is not installed
 
104
                raise errors.SigningFailed(self._command_line())
 
105
            else:
 
106
                raise