@@ -1612,22 +1612,34 @@ def close(self, save_to=None):
1612
1612
# This makes later close() a no-op
1613
1613
self .folder = None
1614
1614
1615
- def checksum_copy (file_path , # type: IO
1616
- copy_to_fp = None , # type: Optional[IO]
1615
+ def checksum_copy (src_file , # type: IO
1616
+ dst_file = None , # type: Optional[IO]
1617
1617
hasher = Hasher , # type: Callable[[], Any]
1618
1618
buffersize = 1024 * 1024 # type: int
1619
1619
): # type: (...) -> str
1620
1620
"""Compute checksums while copying a file."""
1621
1621
# TODO: Use hashlib.new(Hasher_str) instead?
1622
1622
checksum = hasher ()
1623
- contents = file_path .read (buffersize )
1623
+ contents = src_file .read (buffersize )
1624
+ if dst_file and hasattr (dst_file , "name" ) and hasattr (src_file , "name" ):
1625
+ temp_location = os .path .join (os .path .dirname (dst_file .name ),
1626
+ str (uuid .uuid4 ()))
1627
+ try :
1628
+ os .rename (dst_file .name , temp_location )
1629
+ os .link (src_file .name , dst_file .name )
1630
+ dst_file = None
1631
+ os .unlink (temp_location )
1632
+ except OSError :
1633
+ pass
1634
+ if os .path .exists (temp_location ):
1635
+ os .rename (temp_location , dst_file .name ) # type: ignore
1624
1636
while contents != b"" :
1625
- if copy_to_fp is not None :
1626
- copy_to_fp .write (contents )
1637
+ if dst_file :
1638
+ dst_file .write (contents )
1627
1639
checksum .update (contents )
1628
- contents = file_path .read (buffersize )
1629
- if copy_to_fp is not None :
1630
- copy_to_fp .flush ()
1640
+ contents = src_file .read (buffersize )
1641
+ if dst_file is not None :
1642
+ dst_file .flush ()
1631
1643
return checksum .hexdigest ().lower ()
1632
1644
1633
1645
def copy_job_order (job , job_order_object ):
0 commit comments