/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

Merge bzr.dev r3466

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
 
149
148
    try:
150
149
        return _mapper(_lstat(f).st_mode)
151
150
    except OSError, e:
152
 
        if getattr(e, 'errno', None) == errno.ENOENT:
 
151
        if getattr(e, 'errno', None) in (errno.ENOENT, errno.ENOTDIR):
153
152
            raise errors.NoSuchFile(f)
154
153
        raise
155
154
 
468
467
        return pathjoin(F(p), e)
469
468
 
470
469
 
471
 
@deprecated_function(one_zero)
472
 
def backup_file(fn):
473
 
    """Copy a file to a backup.
474
 
 
475
 
    Backups are named in GNU-style, with a ~ suffix.
476
 
 
477
 
    If the file is already a backup, it's not copied.
478
 
    """
479
 
    if fn[-1] == '~':
480
 
        return
481
 
    bfn = fn + '~'
482
 
 
483
 
    if has_symlinks() and os.path.islink(fn):
484
 
        target = os.readlink(fn)
485
 
        os.symlink(target, bfn)
486
 
        return
487
 
    inf = file(fn, 'rb')
488
 
    try:
489
 
        content = inf.read()
490
 
    finally:
491
 
        inf.close()
492
 
    
493
 
    outf = file(bfn, 'wb')
494
 
    try:
495
 
        outf.write(content)
496
 
    finally:
497
 
        outf.close()
498
 
 
499
 
 
500
470
def isdir(f):
501
471
    """True if f is an accessible directory."""
502
472
    try:
559
529
    return False
560
530
 
561
531
 
562
 
def pumpfile(fromfile, tofile):
 
532
def pumpfile(from_file, to_file, read_length=-1, buff_size=32768):
563
533
    """Copy contents of one file to another.
564
 
    
 
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
 
565
541
    :return: The number of bytes copied.
566
542
    """
567
 
    BUFSIZE = 32768
568
543
    length = 0
569
 
    while True:
570
 
        b = fromfile.read(BUFSIZE)
571
 
        if not b:
572
 
            break
573
 
        tofile.write(b)
574
 
        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)
575
568
    return length
576
569
 
577
570
 
584
577
 
585
578
 
586
579
def sha_file(f):
587
 
    if getattr(f, 'tell', None) is not None:
588
 
        assert f.tell() == 0
 
580
    """Calculate the hexdigest of an open file.
 
581
 
 
582
    The file cursor should be already at the start.
 
583
    """
589
584
    s = sha.new()
590
585
    BUFSIZE = 128<<10
591
586
    while True:
779
774
 
780
775
def splitpath(p):
781
776
    """Turn string into list of parts."""
782
 
    assert isinstance(p, basestring)
783
 
 
784
777
    # split on either delimiter because people might use either on
785
778
    # Windows
786
779
    ps = re.split(r'[\\/]', p)
796
789
    return rps
797
790
 
798
791
def joinpath(p):
799
 
    assert isinstance(p, (list, tuple))
800
792
    for f in p:
801
793
        if (f == '..') or (f is None) or (f == ''):
802
794
            raise errors.BzrError("sorry, %r not allowed in path" % f)
896
888
    avoids that problem.
897
889
    """
898
890
 
899
 
    assert len(base) >= MIN_ABS_PATHLENGTH, ('Length of base must be equal or'
900
 
        ' exceed the platform minimum length (which is %d)' % 
901
 
        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,))
902
895
 
903
896
    rp = abspath(path)
904
897
 
1374
1367
    # Windows returns 'cp0' to indicate there is no code page. So we'll just
1375
1368
    # treat that as ASCII, and not support printing unicode characters to the
1376
1369
    # console.
1377
 
    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', ''):
1378
1373
        user_encoding = 'ascii'
1379
1374
    else:
1380
1375
        # check encoding