99from datetime import datetime
1010
1111this_dir = os .path .dirname (os .path .realpath (__file__ ))
12+ CHUNK_SIZE = 10 * 1024 * 1024
1213
1314try :
1415 from requests_toolbelt import MultipartEncoder
@@ -356,7 +357,7 @@ def download_project(self, project_path, directory):
356357 os .makedirs (directory )
357358
358359 project_info = self .project_info (project_path )
359- version = project_info ['version' ] if len ( project_info ['version' ]) else 'v0'
360+ version = project_info ['version' ] if project_info ['version' ] else 'v0'
360361
361362 for file in project_info ['files' ]:
362363 self ._download_file (project_path , version , file , directory )
@@ -468,7 +469,7 @@ def backup_if_conflict(path, checksum):
468469 move_file (local_path (file ["path" ]), local_path (file ["new_path" ]))
469470
470471 local_info ["files" ] = server_info ["files" ]
471- local_info ["version" ] = server_info ["version" ] if len ( server_info ["version" ]) else 'v0'
472+ local_info ["version" ] = server_info ["version" ] if server_info ["version" ] else 'v0'
472473 save_project_file (directory , local_info )
473474
474475 def _download_file (self , project_path , project_version , file , directory ):
@@ -484,7 +485,6 @@ def _download_file(self, project_path, project_version, file, directory):
484485 :param directory: Project's directory
485486 :type directory: String
486487 """
487- chunk_size = 10 * 1024 * 1024
488488 query_params = {
489489 "file" : file ['path' ],
490490 "version" : project_version
@@ -494,15 +494,14 @@ def _download_file(self, project_path, project_version, file, directory):
494494 length = 0
495495 count = 0
496496 while length < file ['size' ]:
497- range_header = {"Range" : "bytes={}-{}" .format (length , length + chunk_size )}
497+ range_header = {"Range" : "bytes={}-{}" .format (length , length + CHUNK_SIZE )}
498498 resp = self .get ("/v1/project/raw/{}" .format (project_path ), data = query_params , headers = range_header )
499- # TODO some kind of recovery? do_request already raises exception
500499 if resp .status in [200 , 206 ]:
501500 save_to_file (resp , os .path .join (file_dir , basename + ".{}" .format (count )))
502- length += (chunk_size + 1 )
501+ length += (CHUNK_SIZE + 1 )
503502 count += 1
504503
505- # merge chunks together (maybe do checksum check? (might be costly))
504+ # merge chunks together
506505 with open (os .path .join (file_dir , basename ), 'wb' ) as final :
507506 for i in range (count ):
508507 with open (os .path .join (directory , file ['path' ] + ".{}" .format (i )), 'rb' ) as chunk :
0 commit comments