~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Patch Queue Manager
  • Date: 2011-09-16 13:27:14 UTC
  • mfrom: (6138.3.15 i18n-gettext-errors)
  • Revision ID: pqm@pqm.ubuntu.com-20110916132714-w81xvesg996w68r6
(jr) Add gettext() to user error messages
 (Jonathan Riddell)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 
 
18
 
from __future__ import absolute_import
19
 
 
20
 
from bzrlib.errors import (
21
 
    BinaryFiles,
22
 
    MalformedHunkHeader,
23
 
    MalformedLine,
24
 
    MalformedPatchHeader,
25
 
    PatchConflict,
26
 
    PatchSyntax,
27
 
    )
28
 
 
29
17
import re
30
18
 
31
19
 
32
20
binary_files_re = 'Binary files (.*) and (.*) differ\n'
33
21
 
34
22
 
 
23
class BinaryFiles(Exception):
 
24
 
 
25
    def __init__(self, orig_name, mod_name):
 
26
        self.orig_name = orig_name
 
27
        self.mod_name = mod_name
 
28
        Exception.__init__(self, 'Binary files section encountered.')
 
29
 
 
30
 
 
31
class PatchSyntax(Exception):
 
32
    def __init__(self, msg):
 
33
        Exception.__init__(self, msg)
 
34
 
 
35
 
 
36
class MalformedPatchHeader(PatchSyntax):
 
37
    def __init__(self, desc, line):
 
38
        self.desc = desc
 
39
        self.line = line
 
40
        msg = "Malformed patch header.  %s\n%r" % (self.desc, self.line)
 
41
        PatchSyntax.__init__(self, msg)
 
42
 
 
43
 
 
44
class MalformedHunkHeader(PatchSyntax):
 
45
    def __init__(self, desc, line):
 
46
        self.desc = desc
 
47
        self.line = line
 
48
        msg = "Malformed hunk header.  %s\n%r" % (self.desc, self.line)
 
49
        PatchSyntax.__init__(self, msg)
 
50
 
 
51
 
 
52
class MalformedLine(PatchSyntax):
 
53
    def __init__(self, desc, line):
 
54
        self.desc = desc
 
55
        self.line = line
 
56
        msg = "Malformed line.  %s\n%s" % (self.desc, self.line)
 
57
        PatchSyntax.__init__(self, msg)
 
58
 
 
59
 
 
60
class PatchConflict(Exception):
 
61
    def __init__(self, line_no, orig_line, patch_line):
 
62
        orig = orig_line.rstrip('\n')
 
63
        patch = str(patch_line).rstrip('\n')
 
64
        msg = 'Text contents mismatch at line %d.  Original has "%s",'\
 
65
            ' but patch says it should be "%s"' % (line_no, orig, patch)
 
66
        Exception.__init__(self, msg)
 
67
 
 
68
 
35
69
def get_patch_names(iter_lines):
36
 
    line = iter_lines.next()
37
70
    try:
 
71
        line = iter_lines.next()
38
72
        match = re.match(binary_files_re, line)
39
73
        if match is not None:
40
74
            raise BinaryFiles(match.group(1), match.group(2))
318
352
                if isinstance(line, ContextLine):
319
353
                    pos += 1
320
354
 
 
355
 
321
356
def parse_patch(iter_lines, allow_dirty=False):
322
357
    '''
323
358
    :arg iter_lines: iterable of lines to parse
336
371
        return patch
337
372
 
338
373
 
339
 
def iter_file_patch(iter_lines, allow_dirty=False, keep_dirty=False):
 
374
def iter_file_patch(iter_lines, allow_dirty=False):
340
375
    '''
341
376
    :arg iter_lines: iterable of lines to parse for patches
342
377
    :kwarg allow_dirty: If True, allow comments and other non-patch text
352
387
    # (as allow_dirty does).
353
388
    regex = re.compile(binary_files_re)
354
389
    saved_lines = []
355
 
    dirty_head = []
356
390
    orig_range = 0
357
391
    beginning = True
358
 
 
359
392
    for line in iter_lines:
360
 
        if line.startswith('=== '):
361
 
            if len(saved_lines) > 0:
362
 
                if keep_dirty and len(dirty_head) > 0:
363
 
                    yield {'saved_lines': saved_lines,
364
 
                           'dirty_head': dirty_head}
365
 
                    dirty_head = []
366
 
                else:
367
 
                    yield saved_lines
368
 
                saved_lines = []
369
 
            dirty_head.append(line)
370
 
            continue
371
 
        if line.startswith('*** '):
 
393
        if line.startswith('=== ') or line.startswith('*** '):
372
394
            continue
373
395
        if line.startswith('#'):
374
396
            continue
382
404
                # parse the patch
383
405
                beginning = False
384
406
            elif len(saved_lines) > 0:
385
 
                if keep_dirty and len(dirty_head) > 0:
386
 
                    yield {'saved_lines': saved_lines,
387
 
                           'dirty_head': dirty_head}
388
 
                    dirty_head = []
389
 
                else:
390
 
                    yield saved_lines
 
407
                yield saved_lines
391
408
            saved_lines = []
392
409
        elif line.startswith('@@'):
393
410
            hunk = hunk_from_header(line)
394
411
            orig_range = hunk.orig_range
395
412
        saved_lines.append(line)
396
413
    if len(saved_lines) > 0:
397
 
        if keep_dirty and len(dirty_head) > 0:
398
 
            yield {'saved_lines': saved_lines,
399
 
                   'dirty_head': dirty_head}
400
 
        else:
401
 
            yield saved_lines
 
414
        yield saved_lines
402
415
 
403
416
 
404
417
def iter_lines_handle_nl(iter_lines):
422
435
        yield last_line
423
436
 
424
437
 
425
 
def parse_patches(iter_lines, allow_dirty=False, keep_dirty=False):
 
438
def parse_patches(iter_lines, allow_dirty=False):
426
439
    '''
427
440
    :arg iter_lines: iterable of lines to parse for patches
428
441
    :kwarg allow_dirty: If True, allow text that's not part of the patch at
429
442
        selected places.  This includes comments before and after a patch
430
443
        for instance.  Default False.
431
 
    :kwarg keep_dirty: If True, returns a dict of patches with dirty headers.
432
 
        Default False.
433
444
    '''
434
 
    patches = []
435
 
    for patch_lines in iter_file_patch(iter_lines, allow_dirty, keep_dirty):
436
 
        if 'dirty_head' in patch_lines:
437
 
            patches.append({'patch': parse_patch(
438
 
                patch_lines['saved_lines'], allow_dirty),
439
 
                            'dirty_head': patch_lines['dirty_head']})
440
 
        else:
441
 
            patches.append(parse_patch(patch_lines, allow_dirty))
442
 
    return patches
 
445
    return [parse_patch(f.__iter__(), allow_dirty) for f in
 
446
                        iter_file_patch(iter_lines, allow_dirty)]
443
447
 
444
448
 
445
449
def difference_index(atext, btext):