Skip to content

Commit 81e4195

Browse files
committed
Naming downloaded file to have 'partial' extension during download, then removing when done
1 parent 70d698d commit 81e4195

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

src/BaseSpacePy/model/MultipartDownload.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def run(self):
134134
self.result_queue.put(True)
135135
break
136136
else:
137-
LOGGER.debug("Worker %s retrying task %s after failure, retry attempt %d, with error msg: %s" % self.name, str(next_task), i, answer.err_msg)
137+
LOGGER.debug("Worker %s retrying task %s after failure, retry attempt %d, with error msg: %s" % (self.name, str(next_task), i, answer.err_msg))
138138
time.sleep(self.retry_wait)
139139
if not answer.success == True:
140140
LOGGER.debug("Worker %s exiting, too many failures with retry for worker %s" % self.name, str(self))
@@ -232,21 +232,30 @@ def __init__(self, api, fileId, local_path, process_count, part_size, start_chun
232232
self.temp_dir = temp_dir
233233
self.start_chunk = start_chunk
234234
self.debug = debug
235+
236+
self.partial_file_ext = ".partial"
237+
235238
self.setup()
236-
self.start_download()
239+
self.start_download()
237240

238241
def setup(self):
239242
'''
240-
Determine number of file pieces to download, add download tasks to work queue
243+
Determine number of file pieces to download, add download tasks to work queue
244+
While download is in progress, name the file with a 'partial' extension
241245
'''
242246
bs_file = self.api.getFileById(self.fileId)
243247
self.file_name = bs_file.Name
244248
total_size = bs_file.Size
245249
self.file_count = int(math.ceil(total_size/self.part_size)) + 1
246250

251+
file_name = self.file_name
252+
if not self.debug:
253+
file_name = self.file_name + self.partial_file_ext
254+
247255
self.exe = Executor()
248256
for i in xrange(self.start_chunk,self.file_count+1):
249-
t = DownloadTask(self.api, self.fileId, self.file_name, self.local_path, i, self.part_size, total_size, self.temp_dir, self.debug)
257+
t = DownloadTask(self.api, self.fileId, file_name, self.local_path,
258+
i, self.part_size, total_size, self.temp_dir, self.debug)
250259
self.exe.add_task(t)
251260
self.exe.add_workers(self.process_count)
252261
self.task_total = self.file_count
@@ -264,9 +273,17 @@ def start_download(self):
264273
if self.debug:
265274
finalize_callback = self.combine_file_chunks
266275
else:
267-
finalize_callback = lambda: None
276+
finalize_callback = self.rename_final_file # lambda: None
268277
self.exe.start_workers(finalize_callback)
269278

279+
def rename_final_file(self):
280+
'''
281+
Remove the 'partial' extension from the downloaded file
282+
'''
283+
final_file = os.path.join(self.local_path, self.file_name)
284+
partial_file = final_file + self.partial_file_ext
285+
os.rename(partial_file, final_file)
286+
270287
def combine_file_chunks(self):
271288
'''
272289
Assembles download files chunks into single large file, then cleanup by deleting file chunks

0 commit comments

Comments
 (0)