/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 breezy/osutils.py

  • Committer: Martin
  • Date: 2017-11-11 15:06:09 UTC
  • mto: This revision was merged to the branch mainline in revision 6807.
  • Revision ID: gzlist@googlemail.com-20171111150609-zedlssleo8699m4u
Make groupcompress Python 3 compatible

Change hexdigest from osutils to return bytes as required by most callers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
736
736
        yield b
737
737
 
738
738
 
 
739
# GZ 2017-09-16: Makes sense in general for hexdigest() result to be text, but
 
740
# used as bytes through most interfaces so encode with this wrapper.
 
741
if PY3:
 
742
    def _hexdigest(hashobj):
 
743
        return hashobj.hexdigest().encode()
 
744
else:
 
745
    def _hexdigest(hashobj):
 
746
        return hashobj.hexdigest()
 
747
 
 
748
 
739
749
def sha_file(f):
740
750
    """Calculate the hexdigest of an open file.
741
751
 
748
758
        if not b:
749
759
            break
750
760
        s.update(b)
751
 
    return s.hexdigest()
 
761
    return _hexdigest(s)
752
762
 
753
763
 
754
764
def size_sha_file(f):
766
776
            break
767
777
        size += len(b)
768
778
        s.update(b)
769
 
    return size, s.hexdigest()
 
779
    return size, _hexdigest(s)
770
780
 
771
781
 
772
782
def sha_file_by_name(fname):
777
787
        while True:
778
788
            b = os.read(f, 1<<16)
779
789
            if not b:
780
 
                return s.hexdigest()
 
790
                return _hexdigest(s)
781
791
            s.update(b)
782
792
    finally:
783
793
        os.close(f)
788
798
    s = _factory()
789
799
    for string in strings:
790
800
        s.update(string)
791
 
    return s.hexdigest()
 
801
    return _hexdigest(s)
792
802
 
793
803
 
794
804
def sha_string(f, _factory=sha):
795
 
    return _factory(f).hexdigest()
 
805
    # GZ 2017-09-16: Dodgy if factory is ever not sha, probably shouldn't be.
 
806
    return _hexdigest(_factory(f))
796
807
 
797
808
 
798
809
def fingerprint_file(f):
799
810
    b = f.read()
800
811
    return {'size': len(b),
801
 
            'sha1': sha(b).hexdigest()}
 
812
            'sha1': _hexdigest(sha(b))}
802
813
 
803
814
 
804
815
def compare_files(a, b):