~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/gpg.py

  • Committer: Robert Collins
  • Date: 2006-05-24 08:14:45 UTC
  • mfrom: (1725.1.1 benchmark)
  • mto: (1725.2.6 commit)
  • mto: This revision was merged to the branch mainline in revision 1729.
  • Revision ID: robertc@robertcollins.net-20060524081445-c046b4406ffc8dfa
(rbc)Merge in benchmark --lsprof-timed lsprofiling feature. (Robert Collins, Martin Pool).

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 subprocess
 
22
 
 
23
import bzrlib.errors as errors
 
24
 
 
25
class DisabledGPGStrategy(object):
 
26
    """A GPG Strategy that makes everything fail."""
 
27
 
 
28
    def __init__(self, ignored):
 
29
        """Real strategies take a configuration."""
 
30
 
 
31
    def sign(self, content):
 
32
        raise errors.SigningFailed('Signing is disabled.')
 
33
 
 
34
 
 
35
class LoopbackGPGStrategy(object):
 
36
    """A GPG Strategy that acts like 'cat' - data is just passed through."""
 
37
 
 
38
    def __init__(self, ignored):
 
39
        """Real strategies take a configuration."""
 
40
 
 
41
    def sign(self, content):
 
42
        return content
 
43
 
 
44
 
 
45
class GPGStrategy(object):
 
46
    """GPG Signing and checking facilities."""
 
47
        
 
48
    def _command_line(self):
 
49
        return [self._config.gpg_signing_command(), '--clearsign']
 
50
 
 
51
    def __init__(self, config):
 
52
        self._config = config
 
53
 
 
54
    def sign(self, content):
 
55
        try:
 
56
            process = subprocess.Popen(self._command_line(),
 
57
                                       stdin=subprocess.PIPE,
 
58
                                       stdout=subprocess.PIPE)
 
59
            try:
 
60
                result = process.communicate(content)[0]
 
61
                if process.returncode is None:
 
62
                    process.wait()
 
63
                if process.returncode != 0:
 
64
                    raise errors.SigningFailed(self._command_line())
 
65
                return result
 
66
            except OSError, e:
 
67
                if e.errno == errno.EPIPE:
 
68
                    raise errors.SigningFailed(self._command_line())
 
69
                else:
 
70
                    raise
 
71
        except ValueError:
 
72
            # bad subprocess parameters, should never happen.
 
73
            raise
 
74
        except OSError, e:
 
75
            if e.errno == errno.ENOENT:
 
76
                # gpg is not installed
 
77
                raise errors.SigningFailed(self._command_line())
 
78
            else:
 
79
                raise