From 4e7772257b2985016e2daca095c4715d15d0670b Mon Sep 17 00:00:00 2001 From: Craig Davison Date: Wed, 26 May 2021 02:13:43 -0600 Subject: [PATCH 1/2] Improved resume Does not cause HTTP err 416 --- installinstallmacos.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/installinstallmacos.py b/installinstallmacos.py index 21b2929..177298c 100755 --- a/installinstallmacos.py +++ b/installinstallmacos.py @@ -260,6 +260,7 @@ def replicate_url(full_url, relative_url = path.lstrip('/') relative_url = os.path.normpath(relative_url) local_file_path = os.path.join(root_dir, relative_url) + tmp_file_path = local_file_path + ".partial" if show_progress: options = '-fL' else: @@ -267,37 +268,39 @@ def replicate_url(full_url, need_download = True while need_download: curl_cmd = ['/usr/bin/curl', options, - '--create-dirs', - '-o', local_file_path, - '-w', '%{http_code}'] + '-o', tmp_file_path, + '--create-dirs'] if not full_url.endswith(".gz"): # stupid hack for stupid Apple behavior where it sometimes returns # compressed files even when not asked for curl_cmd.append('--compressed') resumed = False if not ignore_cache and os.path.exists(local_file_path): - if not attempt_resume: - curl_cmd.extend(['-z', local_file_path]) - else: - resumed = True - curl_cmd.extend(['-z', '-' + local_file_path, '-C', '-']) + os.rename(local_file_path, tmp_file_path) + # Re-download file only if it was updated server-side + curl_cmd.extend(['-z', tmp_file_path]) + elif (not ignore_cache and attempt_resume and + os.path.exists(tmp_file_path)): + resumed = True + # Continue download only if file was not updated server-side + curl_cmd.extend(['-z', '-' + tmp_file_path, + '-C', '-', + '-w', '%{http_code}']) + curl_cmd.append(full_url) print("Downloading %s..." % full_url) need_download = False try: - output = subprocess.check_output(curl_cmd) + _ = subprocess.check_output(curl_cmd) except subprocess.CalledProcessError as err: - if not resumed or not err.output.isdigit(): - raise ReplicationError(err) - # HTTP error 416 on resume: the download is already complete and the - # file is up-to-date # HTTP error 412 on resume: the file was updated server-side - if int(err.output) == 412: - print("Removing %s and retrying." % local_file_path) - os.unlink(local_file_path) + if resumed and err.output.isdigit() and int(err.output) == 412: + print("Removing %s and retrying." % tmp_file_path) + os.unlink(tmp_file_path) need_download = True - elif int(err.output) != 416: + else: raise ReplicationError(err) + os.rename(tmp_file_path, local_file_path) return local_file_path From e1ac16f848aed59ba51a9154a039491fcdf771cb Mon Sep 17 00:00:00 2001 From: Craig Davison Date: Wed, 26 May 2021 12:29:50 -0600 Subject: [PATCH 2/2] reorder params --- installinstallmacos.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/installinstallmacos.py b/installinstallmacos.py index 177298c..7279124 100755 --- a/installinstallmacos.py +++ b/installinstallmacos.py @@ -268,8 +268,8 @@ def replicate_url(full_url, need_download = True while need_download: curl_cmd = ['/usr/bin/curl', options, - '-o', tmp_file_path, - '--create-dirs'] + '--create-dirs', + '-o', tmp_file_path] if not full_url.endswith(".gz"): # stupid hack for stupid Apple behavior where it sometimes returns # compressed files even when not asked for