/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1558.15.1 by Aaron Bentley
Add text_file function
1
from itertools import chain 
2
3
from bzrlib.errors import BinaryFile
4
from bzrlib.iterablefile import IterableFile
5
from bzrlib.osutils import file_iterator
6
def text_file(input):
7
    """Produce a file iterator that is guaranteed to be text, without seeking.
8
    BinaryFile is raised if the file contains a NUL in the first 1024 bytes.
9
    """
10
    first_chunk = input.read(1024)
11
    if '\x00' in first_chunk:
12
        raise BinaryFile()
13
    return IterableFile(chain((first_chunk,), file_iterator(input)))
1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
14
15
16
def check_text_lines(lines):
17
    """Raise BinaryFile if the supplied lines contain NULs.
18
    Only the first 1024 characters are checked.
19
    """
20
    f = IterableFile(lines)
21
    if '\x00' in f.read(1024):
22
        raise BinaryFile()
1558.15.3 by Aaron Bentley
Handle binary files for diff3 merges
23
24
def check_text_path(path):
25
    """Check whether the supplied path is a text, not binary file.
26
    Raise BinaryFile if a NUL occurs in the first 1024 bytes.
27
    """
28
    text_file(open(path, 'rb'))