2222import time
2323from typing import List , Tuple , Optional , ByteString
2424
25- from .local_changes import LocalChange , LocalChanges
25+ from .local_changes import FileChange , LocalPojectChanges
2626
2727from .common import UPLOAD_CHUNK_ATTEMPT_WAIT , UPLOAD_CHUNK_ATTEMPTS , UPLOAD_CHUNK_SIZE , ClientError , ErrorCode
2828from .merginproject import MerginProject
@@ -66,7 +66,7 @@ def __init__(
6666
6767 self ._request_headers = {"Content-Type" : "application/octet-stream" }
6868
69- def upload_chunk (self , data : ByteString , checksum : str ):
69+ def upload_chunk_v1_api (self , data : ByteString , checksum : str ):
7070 """
7171 Uploads the chunk to the server.
7272 """
@@ -118,7 +118,7 @@ def upload_blocking(self):
118118 self .upload_chunk_v2_api (data , checksum_str )
119119 else :
120120 # use v1 API for uploading chunks
121- self .upload_chunk (data , checksum_str )
121+ self .upload_chunk_v1_api (data , checksum_str )
122122 break # exit loop if upload was successful
123123 except ClientError as e :
124124 if attempt < UPLOAD_CHUNK_ATTEMPTS - 1 :
@@ -133,10 +133,10 @@ class UploadJob:
133133 """Keeps all the important data about a pending upload job"""
134134
135135 def __init__ (
136- self , version : str , changes : LocalChanges , transaction_id : Optional [str ], mp : MerginProject , mc , tmp_dir
136+ self , version : str , changes : LocalPojectChanges , transaction_id : Optional [str ], mp : MerginProject , mc , tmp_dir
137137 ):
138138 self .version = version
139- self .changes : LocalChanges = changes # dictionary of local changes to the project
139+ self .changes : LocalPojectChanges = changes # dictionary of local changes to the project
140140 self .transaction_id = transaction_id # ID of the transaction assigned by the server
141141 self .total_size = 0 # size of data to upload (in bytes)
142142 self .transferred_size = 0 # size of data already uploaded (in bytes)
@@ -160,8 +160,8 @@ def _submit_item_to_thread(self, item: UploadQueueItem):
160160 future = self .executor .submit (_do_upload , item , self )
161161 self .futures .append (future )
162162
163- def add_items (self , items : List [UploadQueueItem ]):
164- """Add multiple chunks to the upload queue """
163+ def start (self , items : List [UploadQueueItem ]):
164+ """Starting upload in background with multiple upload items (UploadQueueItem) """
165165 self .total_size = sum (item .size for item in items )
166166 self .upload_queue_items = items
167167
@@ -175,7 +175,10 @@ def add_items(self, items: List[UploadQueueItem]):
175175 self ._submit_item_to_thread (item )
176176
177177 def update_chunks_from_items (self ):
178- """Update chunks in LocalChanges from the upload queue items."""
178+ """
179+ Update chunks in LocalChanges from the upload queue items.
180+ Used just before finalizing the transaction to set the server_chunk_id in v2 API.
181+ """
179182 self .changes .update_chunks ([(item .chunk_id , item .server_chunk_id ) for item in self .upload_queue_items ])
180183
181184
@@ -188,7 +191,7 @@ def _do_upload(item: UploadQueueItem, job: UploadJob):
188191 job .transferred_size += item .size
189192
190193
191- def create_upload_chunks (mc , mp : MerginProject , local_changes : List [LocalChange ]) -> List [UploadQueueItem ]:
194+ def create_upload_chunks (mc , mp : MerginProject , local_changes : List [FileChange ]) -> List [UploadQueueItem ]:
192195 """
193196 Create a list of UploadQueueItem objects from the changes dictionary and calculate total size of files.
194197 This is used to prepare the upload queue for the upload job.
@@ -221,7 +224,7 @@ def create_upload_chunks(mc, mp: MerginProject, local_changes: List[LocalChange]
221224
222225
223226def create_upload_job (
224- mc , mp : MerginProject , changes : LocalChanges , tmp_dir : tempfile .TemporaryDirectory
227+ mc , mp : MerginProject , changes : LocalPojectChanges , tmp_dir : tempfile .TemporaryDirectory
225228) -> Optional [UploadJob ]:
226229 """
227230 Prepare transaction and create an upload job for the project using the v1 API.
@@ -261,18 +264,20 @@ def create_upload_job(
261264 if not upload_changes :
262265 mp .log .info ("not uploading any files" )
263266 if push_start_resp :
267+ # This is related just to v1 API
264268 job .server_resp = push_start_resp
265269 push_project_finalize (job )
266270 return # all done - no pending job
267271
268272 if transaction_id :
273+ # This is related just to v1 API
269274 mp .log .info (f"got transaction ID { transaction_id } " )
270275
271276 # prepare file chunks for upload
272277 upload_queue_items = create_upload_chunks (mc , mp , upload_changes )
273278
274279 mp .log .info (f"Starting upload chunks for project { project_id } " )
275- job .add_items (upload_queue_items )
280+ job .start (upload_queue_items )
276281 return job
277282
278283
@@ -312,10 +317,10 @@ def push_project_async(mc, directory) -> Optional[UploadJob]:
312317 if mp .is_versioned_file (f ["path" ]):
313318 mp .copy_versioned_file_for_upload (f , tmp_dir .name )
314319
315- local_changes = LocalChanges (
316- added = [LocalChange (** change ) for change in changes ["added" ]],
317- updated = [LocalChange (** change ) for change in changes ["updated" ]],
318- removed = [LocalChange (** change ) for change in changes ["removed" ]],
320+ local_changes = LocalPojectChanges (
321+ added = [FileChange (** change ) for change in changes ["added" ]],
322+ updated = [FileChange (** change ) for change in changes ["updated" ]],
323+ removed = [FileChange (** change ) for change in changes ["removed" ]],
319324 )
320325 job = create_upload_job (mc , mp , local_changes , tmp_dir )
321326 return job
@@ -449,6 +454,7 @@ def push_project_cancel(job: UploadJob):
449454
450455 job .executor .shutdown (wait = True )
451456 if not job .transaction_id :
457+ # If not v2 api endpoint with transaction, nothing to cancel on server
452458 job .mp .log .info ("--- push cancelled" )
453459 return
454460 try :
0 commit comments