maven_artifact: raise an explicit exception if the .md5 url returns 404 (#40477)

This commit is contained in:
S. Veyrié 2018-05-23 18:09:23 +02:00 committed by ansibot
parent a5f05c6fc2
commit 012003c871

View file

@ -384,10 +384,12 @@ class MavenDownloader:
response = self._request(url, "Failed to download artifact " + str(artifact)) response = self._request(url, "Failed to download artifact " + str(artifact))
with io.open(filename, 'wb') as f: with io.open(filename, 'wb') as f:
self._write_chunks(response, f, report_hook=self.chunk_report) self._write_chunks(response, f, report_hook=self.chunk_report)
if verify_download and not self.verify_md5(filename, url): if verify_download:
# if verify_change was set, the previous file would be deleted invalid_md5 = self.is_invalid_md5(filename, url)
os.remove(filename) if invalid_md5:
return "Checksum verification failed" # if verify_change was set, the previous file would be deleted
os.remove(filename)
return invalid_md5
return None return None
def chunk_report(self, bytes_so_far, chunk_size, total_size): def chunk_report(self, bytes_so_far, chunk_size, total_size):
@ -416,7 +418,7 @@ class MavenDownloader:
return bytes_so_far return bytes_so_far
def verify_md5(self, file, remote_url): def is_invalid_md5(self, file, remote_url):
if os.path.exists(file): if os.path.exists(file):
local_md5 = self._local_md5(file) local_md5 = self._local_md5(file)
if self.local: if self.local:
@ -424,8 +426,14 @@ class MavenDownloader:
remote_md5 = self._local_md5(parsed_url.path) remote_md5 = self._local_md5(parsed_url.path)
else: else:
remote_md5 = self._getContent(remote_url + '.md5', "Failed to retrieve MD5", False) remote_md5 = self._getContent(remote_url + '.md5', "Failed to retrieve MD5", False)
return local_md5 == remote_md5 if(not remote_md5):
return False return "Cannot find md5 from " + remote_url
if local_md5 == remote_md5:
return None
else:
return "Checksum does not match: we computed " + local_md5 + "but the repository states " + remote_md5
return "Path does not exist: " + file
def _local_md5(self, file): def _local_md5(self, file):
md5 = hashlib.md5() md5 = hashlib.md5()
@ -519,7 +527,7 @@ def main():
dest = posixpath.join(dest, "%s-%s.%s" % (artifact_id, version_part, extension)) dest = posixpath.join(dest, "%s-%s.%s" % (artifact_id, version_part, extension))
b_dest = to_bytes(dest, errors='surrogate_or_strict') b_dest = to_bytes(dest, errors='surrogate_or_strict')
if os.path.lexists(b_dest) and ((not verify_change) or downloader.verify_md5(dest, downloader.find_uri_for_artifact(artifact))): if os.path.lexists(b_dest) and ((not verify_change) or not downloader.is_invalid_md5(dest, downloader.find_uri_for_artifact(artifact))):
prev_state = "present" prev_state = "present"
if prev_state == "absent": if prev_state == "absent":