/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

  • Committer: John Arbash Meinel
  • Date: 2008-07-08 14:55:19 UTC
  • mfrom: (3530 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3532.
  • Revision ID: john@arbash-meinel.com-20080708145519-paqg4kjwbpgs2xmq
Merge bzr.dev 3530

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
 
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:
646
641
    offset = datetime.fromtimestamp(t) - datetime.utcfromtimestamp(t)
647
642
    return offset.days * 86400 + offset.seconds
648
643
 
 
644
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
649
645
    
650
646
def format_date(t, offset=0, timezone='original', date_fmt=None,
651
647
                show_offset=True):
677
673
        offset_str = ' %+03d%02d' % (offset / 3600, (offset / 60) % 60)
678
674
    else:
679
675
        offset_str = ''
 
676
    # day of week depends on locale, so we do this ourself
 
677
    date_fmt = date_fmt.replace('%a', weekdays[tt[6]])
680
678
    return (time.strftime(date_fmt, tt) +  offset_str)
681
679
 
682
680
 
779
777
 
780
778
def splitpath(p):
781
779
    """Turn string into list of parts."""
782
 
    assert isinstance(p, basestring)
783
 
 
784
780
    # split on either delimiter because people might use either on
785
781
    # Windows
786
782
    ps = re.split(r'[\\/]', p)
796
792
    return rps
797
793
 
798
794
def joinpath(p):
799
 
    assert isinstance(p, (list, tuple))
800
795
    for f in p:
801
796
        if (f == '..') or (f is None) or (f == ''):
802
797
            raise errors.BzrError("sorry, %r not allowed in path" % f)
856
851
        return False
857
852
 
858
853
 
 
854
def host_os_dereferences_symlinks():
 
855
    return (has_symlinks()
 
856
            and sys.platform not in ('cygwin', 'win32'))
 
857
 
 
858
 
859
859
def contains_whitespace(s):
860
860
    """True if there are any whitespace characters in s."""
861
861
    # string.whitespace can include '\xa0' in certain locales, because it is
896
896
    avoids that problem.
897
897
    """
898
898
 
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)
 
899
    if len(base) < MIN_ABS_PATHLENGTH:
 
900
        # must have space for e.g. a drive letter
 
901
        raise ValueError('%r is too short to calculate a relative path'
 
902
            % (base,))
902
903
 
903
904
    rp = abspath(path)
904
905
 
1374
1375
    # Windows returns 'cp0' to indicate there is no code page. So we'll just
1375
1376
    # treat that as ASCII, and not support printing unicode characters to the
1376
1377
    # console.
1377
 
    if user_encoding in (None, 'cp0'):
 
1378
    #
 
1379
    # For python scripts run under vim, we get '', so also treat that as ASCII
 
1380
    if user_encoding in (None, 'cp0', ''):
1378
1381
        user_encoding = 'ascii'
1379
1382
    else:
1380
1383
        # check encoding