def fetch(self): if self.archive_file: tty.debug('Already downloaded {0}'.format(self.archive_file)) return parsed_url = url_util.parse(self.url) if parsed_url.scheme != 's3': raise FetchError( 'S3FetchStrategy can only fetch from s3:// urls.') tty.debug('Fetching {0}'.format(self.url)) basename = os.path.basename(parsed_url.path) with working_dir(self.stage.path): _, headers, stream = web_util.read_from_url(self.url) with open(basename, 'wb') as f: shutil.copyfileobj(stream, f) content_type = web_util.get_header(headers, 'Content-type') if content_type == 'text/html': warn_content_type_mismatch(self.archive_file or "the archive") if self.stage.save_filename: os.rename( os.path.join(self.stage.path, basename), self.stage.save_filename) if not self.archive_file: raise FailedDownloadError(self.url)
def test_get_header(): headers = { 'Content-type': 'text/plain' } # looking up headers should just work like a plain dict # lookup when there is an entry with the right key assert(web_util.get_header(headers, 'Content-type') == 'text/plain') # looking up headers should still work if there is a fuzzy match assert(web_util.get_header(headers, 'contentType') == 'text/plain') # ...unless there is an exact match for the "fuzzy" spelling. headers['contentType'] = 'text/html' assert(web_util.get_header(headers, 'contentType') == 'text/html') # If lookup has to fallback to fuzzy matching and there are more than one # fuzzy match, the result depends on the internal ordering of the given # mapping headers = OrderedDict() headers['Content-type'] = 'text/plain' headers['contentType'] = 'text/html' assert(web_util.get_header(headers, 'CONTENT_TYPE') == 'text/plain') del headers['Content-type'] assert(web_util.get_header(headers, 'CONTENT_TYPE') == 'text/html') # Same as above, but different ordering headers = OrderedDict() headers['contentType'] = 'text/html' headers['Content-type'] = 'text/plain' assert(web_util.get_header(headers, 'CONTENT_TYPE') == 'text/html') del headers['contentType'] assert(web_util.get_header(headers, 'CONTENT_TYPE') == 'text/plain') # If there isn't even a fuzzy match, raise KeyError with pytest.raises(KeyError): web_util.get_header(headers, 'ContentLength')