~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/gpg.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-15 13:49:38 UTC
  • mfrom: (6060.3.1 fix-last-revno-args)
  • Revision ID: pqm@pqm.ubuntu.com-20110815134938-4fuo63g4v2hj8jdt
(jelmer) Cope with the localhost having the name 'localhost' when running
 the test suite. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
    errors,
31
31
    trace,
32
32
    ui,
33
 
    i18n,
34
33
    )
35
34
""")
36
35
 
 
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
 
49
 
37
50
#verification results
38
51
SIGNATURE_VALID = 0
39
52
SIGNATURE_KEY_MISSING = 1
63
76
 
64
77
 
65
78
class LoopbackGPGStrategy(object):
66
 
    """A GPG Strategy that acts like 'cat' - data is just passed through."""
 
79
    """A GPG Strategy that acts like 'cat' - data is just passed through.
 
80
    Used in tests.
 
81
    """
67
82
 
68
83
    @staticmethod
69
84
    def verify_signatures_available():
106
121
        return (count, result, all_verifiable)
107
122
 
108
123
    def valid_commits_message(self, count):
109
 
        return i18n.gettext("{0} commits with valid signatures").format(
 
124
        return i18n.gettext(u"{0} commits with valid signatures").format(
110
125
                                        count[SIGNATURE_VALID])            
111
126
 
112
127
    def unknown_key_message(self, count):
113
 
        return i18n.ngettext("{0} commit with unknown key",
114
 
                             "{0} commits with unknown keys",
 
128
        return i18n.ngettext(u"{0} commit with unknown key",
 
129
                             u"{0} commits with unknown keys",
115
130
                             count[SIGNATURE_KEY_MISSING]).format(
116
131
                                        count[SIGNATURE_KEY_MISSING])
117
132
 
118
133
    def commit_not_valid_message(self, count):
119
 
        return i18n.ngettext("{0} commit not valid",
120
 
                             "{0} commits not valid",
 
134
        return i18n.ngettext(u"{0} commit not valid",
 
135
                             u"{0} commits not valid",
121
136
                             count[SIGNATURE_NOT_VALID]).format(
122
137
                                            count[SIGNATURE_NOT_VALID])
123
138
 
124
139
    def commit_not_signed_message(self, count):
125
 
        return i18n.ngettext("{0} commit not signed",
126
 
                             "{0} commits not signed",
 
140
        return i18n.ngettext(u"{0} commit not signed",
 
141
                             u"{0} commits not signed",
127
142
                             count[SIGNATURE_NOT_SIGNED]).format(
128
143
                                        count[SIGNATURE_NOT_SIGNED])
129
144
 
148
163
 
149
164
    @staticmethod
150
165
    def verify_signatures_available():
 
166
        """
 
167
        check if this strategy can verify signatures
 
168
 
 
169
        :return: boolean if this strategy can verify signatures
 
170
        """
151
171
        try:
152
172
            import gpgme
153
173
            return True
155
175
            return False
156
176
 
157
177
    def _command_line(self):
158
 
        return [self._config.gpg_signing_command(), '--clearsign']
 
178
        
 
179
        return [self._config.gpg_signing_command(), '--clearsign', '-u',
 
180
                                                self._config.gpg_signing_key()]
159
181
 
160
182
    def __init__(self, config):
161
183
        self._config = config
261
283
            if isinstance(acceptable_keys_config, unicode):
262
284
                acceptable_keys_config = str(acceptable_keys_config)
263
285
        except UnicodeEncodeError:
 
286
            #gpg Context.keylist(pattern) does not like unicode
264
287
            raise errors.BzrCommandError('Only ASCII permitted in option names')
265
288
 
266
289
        if acceptable_keys_config is not None:
283
306
                            "No GnuPG key results for pattern: {}"
284
307
                                ).format(pattern))
285
308
 
286
 
    def do_verifications(self, revisions, repository):
 
309
    def do_verifications(self, revisions, repository,
 
310
                            process_events_callback=None):
287
311
        """do verifications on a set of revisions
288
312
        
289
313
        :param revisions: list of revision ids to verify
290
314
        :param repository: repository object
 
315
        :param process_events_callback: method to call for GUI frontends that
 
316
                                                want to keep their UI refreshed
291
317
        
292
318
        :return: count dictionary of results of each type,
293
319
                 result list for each revision,
306
332
            count[verification_result] += 1
307
333
            if verification_result != SIGNATURE_VALID:
308
334
                all_verifiable = False
 
335
            if process_events_callback is not None:
 
336
                process_events_callback()
309
337
        return (count, result, all_verifiable)
310
338
 
311
339
    def verbose_valid_message(self, result):
317
345
                signers[uid] += 1
318
346
        result = []
319
347
        for uid, number in signers.items():
320
 
             result.append( i18n.ngettext("{0} signed {1} commit", 
321
 
                             "{0} signed {1} commits",
 
348
             result.append( i18n.ngettext(u"{0} signed {1} commit", 
 
349
                             u"{0} signed {1} commits",
322
350
                             number).format(uid, number) )
323
351
        return result
324
352
 
334
362
                signers[authors] += 1
335
363
        result = []
336
364
        for authors, number in signers.items():
337
 
            result.append( i18n.ngettext("{0} commit by author {1}", 
338
 
                                 "{0} commits by author {1}",
 
365
            result.append( i18n.ngettext(u"{0} commit by author {1}", 
 
366
                                 u"{0} commits by author {1}",
339
367
                                 number).format(number, authors) )
340
368
        return result
341
369
 
350
378
                signers[authors] += 1
351
379
        result = []
352
380
        for authors, number in signers.items():
353
 
            result.append( i18n.ngettext("{0} commit by author {1}", 
354
 
                                 "{0} commits by author {1}",
 
381
            result.append( i18n.ngettext(u"{0} commit by author {1}", 
 
382
                                 u"{0} commits by author {1}",
355
383
                                 number).format(number, authors) )
356
384
        return result
357
385
 
364
392
                signers[fingerprint] += 1
365
393
        result = []
366
394
        for fingerprint, number in signers.items():
367
 
            result.append( i18n.ngettext("Unknown key {0} signed {1} commit", 
368
 
                                 "Unknown key {0} signed {1} commits",
 
395
            result.append( i18n.ngettext(u"Unknown key {0} signed {1} commit", 
 
396
                                 u"Unknown key {0} signed {1} commits",
369
397
                                 number).format(fingerprint, number) )
370
398
        return result
371
399
 
372
400
    def valid_commits_message(self, count):
373
401
        """returns message for number of commits"""
374
 
        return i18n.gettext("{0} commits with valid signatures").format(
 
402
        return i18n.gettext(u"{0} commits with valid signatures").format(
375
403
                                        count[SIGNATURE_VALID])
376
404
 
377
405
    def unknown_key_message(self, count):
378
406
        """returns message for number of commits"""
379
 
        return i18n.ngettext("{0} commit with unknown key",
380
 
                             "{0} commits with unknown keys",
 
407
        return i18n.ngettext(u"{0} commit with unknown key",
 
408
                             u"{0} commits with unknown keys",
381
409
                             count[SIGNATURE_KEY_MISSING]).format(
382
410
                                        count[SIGNATURE_KEY_MISSING])
383
411
 
384
412
    def commit_not_valid_message(self, count):
385
413
        """returns message for number of commits"""
386
 
        return i18n.ngettext("{0} commit not valid",
387
 
                             "{0} commits not valid",
 
414
        return i18n.ngettext(u"{0} commit not valid",
 
415
                             u"{0} commits not valid",
388
416
                             count[SIGNATURE_NOT_VALID]).format(
389
417
                                            count[SIGNATURE_NOT_VALID])
390
418
 
391
419
    def commit_not_signed_message(self, count):
392
420
        """returns message for number of commits"""
393
 
        return i18n.ngettext("{0} commit not signed",
394
 
                             "{0} commits not signed",
 
421
        return i18n.ngettext(u"{0} commit not signed",
 
422
                             u"{0} commits not signed",
395
423
                             count[SIGNATURE_NOT_SIGNED]).format(
396
424
                                        count[SIGNATURE_NOT_SIGNED])