/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: Jelmer Vernooij
  • Date: 2018-04-02 00:52:27 UTC
  • mfrom: (6939 work)
  • mto: This revision was merged to the branch mainline in revision 7274.
  • Revision ID: jelmer@jelmer.uk-20180402005227-pecflp1mvdjrjqd6
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from .lazy_import import lazy_import
28
28
lazy_import(globals(), """
29
29
from datetime import datetime
30
 
from datetime import timedelta
31
30
import getpass
32
31
import locale
33
32
import ntpath
593
592
        F = realpath
594
593
    else:
595
594
        F = abspath
596
 
    [p,e] = os.path.split(f)
 
595
    [p, e] = os.path.split(f)
597
596
    if e == "" or e == "." or e == "..":
598
597
        return F(f)
599
598
    else:
736
735
        yield b
737
736
 
738
737
 
 
738
# GZ 2017-09-16: Makes sense in general for hexdigest() result to be text, but
 
739
# used as bytes through most interfaces so encode with this wrapper.
 
740
if PY3:
 
741
    def _hexdigest(hashobj):
 
742
        return hashobj.hexdigest().encode()
 
743
else:
 
744
    def _hexdigest(hashobj):
 
745
        return hashobj.hexdigest()
 
746
 
 
747
 
739
748
def sha_file(f):
740
749
    """Calculate the hexdigest of an open file.
741
750
 
748
757
        if not b:
749
758
            break
750
759
        s.update(b)
751
 
    return s.hexdigest()
 
760
    return _hexdigest(s)
752
761
 
753
762
 
754
763
def size_sha_file(f):
766
775
            break
767
776
        size += len(b)
768
777
        s.update(b)
769
 
    return size, s.hexdigest()
 
778
    return size, _hexdigest(s)
770
779
 
771
780
 
772
781
def sha_file_by_name(fname):
777
786
        while True:
778
787
            b = os.read(f, 1<<16)
779
788
            if not b:
780
 
                return s.hexdigest()
 
789
                return _hexdigest(s)
781
790
            s.update(b)
782
791
    finally:
783
792
        os.close(f)
788
797
    s = _factory()
789
798
    for string in strings:
790
799
        s.update(string)
791
 
    return s.hexdigest()
 
800
    return _hexdigest(s)
792
801
 
793
802
 
794
803
def sha_string(f, _factory=sha):
795
 
    return _factory(f).hexdigest()
 
804
    # GZ 2017-09-16: Dodgy if factory is ever not sha, probably shouldn't be.
 
805
    return _hexdigest(_factory(f))
796
806
 
797
807
 
798
808
def fingerprint_file(f):
799
809
    b = f.read()
800
810
    return {'size': len(b),
801
 
            'sha1': sha(b).hexdigest()}
 
811
            'sha1': _hexdigest(sha(b))}
802
812
 
803
813
 
804
814
def compare_files(a, b):
813
823
            return True
814
824
 
815
825
 
816
 
def gmtime(seconds=None):
817
 
    """Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.
818
 
    GMT). When 'seconds' is not passed in, convert the current time instead.
819
 
    Handy replacement for time.gmtime() buggy on Windows and 32-bit platforms.
820
 
    """
821
 
    if seconds is None:
822
 
        seconds = time.time()
823
 
    return (datetime(1970, 1, 1) + timedelta(seconds=seconds)).timetuple()
824
 
 
825
 
 
826
826
def local_time_offset(t=None):
827
827
    """Return offset of local zone from GMT, either at present or at time t."""
828
828
    if t is None:
868
868
    """
869
869
    if offset is None:
870
870
        offset = 0
871
 
    tt = gmtime(t + offset)
 
871
    tt = time.gmtime(t + offset)
872
872
    date_fmt = _default_format_by_weekday_num[tt[6]]
873
873
    date_str = time.strftime(date_fmt, tt)
874
874
    offset_str = _cache.get(offset, None)
900
900
 
901
901
def _format_date(t, offset, timezone, date_fmt, show_offset):
902
902
    if timezone == 'utc':
903
 
        tt = gmtime(t)
 
903
        tt = time.gmtime(t)
904
904
        offset = 0
905
905
    elif timezone == 'original':
906
906
        if offset is None:
907
907
            offset = 0
908
 
        tt = gmtime(t + offset)
 
908
        tt = time.gmtime(t + offset)
909
909
    elif timezone == 'local':
910
910
        tt = time.localtime(t)
911
911
        offset = local_time_offset(t)
921
921
 
922
922
 
923
923
def compact_date(when):
924
 
    return time.strftime('%Y%m%d%H%M%S', gmtime(when))
 
924
    return time.strftime('%Y%m%d%H%M%S', time.gmtime(when))
925
925
 
926
926
 
927
927
def format_delta(delta):
1937
1937
        link_to = os.readlink(source)
1938
1938
        os.symlink(link_to, dest)
1939
1939
 
1940
 
    real_handlers = {'file':shutil.copy2,
1941
 
                     'symlink':copy_link,
1942
 
                     'directory':copy_dir,
 
1940
    real_handlers = {'file': shutil.copy2,
 
1941
                     'symlink': copy_link,
 
1942
                     'directory': copy_dir,
1943
1943
                    }
1944
1944
    real_handlers.update(handlers)
1945
1945
 
1981
1981
 
1982
1982
    This can be used to sort paths in the same way that walkdirs does.
1983
1983
    """
1984
 
    return (dirname(path) , path)
 
1984
    return (dirname(path), path)
1985
1985
 
1986
1986
 
1987
1987
def compare_paths_prefix_order(path_a, path_b):