~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2008-06-11 02:36:40 UTC
  • mfrom: (3490 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3492.
  • Revision ID: mbp@sourcefrog.net-20080611023640-db0lqd75yueksdw7
Merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
from bzrlib import symbol_versioning
58
58
from bzrlib.symbol_versioning import (
59
59
    deprecated_function,
60
 
    one_zero,
61
60
    )
62
61
from bzrlib.trace import mutter
63
62
 
530
529
    return False
531
530
 
532
531
 
533
 
def pumpfile(fromfile, tofile):
 
532
def pumpfile(from_file, to_file, read_length=-1, buff_size=32768):
534
533
    """Copy contents of one file to another.
535
 
    
 
534
 
 
535
    The read_length can either be -1 to read to end-of-file (EOF) or
 
536
    it can specify the maximum number of bytes to read.
 
537
 
 
538
    The buff_size represents the maximum size for each read operation
 
539
    performed on from_file.
 
540
 
536
541
    :return: The number of bytes copied.
537
542
    """
538
 
    BUFSIZE = 32768
539
543
    length = 0
540
 
    while True:
541
 
        b = fromfile.read(BUFSIZE)
542
 
        if not b:
543
 
            break
544
 
        tofile.write(b)
545
 
        length += len(b)
 
544
    if read_length >= 0:
 
545
        # read specified number of bytes
 
546
 
 
547
        while read_length > 0:
 
548
            num_bytes_to_read = min(read_length, buff_size)
 
549
 
 
550
            block = from_file.read(num_bytes_to_read)
 
551
            if not block:
 
552
                # EOF reached
 
553
                break
 
554
            to_file.write(block)
 
555
 
 
556
            actual_bytes_read = len(block)
 
557
            read_length -= actual_bytes_read
 
558
            length += actual_bytes_read
 
559
    else:
 
560
        # read to EOF
 
561
        while True:
 
562
            block = from_file.read(buff_size)
 
563
            if not block:
 
564
                # EOF reached
 
565
                break
 
566
            to_file.write(block)
 
567
            length += len(block)
546
568
    return length
547
569
 
548
570
 
555
577
 
556
578
 
557
579
def sha_file(f):
558
 
    if getattr(f, 'tell', None) is not None:
559
 
        assert f.tell() == 0
 
580
    """Calculate the hexdigest of an open file.
 
581
 
 
582
    The file cursor should be already at the start.
 
583
    """
560
584
    s = sha.new()
561
585
    BUFSIZE = 128<<10
562
586
    while True:
750
774
 
751
775
def splitpath(p):
752
776
    """Turn string into list of parts."""
753
 
    assert isinstance(p, basestring)
754
 
 
755
777
    # split on either delimiter because people might use either on
756
778
    # Windows
757
779
    ps = re.split(r'[\\/]', p)
767
789
    return rps
768
790
 
769
791
def joinpath(p):
770
 
    assert isinstance(p, (list, tuple))
771
792
    for f in p:
772
793
        if (f == '..') or (f is None) or (f == ''):
773
794
            raise errors.BzrError("sorry, %r not allowed in path" % f)
867
888
    avoids that problem.
868
889
    """
869
890
 
870
 
    assert len(base) >= MIN_ABS_PATHLENGTH, ('Length of base must be equal or'
871
 
        ' exceed the platform minimum length (which is %d)' % 
872
 
        MIN_ABS_PATHLENGTH)
 
891
    if len(base) < MIN_ABS_PATHLENGTH:
 
892
        # must have space for e.g. a drive letter
 
893
        raise ValueError('%r is too short to calculate a relative path'
 
894
            % (base,))
873
895
 
874
896
    rp = abspath(path)
875
897
 
1345
1367
    # Windows returns 'cp0' to indicate there is no code page. So we'll just
1346
1368
    # treat that as ASCII, and not support printing unicode characters to the
1347
1369
    # console.
1348
 
    if user_encoding in (None, 'cp0'):
 
1370
    #
 
1371
    # For python scripts run under vim, we get '', so also treat that as ASCII
 
1372
    if user_encoding in (None, 'cp0', ''):
1349
1373
        user_encoding = 'ascii'
1350
1374
    else:
1351
1375
        # check encoding