2525from .common import CHUNK_SIZE , ClientError , DeltaChangeType , PullActionType
2626from .models import ProjectDelta , ProjectDeltaChange , PullAction
2727from .merginproject import MerginProject
28- from .utils import cleanup_tmp_dir , save_to_file , long_path
28+ from .utils import cleanup_tmp_dir , save_to_file
29+ from . import fs
2930from typing import List , Optional
3031
3132# status = download_project_async(...)
@@ -93,9 +94,7 @@ def __init__(self, file_path, size, version, diff_only, part_index, download_fil
9394 self .version = version # version of the file ("v123")
9495 self .diff_only = diff_only # whether downloading diff or full version
9596 self .part_index = part_index # index of the chunk
96- self .download_file_path = long_path (
97- download_file_path
98- ) # full path to a temporary file which will receive the content
97+ self .download_file_path = download_file_path # full path to a temporary file which will receive the content
9998
10099 def __repr__ (self ):
101100 return "<DownloadQueueItem path={} version={} diff_only={} part_index={} size={} dest={}>" .format (
@@ -130,9 +129,7 @@ class DownloadDiffQueueItem:
130129
131130 def __init__ (self , diff_id , download_file_path ):
132131 self .diff_id = diff_id # relative path to the file within project
133- self .download_file_path = long_path (
134- download_file_path
135- ) # full path to a temporary file which will receive the content
132+ self .download_file_path = download_file_path # full path to a temporary file which will receive the content
136133 self .size = 0 # size of the item in bytes
137134
138135 def __repr__ (self ):
@@ -146,7 +143,7 @@ def download_blocking(self, mc, mp):
146143 if resp .status in [200 , 206 ]:
147144 mp .log .debug (f"Download finished: { self .diff_id } " )
148145 save_to_file (resp , self .download_file_path )
149- self .size = os . path .getsize (self .download_file_path )
146+ self .size = fs .getsize (self .download_file_path )
150147 else :
151148 mp .log .error (f"Download failed: { self .diff_id } " )
152149 raise ClientError (f"Failed to download of diff file { self .diff_id } to { self .download_file_path } " )
@@ -161,26 +158,26 @@ class DownloadFile:
161158 """
162159
163160 def __init__ (self , dest_file , downloaded_items : typing .List [DownloadQueueItem ], size_check = True ):
164- self .dest_file = long_path ( dest_file ) # full path to the destination file to be created
161+ self .dest_file = dest_file # full path to the destination file to be created
165162 self .downloaded_items = downloaded_items # list of pieces of the destination file to be merged
166163 self .size_check = size_check # whether we want to do merged file size check
167164
168165 def from_chunks (self ):
169166 """Merges downloaded chunks into a single file at dest_file path"""
170167 file_dir = os .path .dirname (self .dest_file )
171- os .makedirs (file_dir , exist_ok = True )
168+ fs .makedirs (file_dir , exist_ok = True )
172169
173- with open (self .dest_file , "wb" ) as final :
170+ with fs . open_file (self .dest_file , "wb" ) as final :
174171 for item in self .downloaded_items :
175- with open (item .download_file_path , "rb" ) as chunk :
172+ with fs . open_file (item .download_file_path , "rb" ) as chunk :
176173 shutil .copyfileobj (chunk , final )
177- os .remove (item .download_file_path )
174+ fs .remove (item .download_file_path )
178175
179176 if not self .size_check :
180177 return
181178 expected_size = sum (item .size for item in self .downloaded_items )
182- if os . path .getsize (self .dest_file ) != expected_size :
183- os .remove (self .dest_file )
179+ if fs .getsize (self .dest_file ) != expected_size :
180+ fs .remove (self .dest_file )
184181 raise ClientError ("Download of file {} failed. Please try it again." .format (self .dest_file ))
185182
186183
@@ -200,7 +197,7 @@ def get_download_items(
200197
201198 items = []
202199 for part_index in range (chunks ):
203- download_file_path = long_path ( os .path .join (file_dir , basename + ".{}" .format (part_index ) ))
200+ download_file_path = os .path .join (file_dir , basename + ".{}" .format (part_index ))
204201 size = min (CHUNK_SIZE , file_size - part_index * CHUNK_SIZE )
205202 items .append (DownloadQueueItem (file_path , size , file_version , diff_only , part_index , download_file_path ))
206203
@@ -483,7 +480,7 @@ def get_download_diff_files(delta_item: ProjectDeltaChange, target_dir: str) ->
483480 result = []
484481
485482 for diff in delta_item .diffs :
486- dest_file_path = long_path ( os .path .normpath (os .path .join (target_dir , diff .id ) ))
483+ dest_file_path = os .path .normpath (os .path .join (target_dir , diff .id ))
487484 download_items = get_download_items (delta_item .path , diff .size , diff .version , target_dir , diff .id , True )
488485 result .append (DownloadFile (dest_file_path , download_items ))
489486 return result
@@ -559,7 +556,7 @@ def pull_project_async(mc, directory) -> Optional[PullJob]:
559556 pull_action_type == PullActionType .COPY_CONFLICT and change .type == DeltaChangeType .UPDATE_DIFF
560557 ):
561558 basefile = mp .fpath_meta (change .path )
562- if not os . path . exists (long_path ( basefile ) ):
559+ if not fs . exists (basefile ):
563560 # The basefile does not exist for some reason. This should not happen normally (maybe user removed the file
564561 # or we removed it within previous pull because we failed to apply patch the older version for some reason).
565562 # But it's not a problem - we will download the newest version and we're sorted.
@@ -726,7 +723,7 @@ def pull_project_finalize(job: PullJob):
726723 basefile = job .mp .fpath_meta (file_path )
727724 server_file = job .mp .fpath (file_path , job .tmp_dir .name )
728725
729- shutil .copy (long_path ( basefile ), long_path ( server_file ) )
726+ fs .copy (basefile , server_file )
730727 diffs = [job .mp .fpath (f , job .tmp_dir .name ) for f in file_diffs ]
731728 patch_error = job .mp .apply_diffs (server_file , diffs )
732729 if patch_error :
@@ -739,7 +736,7 @@ def pull_project_finalize(job: PullJob):
739736 job .mp .log .error ("Diffs we were applying: " + str (diffs ))
740737 job .mp .log .error ("Removing basefile because it would be corrupted anyway..." )
741738 job .mp .log .info ("--- pull aborted" )
742- os .remove (long_path ( basefile ) )
739+ fs .remove (basefile )
743740 raise ClientError ("Cannot patch basefile {}! Please try syncing again." .format (basefile ))
744741 conflicts = []
745742 job .mp .log .info (f"--- applying pull actions { job .pull_actions } " )
@@ -833,8 +830,8 @@ def download_diffs_async(mc, project_directory, file_path, versions):
833830 download_path = diff .get ("path" ),
834831 diff_only = True ,
835832 )
836- dest_file_path = long_path ( mp .fpath_cache (diff ["path" ], version = file ["version" ]) )
837- if os . path .exists (dest_file_path ):
833+ dest_file_path = mp .fpath_cache (diff ["path" ], version = file ["version" ])
834+ if fs .exists (dest_file_path ):
838835 continue
839836 download_files .append (DownloadFile (dest_file_path , items ))
840837 download_list .extend (items )
0 commit comments