hgext/largefiles/lfutil.py
changeset 44062 2d49482d0dd4
parent 43584 a02e4c12ae60
child 44379 ca82929e433d
equal deleted inserted replaced
44061:cbc5755df6bf 44062:2d49482d0dd4
     9 '''largefiles utility code: must not import other modules in this package.'''
     9 '''largefiles utility code: must not import other modules in this package.'''
    10 from __future__ import absolute_import
    10 from __future__ import absolute_import
    11 
    11 
    12 import contextlib
    12 import contextlib
    13 import copy
    13 import copy
    14 import hashlib
       
    15 import os
    14 import os
    16 import stat
    15 import stat
    17 
    16 
    18 from mercurial.i18n import _
    17 from mercurial.i18n import _
    19 from mercurial.node import hex
    18 from mercurial.node import hex
    30     scmutil,
    29     scmutil,
    31     sparse,
    30     sparse,
    32     util,
    31     util,
    33     vfs as vfsmod,
    32     vfs as vfsmod,
    34 )
    33 )
       
    34 from mercurial.utils import hashutil
    35 
    35 
    36 shortname = b'.hglf'
    36 shortname = b'.hglf'
    37 shortnameslash = shortname + b'/'
    37 shortnameslash = shortname + b'/'
    38 longname = b'largefiles'
    38 longname = b'largefiles'
    39 
    39 
   430 
   430 
   431 
   431 
   432 def copyandhash(instream, outfile):
   432 def copyandhash(instream, outfile):
   433     '''Read bytes from instream (iterable) and write them to outfile,
   433     '''Read bytes from instream (iterable) and write them to outfile,
   434     computing the SHA-1 hash of the data along the way. Return the hash.'''
   434     computing the SHA-1 hash of the data along the way. Return the hash.'''
   435     hasher = hashlib.sha1(b'')
   435     hasher = hashutil.sha1(b'')
   436     for data in instream:
   436     for data in instream:
   437         hasher.update(data)
   437         hasher.update(data)
   438         outfile.write(data)
   438         outfile.write(data)
   439     return hex(hasher.digest())
   439     return hex(hasher.digest())
   440 
   440 
   470 
   470 
   471 
   471 
   472 def hexsha1(fileobj):
   472 def hexsha1(fileobj):
   473     """hexsha1 returns the hex-encoded sha1 sum of the data in the file-like
   473     """hexsha1 returns the hex-encoded sha1 sum of the data in the file-like
   474     object data"""
   474     object data"""
   475     h = hashlib.sha1()
   475     h = hashutil.sha1()
   476     for chunk in util.filechunkiter(fileobj):
   476     for chunk in util.filechunkiter(fileobj):
   477         h.update(chunk)
   477         h.update(chunk)
   478     return hex(h.digest())
   478     return hex(h.digest())
   479 
   479 
   480 
   480