~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: John Arbash Meinel
  • Date: 2009-02-25 21:13:22 UTC
  • mto: This revision was merged to the branch mainline in revision 4051.
  • Revision ID: john@arbash-meinel.com-20090225211322-qc94czk3s1g7nliq
Some direct tests for _group_keys_for_io

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#
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
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 
import re
18
 
 
19
 
 
20
 
binary_files_re = 'Binary files (.*) and (.*) differ\n'
21
 
 
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.')
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
17
 
30
18
 
31
19
class PatchSyntax(Exception):
69
57
def get_patch_names(iter_lines):
70
58
    try:
71
59
        line = iter_lines.next()
72
 
        match = re.match(binary_files_re, line)
73
 
        if match is not None:
74
 
            raise BinaryFiles(match.group(1), match.group(2))
75
60
        if not line.startswith("--- "):
76
61
            raise MalformedPatchHeader("No orig name", line)
77
62
        else:
179
164
        return InsertLine(line[1:])
180
165
    elif line.startswith("-"):
181
166
        return RemoveLine(line[1:])
 
167
    elif line == NO_NL:
 
168
        return NO_NL
182
169
    else:
183
170
        raise MalformedLine("Unknown line type", line)
184
171
__pychecker__=""
274
261
        yield hunk
275
262
 
276
263
 
277
 
class BinaryPatch(object):
 
264
class Patch:
278
265
    def __init__(self, oldname, newname):
279
266
        self.oldname = oldname
280
267
        self.newname = newname
281
 
 
282
 
    def __str__(self):
283
 
        return 'Binary files %s and %s differ\n' % (self.oldname, self.newname)
284
 
 
285
 
 
286
 
class Patch(BinaryPatch):
287
 
 
288
 
    def __init__(self, oldname, newname):
289
 
        BinaryPatch.__init__(self, oldname, newname)
290
268
        self.hunks = []
291
269
 
292
270
    def __str__(self):
340
318
 
341
319
 
342
320
def parse_patch(iter_lines):
343
 
    iter_lines = iter_lines_handle_nl(iter_lines)
344
 
    try:
345
 
        (orig_name, mod_name) = get_patch_names(iter_lines)
346
 
    except BinaryFiles, e:
347
 
        return BinaryPatch(e.orig_name, e.mod_name)
348
 
    else:
349
 
        patch = Patch(orig_name, mod_name)
350
 
        for hunk in iter_hunks(iter_lines):
351
 
            patch.hunks.append(hunk)
352
 
        return patch
 
321
    (orig_name, mod_name) = get_patch_names(iter_lines)
 
322
    patch = Patch(orig_name, mod_name)
 
323
    for hunk in iter_hunks(iter_lines):
 
324
        patch.hunks.append(hunk)
 
325
    return patch
353
326
 
354
327
 
355
328
def iter_file_patch(iter_lines):
356
 
    regex = re.compile(binary_files_re)
357
329
    saved_lines = []
358
330
    orig_range = 0
359
331
    for line in iter_lines:
364
336
        elif orig_range > 0:
365
337
            if line.startswith('-') or line.startswith(' '):
366
338
                orig_range -= 1
367
 
        elif line.startswith('--- ') or regex.match(line):
 
339
        elif line.startswith('--- '):
368
340
            if len(saved_lines) > 0:
369
341
                yield saved_lines
370
342
            saved_lines = []
398
370
 
399
371
 
400
372
def parse_patches(iter_lines):
 
373
    iter_lines = iter_lines_handle_nl(iter_lines)
401
374
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
402
375
 
403
376