2222import time
2323from typing import List , Tuple , Optional , ByteString
2424
25- from .local_changes import ChangesValidationError , LocalChange , LocalChanges
25+ from .local_changes import ChangesValidationError , FileChange , LocalPojectChanges
2626
2727from .common import (
2828 MAX_UPLOAD_VERSIONED_SIZE ,
@@ -73,7 +73,7 @@ def __init__(
7373
7474 self ._request_headers = {"Content-Type" : "application/octet-stream" }
7575
76- def upload_chunk (self , data : ByteString , checksum : str ):
76+ def upload_chunk_v1_api (self , data : ByteString , checksum : str ):
7777 """
7878 Uploads the chunk to the server.
7979 """
@@ -125,7 +125,7 @@ def upload_blocking(self):
125125 self .upload_chunk_v2_api (data , checksum_str )
126126 else :
127127 # use v1 API for uploading chunks
128- self .upload_chunk (data , checksum_str )
128+ self .upload_chunk_v1_api (data , checksum_str )
129129 break # exit loop if upload was successful
130130 except ClientError as e :
131131 if attempt < UPLOAD_CHUNK_ATTEMPTS - 1 :
@@ -140,10 +140,10 @@ class UploadJob:
140140 """Keeps all the important data about a pending upload job"""
141141
142142 def __init__ (
143- self , version : str , changes : LocalChanges , transaction_id : Optional [str ], mp : MerginProject , mc , tmp_dir
143+ self , version : str , changes : LocalPojectChanges , transaction_id : Optional [str ], mp : MerginProject , mc , tmp_dir
144144 ):
145145 self .version = version
146- self .changes : LocalChanges = changes # dictionary of local changes to the project
146+ self .changes : LocalPojectChanges = changes # dictionary of local changes to the project
147147 self .transaction_id = transaction_id # ID of the transaction assigned by the server
148148 self .total_size = 0 # size of data to upload (in bytes)
149149 self .transferred_size = 0 # size of data already uploaded (in bytes)
@@ -167,8 +167,8 @@ def _submit_item_to_thread(self, item: UploadQueueItem):
167167 future = self .executor .submit (_do_upload , item , self )
168168 self .futures .append (future )
169169
170- def add_items (self , items : List [UploadQueueItem ]):
171- """Add multiple chunks to the upload queue """
170+ def start (self , items : List [UploadQueueItem ]):
171+ """Starting upload in background with multiple upload items (UploadQueueItem) """
172172 self .total_size = sum (item .size for item in items )
173173 self .upload_queue_items = items
174174
@@ -182,7 +182,10 @@ def add_items(self, items: List[UploadQueueItem]):
182182 self ._submit_item_to_thread (item )
183183
184184 def update_chunks_from_items (self ):
185- """Update chunks in LocalChanges from the upload queue items."""
185+ """
186+ Update chunks in LocalProjectChanges from the upload queue items.
187+ Used just before finalizing the transaction to set the server_chunk_id in v2 API.
188+ """
186189 self .changes .update_chunks ([(item .chunk_id , item .server_chunk_id ) for item in self .upload_queue_items ])
187190
188191
@@ -195,7 +198,7 @@ def _do_upload(item: UploadQueueItem, job: UploadJob):
195198 job .transferred_size += item .size
196199
197200
198- def create_upload_chunks (mc , mp : MerginProject , local_changes : List [LocalChange ]) -> List [UploadQueueItem ]:
201+ def create_upload_chunks (mc , mp : MerginProject , local_changes : List [FileChange ]) -> List [UploadQueueItem ]:
199202 """
200203 Create a list of UploadQueueItem objects from the changes dictionary and calculate total size of files.
201204 This is used to prepare the upload queue for the upload job.
@@ -228,7 +231,7 @@ def create_upload_chunks(mc, mp: MerginProject, local_changes: List[LocalChange]
228231
229232
230233def create_upload_job (
231- mc , mp : MerginProject , changes : LocalChanges , tmp_dir : tempfile .TemporaryDirectory
234+ mc , mp : MerginProject , changes : LocalPojectChanges , tmp_dir : tempfile .TemporaryDirectory
232235) -> Optional [UploadJob ]:
233236 """
234237 Prepare transaction and create an upload job for the project using the v1 API.
@@ -268,18 +271,20 @@ def create_upload_job(
268271 if not upload_changes :
269272 mp .log .info ("not uploading any files" )
270273 if push_start_resp :
274+ # This is related just to v1 API
271275 job .server_resp = push_start_resp
272276 push_project_finalize (job )
273277 return # all done - no pending job
274278
275279 if transaction_id :
280+ # This is related just to v1 API
276281 mp .log .info (f"got transaction ID { transaction_id } " )
277282
278283 # prepare file chunks for upload
279284 upload_queue_items = create_upload_chunks (mc , mp , upload_changes )
280285
281286 mp .log .info (f"Starting upload chunks for project { project_id } " )
282- job .add_items (upload_queue_items )
287+ job .start (upload_queue_items )
283288 return job
284289
285290
@@ -451,6 +456,7 @@ def push_project_cancel(job: UploadJob):
451456
452457 job .executor .shutdown (wait = True )
453458 if not job .transaction_id :
459+ # If not v2 api endpoint with transaction, nothing to cancel on server
454460 job .mp .log .info ("--- push cancelled" )
455461 return
456462 try :
@@ -473,7 +479,7 @@ def remove_diff_files(job: UploadJob) -> None:
473479 os .remove (diff_file )
474480
475481
476- def get_push_changes_batch (mc , mp : MerginProject ) -> Tuple [LocalChanges , int ]:
482+ def get_push_changes_batch (mc , mp : MerginProject ) -> Tuple [LocalPojectChanges , int ]:
477483 """
478484 Get changes that need to be pushed to the server.
479485 """
@@ -482,10 +488,10 @@ def get_push_changes_batch(mc, mp: MerginProject) -> Tuple[LocalChanges, int]:
482488 changes = filter_changes (mc , project_role , changes )
483489
484490 try :
485- local_changes = LocalChanges (
486- added = [LocalChange (** change ) for change in changes ["added" ]],
487- updated = [LocalChange (** change ) for change in changes ["updated" ]],
488- removed = [LocalChange (** change ) for change in changes ["removed" ]],
491+ local_changes = LocalPojectChanges (
492+ added = [FileChange (** change ) for change in changes ["added" ]],
493+ updated = [FileChange (** change ) for change in changes ["updated" ]],
494+ removed = [FileChange (** change ) for change in changes ["removed" ]],
489495 )
490496 except ChangesValidationError as e :
491497 raise ClientError (
0 commit comments