~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

MergeĀ upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import os
18
18
import re
597
597
    return s.hexdigest()
598
598
 
599
599
 
 
600
def size_sha_file(f):
 
601
    """Calculate the size and hexdigest of an open file.
 
602
 
 
603
    The file cursor should be already at the start and
 
604
    the caller is responsible for closing the file afterwards.
 
605
    """
 
606
    size = 0
 
607
    s = sha()
 
608
    BUFSIZE = 128<<10
 
609
    while True:
 
610
        b = f.read(BUFSIZE)
 
611
        if not b:
 
612
            break
 
613
        size += len(b)
 
614
        s.update(b)
 
615
    return size, s.hexdigest()
 
616
 
 
617
 
600
618
def sha_file_by_name(fname):
601
619
    """Calculate the SHA1 of a file by reading the full text"""
602
620
    s = sha()
1722
1740
                continue
1723
1741
            raise
1724
1742
 
 
1743
def re_compile_checked(re_string, flags=0, where=""):
 
1744
    """Return a compiled re, or raise a sensible error.
 
1745
    
 
1746
    This should only be used when compiling user-supplied REs.
 
1747
 
 
1748
    :param re_string: Text form of regular expression.
 
1749
    :param flags: eg re.IGNORECASE
 
1750
    :param where: Message explaining to the user the context where 
 
1751
        it occurred, eg 'log search filter'.
 
1752
    """
 
1753
    # from https://bugs.launchpad.net/bzr/+bug/251352
 
1754
    try:
 
1755
        re_obj = re.compile(re_string, flags)
 
1756
        re_obj.search("")
 
1757
        return re_obj
 
1758
    except re.error, e:
 
1759
        if where:
 
1760
            where = ' in ' + where
 
1761
        # despite the name 'error' is a type
 
1762
        raise errors.BzrCommandError('Invalid regular expression%s: %r: %s'
 
1763
            % (where, re_string, e))
 
1764
 
1725
1765
 
1726
1766
if sys.platform == "win32":
1727
1767
    import msvcrt