~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/gpg.py

  • Committer: Vincent Ladeuil
  • Date: 2012-07-31 09:17:34 UTC
  • mto: This revision was merged to the branch mainline in revision 6554.
  • Revision ID: v.ladeuil+lp@free.fr-20120731091734-700oburs0806cont
SplitĀ eagerĀ test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""GPG signing and checking logic."""
19
19
 
 
20
from __future__ import absolute_import
 
21
 
20
22
import os
21
23
import sys
22
24
from StringIO import StringIO
27
29
import subprocess
28
30
 
29
31
from bzrlib import (
 
32
    config,
30
33
    errors,
31
34
    trace,
32
35
    ui,
33
36
    )
 
37
from bzrlib.i18n import (
 
38
    gettext, 
 
39
    ngettext,
 
40
    )
34
41
""")
35
42
 
36
 
class i18n:
37
 
    """this class is ready to use bzrlib.i18n but bzrlib.i18n is not ready to
38
 
    use so here is a stub until it is"""
39
 
    @staticmethod
40
 
    def gettext(string):
41
 
        return string
42
 
        
43
 
    @staticmethod
44
 
    def ngettext(single, plural, number):
45
 
        if number == 1:
46
 
            return single
47
 
        else:
48
 
            return plural
 
43
from bzrlib.symbol_versioning import (
 
44
    deprecated_in,
 
45
    deprecated_method,
 
46
    )
49
47
 
50
48
#verification results
51
49
SIGNATURE_VALID = 0
52
50
SIGNATURE_KEY_MISSING = 1
53
51
SIGNATURE_NOT_VALID = 2
54
52
SIGNATURE_NOT_SIGNED = 3
 
53
SIGNATURE_EXPIRED = 4
 
54
 
 
55
 
 
56
def bulk_verify_signatures(repository, revids, strategy,
 
57
        process_events_callback=None):
 
58
    """Do verifications on a set of revisions
 
59
 
 
60
    :param repository: repository object
 
61
    :param revids: list of revision ids to verify
 
62
    :param strategy: GPG strategy to use
 
63
    :param process_events_callback: method to call for GUI frontends that
 
64
        want to keep their UI refreshed
 
65
 
 
66
    :return: count dictionary of results of each type,
 
67
             result list for each revision,
 
68
             boolean True if all results are verified successfully
 
69
    """
 
70
    count = {SIGNATURE_VALID: 0,
 
71
             SIGNATURE_KEY_MISSING: 0,
 
72
             SIGNATURE_NOT_VALID: 0,
 
73
             SIGNATURE_NOT_SIGNED: 0,
 
74
             SIGNATURE_EXPIRED: 0}
 
75
    result = []
 
76
    all_verifiable = True
 
77
    total = len(revids)
 
78
    pb = ui.ui_factory.nested_progress_bar()
 
79
    try:
 
80
        for i, (rev_id, verification_result, uid) in enumerate(
 
81
                repository.verify_revision_signatures(
 
82
                    revids, strategy)):
 
83
            pb.update("verifying signatures", i, total)
 
84
            result.append([rev_id, verification_result, uid])
 
85
            count[verification_result] += 1
 
86
            if verification_result != SIGNATURE_VALID:
 
87
                all_verifiable = False
 
88
            if process_events_callback is not None:
 
89
                process_events_callback()
 
90
    finally:
 
91
        pb.finished()
 
92
    return (count, result, all_verifiable)
55
93
 
56
94
 
57
95
class DisabledGPGStrategy(object):
76
114
 
77
115
 
78
116
class LoopbackGPGStrategy(object):
79
 
    """A GPG Strategy that acts like 'cat' - data is just passed through."""
 
117
    """A GPG Strategy that acts like 'cat' - data is just passed through.
 
118
    Used in tests.
 
119
    """
80
120
 
81
121
    @staticmethod
82
122
    def verify_signatures_available():
102
142
                else:
103
143
                    self.acceptable_keys.append(pattern)
104
144
 
 
145
    @deprecated_method(deprecated_in((2, 6, 0)))
105
146
    def do_verifications(self, revisions, repository):
106
 
        count = {SIGNATURE_VALID: 0,
107
 
                 SIGNATURE_KEY_MISSING: 0,
108
 
                 SIGNATURE_NOT_VALID: 0,
109
 
                 SIGNATURE_NOT_SIGNED: 0}
110
 
        result = []
111
 
        all_verifiable = True
112
 
        for rev_id in revisions:
113
 
            verification_result, uid =\
114
 
                                repository.verify_revision(rev_id,self)
115
 
            result.append([rev_id, verification_result, uid])
116
 
            count[verification_result] += 1
117
 
            if verification_result != SIGNATURE_VALID:
118
 
                all_verifiable = False
119
 
        return (count, result, all_verifiable)
 
147
        return bulk_verify_signatures(repository, revisions, self)
120
148
 
 
149
    @deprecated_method(deprecated_in((2, 6, 0)))
121
150
    def valid_commits_message(self, count):
122
 
        return i18n.gettext("{0} commits with valid signatures").format(
123
 
                                        count[SIGNATURE_VALID])            
 
151
        return valid_commits_message(count)
124
152
 
 
153
    @deprecated_method(deprecated_in((2, 6, 0)))
125
154
    def unknown_key_message(self, count):
126
 
        return i18n.ngettext("{0} commit with unknown key",
127
 
                             "{0} commits with unknown keys",
128
 
                             count[SIGNATURE_KEY_MISSING]).format(
129
 
                                        count[SIGNATURE_KEY_MISSING])
 
155
        return unknown_key_message(count)
130
156
 
 
157
    @deprecated_method(deprecated_in((2, 6, 0)))
131
158
    def commit_not_valid_message(self, count):
132
 
        return i18n.ngettext("{0} commit not valid",
133
 
                             "{0} commits not valid",
134
 
                             count[SIGNATURE_NOT_VALID]).format(
135
 
                                            count[SIGNATURE_NOT_VALID])
 
159
        return commit_not_valid_message(count)
136
160
 
 
161
    @deprecated_method(deprecated_in((2, 6, 0)))
137
162
    def commit_not_signed_message(self, count):
138
 
        return i18n.ngettext("{0} commit not signed",
139
 
                             "{0} commits not signed",
140
 
                             count[SIGNATURE_NOT_SIGNED]).format(
141
 
                                        count[SIGNATURE_NOT_SIGNED])
 
163
        return commit_not_signed_message(count)
 
164
 
 
165
    @deprecated_method(deprecated_in((2, 6, 0)))
 
166
    def expired_commit_message(self, count):
 
167
        return expired_commit_message(count)
142
168
 
143
169
 
144
170
def _set_gpg_tty():
159
185
 
160
186
    acceptable_keys = None
161
187
 
 
188
    def __init__(self, config_stack):
 
189
        self._config_stack = config_stack
 
190
        try:
 
191
            import gpgme
 
192
            self.context = gpgme.Context()
 
193
        except ImportError, error:
 
194
            pass # can't use verify()
 
195
 
162
196
    @staticmethod
163
197
    def verify_signatures_available():
 
198
        """
 
199
        check if this strategy can verify signatures
 
200
 
 
201
        :return: boolean if this strategy can verify signatures
 
202
        """
164
203
        try:
165
204
            import gpgme
166
205
            return True
168
207
            return False
169
208
 
170
209
    def _command_line(self):
171
 
        return [self._config.gpg_signing_command(), '--clearsign']
172
 
 
173
 
    def __init__(self, config):
174
 
        self._config = config
175
 
        try:
176
 
            import gpgme
177
 
            self.context = gpgme.Context()
178
 
        except ImportError, error:
179
 
            pass # can't use verify()
 
210
        key = self._config_stack.get('gpg_signing_key')
 
211
        if key is None or key == 'default':
 
212
            # 'default' or not setting gpg_signing_key at all means we should
 
213
            # use the user email address
 
214
            key = config.extract_email_address(self._config_stack.get('email'))
 
215
        return [self._config_stack.get('gpg_signing_command'), '--clearsign',
 
216
                '-u', key, '--no-tty']
180
217
 
181
218
    def sign(self, content):
182
219
        if isinstance(content, unicode):
216
253
 
217
254
    def verify(self, content, testament):
218
255
        """Check content has a valid signature.
219
 
        
 
256
 
220
257
        :param content: the commit signature
221
258
        :param testament: the valid testament string for the commit
222
 
        
 
259
 
223
260
        :return: SIGNATURE_VALID or a failed SIGNATURE_ value, key uid if valid
224
261
        """
225
262
        try:
229
266
 
230
267
        signature = StringIO(content)
231
268
        plain_output = StringIO()
232
 
        
233
269
        try:
234
270
            result = self.context.verify(signature, None, plain_output)
235
271
        except gpgme.GpgmeError,error:
236
272
            raise errors.SignatureVerificationFailed(error[2])
237
273
 
 
274
        # No result if input is invalid.
 
275
        # test_verify_invalid()
238
276
        if len(result) == 0:
239
277
            return SIGNATURE_NOT_VALID, None
 
278
        # User has specified a list of acceptable keys, check our result is in
 
279
        # it.  test_verify_unacceptable_key()
240
280
        fingerprint = result[0].fpr
241
281
        if self.acceptable_keys is not None:
242
282
            if not fingerprint in self.acceptable_keys:
243
283
                return SIGNATURE_KEY_MISSING, fingerprint[-8:]
 
284
        # Check the signature actually matches the testament.
 
285
        # test_verify_bad_testament()
244
286
        if testament != plain_output.getvalue():
245
287
            return SIGNATURE_NOT_VALID, None
 
288
        # Yay gpgme set the valid bit.
 
289
        # Can't write a test for this one as you can't set a key to be
 
290
        # trusted using gpgme.
246
291
        if result[0].summary & gpgme.SIGSUM_VALID:
247
292
            key = self.context.get_key(fingerprint)
248
293
            name = key.uids[0].name
249
294
            email = key.uids[0].email
250
295
            return SIGNATURE_VALID, name + " <" + email + ">"
 
296
        # Sigsum_red indicates a problem, unfortunatly I have not been able
 
297
        # to write any tests which actually set this.
251
298
        if result[0].summary & gpgme.SIGSUM_RED:
252
299
            return SIGNATURE_NOT_VALID, None
 
300
        # GPG does not know this key.
 
301
        # test_verify_unknown_key()
253
302
        if result[0].summary & gpgme.SIGSUM_KEY_MISSING:
254
303
            return SIGNATURE_KEY_MISSING, fingerprint[-8:]
255
 
        #summary isn't set if sig is valid but key is untrusted
 
304
        # Summary isn't set if sig is valid but key is untrusted but if user
 
305
        # has explicity set the key as acceptable we can validate it.
256
306
        if result[0].summary == 0 and self.acceptable_keys is not None:
257
307
            if fingerprint in self.acceptable_keys:
 
308
                # test_verify_untrusted_but_accepted()
258
309
                return SIGNATURE_VALID, None
259
 
        else:
260
 
            return SIGNATURE_KEY_MISSING, None
 
310
        # test_verify_valid_but_untrusted()
 
311
        if result[0].summary == 0 and self.acceptable_keys is None:
 
312
            return SIGNATURE_NOT_VALID, None
 
313
        if result[0].summary & gpgme.SIGSUM_KEY_EXPIRED:
 
314
            expires = self.context.get_key(result[0].fpr).subkeys[0].expires
 
315
            if expires > result[0].timestamp:
 
316
                # The expired key was not expired at time of signing.
 
317
                # test_verify_expired_but_valid()
 
318
                return SIGNATURE_EXPIRED, fingerprint[-8:]
 
319
            else:
 
320
                # I can't work out how to create a test where the signature
 
321
                # was expired at the time of signing.
 
322
                return SIGNATURE_NOT_VALID, None
 
323
        # A signature from a revoked key gets this.
 
324
        # test_verify_revoked_signature()
 
325
        if result[0].summary & gpgme.SIGSUM_SYS_ERROR:
 
326
            return SIGNATURE_NOT_VALID, None
 
327
        # Other error types such as revoked keys should (I think) be caught by
 
328
        # SIGSUM_RED so anything else means something is buggy.
261
329
        raise errors.SignatureVerificationFailed("Unknown GnuPG key "\
262
330
                                                 "verification result")
263
331
 
264
332
    def set_acceptable_keys(self, command_line_input):
265
 
        """sets the acceptable keys for verifying with this GPGStrategy
266
 
        
 
333
        """Set the acceptable keys for verifying with this GPGStrategy.
 
334
 
267
335
        :param command_line_input: comma separated list of patterns from
268
336
                                command line
269
337
        :return: nothing
270
338
        """
271
339
        key_patterns = None
272
 
        acceptable_keys_config = self._config.acceptable_keys()
 
340
        acceptable_keys_config = self._config_stack.get('acceptable_keys')
273
341
        try:
274
342
            if isinstance(acceptable_keys_config, unicode):
275
343
                acceptable_keys_config = str(acceptable_keys_config)
276
344
        except UnicodeEncodeError:
277
 
            raise errors.BzrCommandError('Only ASCII permitted in option names')
 
345
            # gpg Context.keylist(pattern) does not like unicode
 
346
            raise errors.BzrCommandError(
 
347
                gettext('Only ASCII permitted in option names'))
278
348
 
279
349
        if acceptable_keys_config is not None:
280
350
            key_patterns = acceptable_keys_config
281
 
        if command_line_input is not None: #command line overrides config
 
351
        if command_line_input is not None: # command line overrides config
282
352
            key_patterns = command_line_input
283
353
        if key_patterns is not None:
284
354
            patterns = key_patterns.split(",")
292
362
                    self.acceptable_keys.append(key.subkeys[0].fpr)
293
363
                    trace.mutter("Added acceptable key: " + key.subkeys[0].fpr)
294
364
                if not found_key:
295
 
                    trace.note(i18n.gettext(
296
 
                            "No GnuPG key results for pattern: {}"
 
365
                    trace.note(gettext(
 
366
                            "No GnuPG key results for pattern: {0}"
297
367
                                ).format(pattern))
298
368
 
299
 
    def do_verifications(self, revisions, repository):
 
369
    @deprecated_method(deprecated_in((2, 6, 0)))
 
370
    def do_verifications(self, revisions, repository,
 
371
                            process_events_callback=None):
300
372
        """do verifications on a set of revisions
301
 
        
 
373
 
302
374
        :param revisions: list of revision ids to verify
303
375
        :param repository: repository object
304
 
        
 
376
        :param process_events_callback: method to call for GUI frontends that
 
377
            want to keep their UI refreshed
 
378
 
305
379
        :return: count dictionary of results of each type,
306
380
                 result list for each revision,
307
381
                 boolean True if all results are verified successfully
308
382
        """
309
 
        count = {SIGNATURE_VALID: 0,
310
 
                 SIGNATURE_KEY_MISSING: 0,
311
 
                 SIGNATURE_NOT_VALID: 0,
312
 
                 SIGNATURE_NOT_SIGNED: 0}
313
 
        result = []
314
 
        all_verifiable = True
315
 
        for rev_id in revisions:
316
 
            verification_result, uid =\
317
 
                                repository.verify_revision(rev_id,self)
318
 
            result.append([rev_id, verification_result, uid])
319
 
            count[verification_result] += 1
320
 
            if verification_result != SIGNATURE_VALID:
321
 
                all_verifiable = False
322
 
        return (count, result, all_verifiable)
 
383
        return bulk_verify_signatures(repository, revisions, self,
 
384
            process_events_callback)
323
385
 
 
386
    @deprecated_method(deprecated_in((2, 6, 0)))
324
387
    def verbose_valid_message(self, result):
325
388
        """takes a verify result and returns list of signed commits strings"""
326
 
        signers = {}
327
 
        for rev_id, validity, uid in result:
328
 
            if validity == SIGNATURE_VALID:
329
 
                signers.setdefault(uid, 0)
330
 
                signers[uid] += 1
331
 
        result = []
332
 
        for uid, number in signers.items():
333
 
             result.append( i18n.ngettext("{0} signed {1} commit", 
334
 
                             "{0} signed {1} commits",
335
 
                             number).format(uid, number) )
336
 
        return result
337
 
 
338
 
 
 
389
        return verbose_valid_message(result)
 
390
 
 
391
    @deprecated_method(deprecated_in((2, 6, 0)))
339
392
    def verbose_not_valid_message(self, result, repo):
340
393
        """takes a verify result and returns list of not valid commit info"""
341
 
        signers = {}
342
 
        for rev_id, validity, empty in result:
343
 
            if validity == SIGNATURE_NOT_VALID:
344
 
                revision = repo.get_revision(rev_id)
345
 
                authors = ', '.join(revision.get_apparent_authors())
346
 
                signers.setdefault(authors, 0)
347
 
                signers[authors] += 1
348
 
        result = []
349
 
        for authors, number in signers.items():
350
 
            result.append( i18n.ngettext("{0} commit by author {1}", 
351
 
                                 "{0} commits by author {1}",
352
 
                                 number).format(number, authors) )
353
 
        return result
 
394
        return verbose_not_valid_message(result, repo)
354
395
 
 
396
    @deprecated_method(deprecated_in((2, 6, 0)))
355
397
    def verbose_not_signed_message(self, result, repo):
356
398
        """takes a verify result and returns list of not signed commit info"""
357
 
        signers = {}
358
 
        for rev_id, validity, empty in result:
359
 
            if validity == SIGNATURE_NOT_SIGNED:
360
 
                revision = repo.get_revision(rev_id)
361
 
                authors = ', '.join(revision.get_apparent_authors())
362
 
                signers.setdefault(authors, 0)
363
 
                signers[authors] += 1
364
 
        result = []
365
 
        for authors, number in signers.items():
366
 
            result.append( i18n.ngettext("{0} commit by author {1}", 
367
 
                                 "{0} commits by author {1}",
368
 
                                 number).format(number, authors) )
369
 
        return result
 
399
        return verbose_not_valid_message(result, repo)
370
400
 
 
401
    @deprecated_method(deprecated_in((2, 6, 0)))
371
402
    def verbose_missing_key_message(self, result):
372
403
        """takes a verify result and returns list of missing key info"""
373
 
        signers = {}
374
 
        for rev_id, validity, fingerprint in result:
375
 
            if validity == SIGNATURE_KEY_MISSING:
376
 
                signers.setdefault(fingerprint, 0)
377
 
                signers[fingerprint] += 1
378
 
        result = []
379
 
        for fingerprint, number in signers.items():
380
 
            result.append( i18n.ngettext("Unknown key {0} signed {1} commit", 
381
 
                                 "Unknown key {0} signed {1} commits",
382
 
                                 number).format(fingerprint, number) )
383
 
        return result
384
 
 
 
404
        return verbose_missing_key_message(result)
 
405
 
 
406
    @deprecated_method(deprecated_in((2, 6, 0)))
 
407
    def verbose_expired_key_message(self, result, repo):
 
408
        """takes a verify result and returns list of expired key info"""
 
409
        return verbose_expired_key_message(result, repo)
 
410
 
 
411
    @deprecated_method(deprecated_in((2, 6, 0)))
385
412
    def valid_commits_message(self, count):
386
413
        """returns message for number of commits"""
387
 
        return i18n.gettext("{0} commits with valid signatures").format(
388
 
                                        count[SIGNATURE_VALID])
 
414
        return valid_commits_message(count)
389
415
 
 
416
    @deprecated_method(deprecated_in((2, 6, 0)))
390
417
    def unknown_key_message(self, count):
391
418
        """returns message for number of commits"""
392
 
        return i18n.ngettext("{0} commit with unknown key",
393
 
                             "{0} commits with unknown keys",
394
 
                             count[SIGNATURE_KEY_MISSING]).format(
395
 
                                        count[SIGNATURE_KEY_MISSING])
 
419
        return unknown_key_message(count)
396
420
 
 
421
    @deprecated_method(deprecated_in((2, 6, 0)))
397
422
    def commit_not_valid_message(self, count):
398
423
        """returns message for number of commits"""
399
 
        return i18n.ngettext("{0} commit not valid",
400
 
                             "{0} commits not valid",
401
 
                             count[SIGNATURE_NOT_VALID]).format(
402
 
                                            count[SIGNATURE_NOT_VALID])
 
424
        return commit_not_valid_message(count)
403
425
 
 
426
    @deprecated_method(deprecated_in((2, 6, 0)))
404
427
    def commit_not_signed_message(self, count):
405
428
        """returns message for number of commits"""
406
 
        return i18n.ngettext("{0} commit not signed",
407
 
                             "{0} commits not signed",
408
 
                             count[SIGNATURE_NOT_SIGNED]).format(
409
 
                                        count[SIGNATURE_NOT_SIGNED])
 
429
        return commit_not_signed_message(count)
 
430
 
 
431
    @deprecated_method(deprecated_in((2, 6, 0)))
 
432
    def expired_commit_message(self, count):
 
433
        """returns message for number of commits"""
 
434
        return expired_commit_message(count)
 
435
 
 
436
 
 
437
def valid_commits_message(count):
 
438
    """returns message for number of commits"""
 
439
    return gettext(u"{0} commits with valid signatures").format(
 
440
                                    count[SIGNATURE_VALID])
 
441
 
 
442
 
 
443
def unknown_key_message(count):
 
444
    """returns message for number of commits"""
 
445
    return ngettext(u"{0} commit with unknown key",
 
446
                    u"{0} commits with unknown keys",
 
447
                    count[SIGNATURE_KEY_MISSING]).format(
 
448
                                    count[SIGNATURE_KEY_MISSING])
 
449
 
 
450
 
 
451
def commit_not_valid_message(count):
 
452
    """returns message for number of commits"""
 
453
    return ngettext(u"{0} commit not valid",
 
454
                    u"{0} commits not valid",
 
455
                    count[SIGNATURE_NOT_VALID]).format(
 
456
                                        count[SIGNATURE_NOT_VALID])
 
457
 
 
458
 
 
459
def commit_not_signed_message(count):
 
460
    """returns message for number of commits"""
 
461
    return ngettext(u"{0} commit not signed",
 
462
                    u"{0} commits not signed",
 
463
                    count[SIGNATURE_NOT_SIGNED]).format(
 
464
                                    count[SIGNATURE_NOT_SIGNED])
 
465
 
 
466
 
 
467
def expired_commit_message(count):
 
468
    """returns message for number of commits"""
 
469
    return ngettext(u"{0} commit with key now expired",
 
470
                    u"{0} commits with key now expired",
 
471
                    count[SIGNATURE_EXPIRED]).format(
 
472
                                count[SIGNATURE_EXPIRED])
 
473
 
 
474
 
 
475
def verbose_expired_key_message(result, repo):
 
476
    """takes a verify result and returns list of expired key info"""
 
477
    signers = {}
 
478
    fingerprint_to_authors = {}
 
479
    for rev_id, validity, fingerprint in result:
 
480
        if validity == SIGNATURE_EXPIRED:
 
481
            revision = repo.get_revision(rev_id)
 
482
            authors = ', '.join(revision.get_apparent_authors())
 
483
            signers.setdefault(fingerprint, 0)
 
484
            signers[fingerprint] += 1
 
485
            fingerprint_to_authors[fingerprint] = authors
 
486
    result = []
 
487
    for fingerprint, number in signers.items():
 
488
        result.append(
 
489
            ngettext(u"{0} commit by author {1} with key {2} now expired",
 
490
                     u"{0} commits by author {1} with key {2} now expired",
 
491
                     number).format(
 
492
                number, fingerprint_to_authors[fingerprint], fingerprint))
 
493
    return result
 
494
 
 
495
 
 
496
def verbose_valid_message(result):
 
497
    """takes a verify result and returns list of signed commits strings"""
 
498
    signers = {}
 
499
    for rev_id, validity, uid in result:
 
500
        if validity == SIGNATURE_VALID:
 
501
            signers.setdefault(uid, 0)
 
502
            signers[uid] += 1
 
503
    result = []
 
504
    for uid, number in signers.items():
 
505
         result.append(ngettext(u"{0} signed {1} commit",
 
506
                                u"{0} signed {1} commits",
 
507
                                number).format(uid, number))
 
508
    return result
 
509
 
 
510
 
 
511
def verbose_not_valid_message(result, repo):
 
512
    """takes a verify result and returns list of not valid commit info"""
 
513
    signers = {}
 
514
    for rev_id, validity, empty in result:
 
515
        if validity == SIGNATURE_NOT_VALID:
 
516
            revision = repo.get_revision(rev_id)
 
517
            authors = ', '.join(revision.get_apparent_authors())
 
518
            signers.setdefault(authors, 0)
 
519
            signers[authors] += 1
 
520
    result = []
 
521
    for authors, number in signers.items():
 
522
        result.append(ngettext(u"{0} commit by author {1}",
 
523
                               u"{0} commits by author {1}",
 
524
                               number).format(number, authors))
 
525
    return result
 
526
 
 
527
 
 
528
def verbose_not_signed_message(result, repo):
 
529
    """takes a verify result and returns list of not signed commit info"""
 
530
    signers = {}
 
531
    for rev_id, validity, empty in result:
 
532
        if validity == SIGNATURE_NOT_SIGNED:
 
533
            revision = repo.get_revision(rev_id)
 
534
            authors = ', '.join(revision.get_apparent_authors())
 
535
            signers.setdefault(authors, 0)
 
536
            signers[authors] += 1
 
537
    result = []
 
538
    for authors, number in signers.items():
 
539
        result.append(ngettext(u"{0} commit by author {1}",
 
540
                               u"{0} commits by author {1}",
 
541
                               number).format(number, authors))
 
542
    return result
 
543
 
 
544
 
 
545
def verbose_missing_key_message(result):
 
546
    """takes a verify result and returns list of missing key info"""
 
547
    signers = {}
 
548
    for rev_id, validity, fingerprint in result:
 
549
        if validity == SIGNATURE_KEY_MISSING:
 
550
            signers.setdefault(fingerprint, 0)
 
551
            signers[fingerprint] += 1
 
552
    result = []
 
553
    for fingerprint, number in signers.items():
 
554
        result.append(ngettext(u"Unknown key {0} signed {1} commit",
 
555
                               u"Unknown key {0} signed {1} commits",
 
556
                               number).format(fingerprint, number))
 
557
    return result