~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/gpg.py

  • Committer: Jonathan Riddell
  • Date: 2011-06-22 15:22:39 UTC
  • mto: This revision was merged to the branch mainline in revision 6003.
  • Revision ID: jriddell@canonical.com-20110622152239-g53co6o2b97hhtr5
make gpgme context global to class

Show diffs side-by-side

added added

removed removed

Lines of Context:
120
120
 
121
121
    def __init__(self, config):
122
122
        self._config = config
 
123
        try:
 
124
            import gpgme
 
125
            self.context = gpgme.Context()
 
126
        except ImportError, error:
 
127
            pass # can't use verify()
123
128
 
124
129
    def sign(self, content):
125
130
        if isinstance(content, unicode):
170
175
        except ImportError, error:
171
176
            raise errors.GpgmeNotInstalled(error)
172
177
 
173
 
        context = gpgme.Context()
174
178
        signature = StringIO(content)
175
179
        plain_output = StringIO()
176
180
        
177
181
        try:
178
 
            result = context.verify(signature, None, plain_output)
 
182
            result = self.context.verify(signature, None, plain_output)
179
183
        except gpgme.GpgmeError,error:
180
184
            raise errors.SignatureVerificationFailed(error[2])
181
185
 
188
192
        if testament != plain_output.getvalue():
189
193
            return SIGNATURE_NOT_VALID, None
190
194
        if result[0].summary & gpgme.SIGSUM_VALID:
191
 
            key = context.get_key(fingerprint)
 
195
            key = self.context.get_key(fingerprint)
192
196
            name = key.uids[0].name
193
197
            email = key.uids[0].email
194
198
            return SIGNATURE_VALID, name + " <" + email + ">"
206
210
                                                 "verification result")
207
211
 
208
212
    def set_acceptable_keys(self, key_patterns):
209
 
        try:
210
 
            import gpgme
211
 
        except ImportError, error:
212
 
            raise errors.GpgmeNotInstalled(error)
213
213
        patterns = key_patterns.split(",")
214
214
 
215
215
        self.acceptable_keys = []
216
 
        context = gpgme.Context()
217
216
        for pattern in patterns:
218
 
            result = context.keylist(pattern)
 
217
            result = self.context.keylist(pattern)
219
218
            found_key = False
220
219
            for key in result:
221
220
                found_key = True