~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Wouter van Heyst
  • Date: 2012-01-25 21:13:15 UTC
  • mto: (6437.3.27 2.5)
  • mto: This revision was merged to the branch mainline in revision 6451.
  • Revision ID: larstiq@larstiq.dyndns.org-20120125211315-ch74doua37bqet0i
mechanically replace file().write() pattern with a with-keyword version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006, 2008 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2005-2010 Aaron Bentley, Canonical Ltd
2
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
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
 
17
29
import re
18
30
 
19
31
 
20
32
binary_files_re = 'Binary files (.*) and (.*) differ\n'
21
33
 
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
 
 
69
34
def get_patch_names(iter_lines):
70
35
    try:
71
36
        line = iter_lines.next()
250
215
        return shift
251
216
 
252
217
 
253
 
def iter_hunks(iter_lines):
 
218
def iter_hunks(iter_lines, allow_dirty=False):
 
219
    '''
 
220
    :arg iter_lines: iterable of lines to parse for hunks
 
221
    :kwarg allow_dirty: If True, when we encounter something that is not
 
222
        a hunk header when we're looking for one, assume the rest of the lines
 
223
        are not part of the patch (comments or other junk).  Default False
 
224
    '''
254
225
    hunk = None
255
226
    for line in iter_lines:
256
227
        if line == "\n":
260
231
            continue
261
232
        if hunk is not None:
262
233
            yield hunk
263
 
        hunk = hunk_from_header(line)
 
234
        try:
 
235
            hunk = hunk_from_header(line)
 
236
        except MalformedHunkHeader:
 
237
            if allow_dirty:
 
238
                # If the line isn't a hunk header, then we've reached the end
 
239
                # of this patch and there's "junk" at the end.  Ignore the
 
240
                # rest of this patch.
 
241
                return
 
242
            raise
264
243
        orig_size = 0
265
244
        mod_size = 0
266
245
        while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
339
318
                    pos += 1
340
319
 
341
320
 
342
 
def parse_patch(iter_lines):
 
321
def parse_patch(iter_lines, allow_dirty=False):
 
322
    '''
 
323
    :arg iter_lines: iterable of lines to parse
 
324
    :kwarg allow_dirty: If True, allow the patch to have trailing junk.
 
325
        Default False
 
326
    '''
343
327
    iter_lines = iter_lines_handle_nl(iter_lines)
344
328
    try:
345
329
        (orig_name, mod_name) = get_patch_names(iter_lines)
347
331
        return BinaryPatch(e.orig_name, e.mod_name)
348
332
    else:
349
333
        patch = Patch(orig_name, mod_name)
350
 
        for hunk in iter_hunks(iter_lines):
 
334
        for hunk in iter_hunks(iter_lines, allow_dirty):
351
335
            patch.hunks.append(hunk)
352
336
        return patch
353
337
 
354
338
 
355
 
def iter_file_patch(iter_lines):
 
339
def iter_file_patch(iter_lines, allow_dirty=False):
 
340
    '''
 
341
    :arg iter_lines: iterable of lines to parse for patches
 
342
    :kwarg allow_dirty: If True, allow comments and other non-patch text
 
343
        before the first patch.  Note that the algorithm here can only find
 
344
        such text before any patches have been found.  Comments after the
 
345
        first patch are stripped away in iter_hunks() if it is also passed
 
346
        allow_dirty=True.  Default False.
 
347
    '''
 
348
    ### FIXME: Docstring is not quite true.  We allow certain comments no
 
349
    # matter what, If they startwith '===', '***', or '#' Someone should
 
350
    # reexamine this logic and decide if we should include those in
 
351
    # allow_dirty or restrict those to only being before the patch is found
 
352
    # (as allow_dirty does).
356
353
    regex = re.compile(binary_files_re)
357
354
    saved_lines = []
358
355
    orig_range = 0
 
356
    beginning = True
359
357
    for line in iter_lines:
360
358
        if line.startswith('=== ') or line.startswith('*** '):
361
359
            continue
365
363
            if line.startswith('-') or line.startswith(' '):
366
364
                orig_range -= 1
367
365
        elif line.startswith('--- ') or regex.match(line):
368
 
            if len(saved_lines) > 0:
 
366
            if allow_dirty and beginning:
 
367
                # Patches can have "junk" at the beginning
 
368
                # Stripping junk from the end of patches is handled when we
 
369
                # parse the patch
 
370
                beginning = False
 
371
            elif len(saved_lines) > 0:
369
372
                yield saved_lines
370
373
            saved_lines = []
371
374
        elif line.startswith('@@'):
397
400
        yield last_line
398
401
 
399
402
 
400
 
def parse_patches(iter_lines):
401
 
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
 
403
def parse_patches(iter_lines, allow_dirty=False):
 
404
    '''
 
405
    :arg iter_lines: iterable of lines to parse for patches
 
406
    :kwarg allow_dirty: If True, allow text that's not part of the patch at
 
407
        selected places.  This includes comments before and after a patch
 
408
        for instance.  Default False.
 
409
    '''
 
410
    return [parse_patch(f.__iter__(), allow_dirty) for f in
 
411
                        iter_file_patch(iter_lines, allow_dirty)]
402
412
 
403
413
 
404
414
def difference_index(atext, btext):