@@ -1649,22 +1649,34 @@ def close(self, save_to=None):
1649
1649
# This makes later close() a no-op
1650
1650
self .folder = None
1651
1651
1652
- def checksum_copy (file_path , # type: IO
1653
- copy_to_fp = None , # type: Optional[IO]
1652
+ def checksum_copy (src_file , # type: IO
1653
+ dst_file = None , # type: Optional[IO]
1654
1654
hasher = Hasher , # type: Callable[[], Any]
1655
1655
buffersize = 1024 * 1024 # type: int
1656
1656
): # type: (...) -> str
1657
1657
"""Compute checksums while copying a file."""
1658
1658
# TODO: Use hashlib.new(Hasher_str) instead?
1659
1659
checksum = hasher ()
1660
- contents = file_path .read (buffersize )
1660
+ contents = src_file .read (buffersize )
1661
+ if dst_file and hasattr (dst_file , "name" ) and hasattr (src_file , "name" ):
1662
+ temp_location = os .path .join (os .path .dirname (dst_file .name ),
1663
+ str (uuid .uuid4 ()))
1664
+ try :
1665
+ os .rename (dst_file .name , temp_location )
1666
+ os .link (src_file .name , dst_file .name )
1667
+ dst_file = None
1668
+ os .unlink (temp_location )
1669
+ except OSError :
1670
+ pass
1671
+ if os .path .exists (temp_location ):
1672
+ os .rename (temp_location , dst_file .name ) # type: ignore
1661
1673
while contents != b"" :
1662
- if copy_to_fp is not None :
1663
- copy_to_fp .write (contents )
1674
+ if dst_file :
1675
+ dst_file .write (contents )
1664
1676
checksum .update (contents )
1665
- contents = file_path .read (buffersize )
1666
- if copy_to_fp is not None :
1667
- copy_to_fp .flush ()
1677
+ contents = src_file .read (buffersize )
1678
+ if dst_file is not None :
1679
+ dst_file .flush ()
1668
1680
return checksum .hexdigest ().lower ()
1669
1681
1670
1682
def copy_job_order (job , job_order_object ):
0 commit comments