~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
87
87
        os.chmod(filename, mod)
88
88
 
89
89
 
 
90
def minimum_path_selection(paths):
 
91
    """Return the smallset subset of paths which are outside paths.
 
92
 
 
93
    :param paths: A container (and hence not None) of paths.
 
94
    :return: A set of paths sufficient to include everything in paths via
 
95
        is_inside_any, drawn from the paths parameter.
 
96
    """
 
97
    search_paths = set()
 
98
    paths = set(paths)
 
99
    for path in paths:
 
100
        other_paths = paths.difference([path])
 
101
        if not is_inside_any(other_paths, path):
 
102
            # this is a top level path, we must check it.
 
103
            search_paths.add(path)
 
104
    return search_paths
 
105
 
 
106
 
90
107
_QUOTE_RE = None
91
108
 
92
109
 
537
554
 
538
555
 
539
556
def pumpfile(fromfile, tofile):
540
 
    """Copy contents of one file to another."""
 
557
    """Copy contents of one file to another.
 
558
    
 
559
    :return: The number of bytes copied.
 
560
    """
541
561
    BUFSIZE = 32768
 
562
    length = 0
542
563
    while True:
543
564
        b = fromfile.read(BUFSIZE)
544
565
        if not b:
545
566
            break
546
567
        tofile.write(b)
 
568
        length += len(b)
 
569
    return length
547
570
 
548
571
 
549
572
def file_iterator(input_file, readsize=32768):
567
590
    return s.hexdigest()
568
591
 
569
592
 
570
 
 
571
 
def sha_strings(strings):
 
593
def sha_strings(strings, _factory=sha.new):
572
594
    """Return the sha-1 of concatenation of strings"""
573
 
    s = sha.new()
 
595
    s = _factory()
574
596
    map(s.update, strings)
575
597
    return s.hexdigest()
576
598
 
577
599
 
578
 
def sha_string(f):
579
 
    s = sha.new()
580
 
    s.update(f)
581
 
    return s.hexdigest()
 
600
def sha_string(f, _factory=sha.new):
 
601
    return _factory(f).hexdigest()
582
602
 
583
603
 
584
604
def fingerprint_file(f):
585
 
    s = sha.new()
586
605
    b = f.read()
587
 
    s.update(b)
588
 
    size = len(b)
589
 
    return {'size': size,
590
 
            'sha1': s.hexdigest()}
 
606
    return {'size': len(b),
 
607
            'sha1': sha.new(b).hexdigest()}
591
608
 
592
609
 
593
610
def compare_files(a, b):
792
809
            raise
793
810
        shutil.copyfile(src, dest)
794
811
 
795
 
def delete_any(full_path):
 
812
 
 
813
# Look Before You Leap (LBYL) is appropriate here instead of Easier to Ask for
 
814
# Forgiveness than Permission (EAFP) because:
 
815
# - root can damage a solaris file system by using unlink,
 
816
# - unlink raises different exceptions on different OSes (linux: EISDIR, win32:
 
817
#   EACCES, OSX: EPERM) when invoked on a directory.
 
818
def delete_any(path):
796
819
    """Delete a file or directory."""
797
 
    try:
798
 
        os.unlink(full_path)
799
 
    except OSError, e:
800
 
    # We may be renaming a dangling inventory id
801
 
        if e.errno not in (errno.EISDIR, errno.EACCES, errno.EPERM):
802
 
            raise
803
 
        os.rmdir(full_path)
 
820
    if isdir(path): # Takes care of symlinks
 
821
        os.rmdir(path)
 
822
    else:
 
823
        os.unlink(path)
804
824
 
805
825
 
806
826
def has_symlinks():
808
828
        return True
809
829
    else:
810
830
        return False
811
 
        
 
831
 
812
832
 
813
833
def contains_whitespace(s):
814
834
    """True if there are any whitespace characters in s."""