def inside_directory_fail_to_rename_tmp_file(dirname): filename = os.path.join(dirname, "downloaded-file") with HttpServerTestContext() as server: url = server.new_download_url(download_length=56780, hash_algorithm='md5') download = FileDownloader(url=url, filename=filename, hash_algorithm='md5') def mock_rename(src, dest): raise OSError("FAIL") monkeypatch.setattr( 'anaconda_project.internal.rename.rename_over_existing', mock_rename) response = IOLoop.current().run_sync( lambda: download.run(IOLoop.current())) assert [ "Failed to rename %s to %s: FAIL" % (filename + ".part", filename) ] == download.errors assert response.code == 200 assert not os.path.isfile(filename) assert not os.path.isfile(filename + ".part")
def _provide_download(self, requirement, context, frontend): filename = context.status.analysis.existing_filename if filename is not None: frontend.info( "Previously downloaded file located at {}".format(filename)) return filename filename = os.path.abspath( os.path.join(context.environ['PROJECT_DIR'], requirement.filename)) if requirement.unzip: download_filename = filename + ".zip" else: download_filename = filename download = FileDownloader(url=requirement.url, filename=download_filename, hash_algorithm=requirement.hash_algorithm) try: _ioloop = IOLoop(make_current=False) response = _ioloop.run_sync(lambda: download.run(_ioloop)) if response is None: for error in download.errors: frontend.error(error) return None elif response.code == 200: if requirement.hash_value is not None and requirement.hash_value != download.hash: frontend.error( "Error downloading {}: mismatched hashes. Expected: {}, calculated: {}" .format(requirement.url, requirement.hash_value, download.hash)) return None if requirement.unzip: unzip_errors = [] if unpack_zip(download_filename, filename, unzip_errors): os.remove(download_filename) return filename else: for error in unzip_errors: frontend.error(error) return None return filename else: frontend.error("Error downloading {}: response code {}".format( requirement.url, response.code)) return None except Exception as e: frontend.error("Error downloading {}: {}".format( requirement.url, str(e))) return None finally: _ioloop.close()
def inside_directory_get_http_error(dirname): filename = os.path.join(dirname, "downloaded-file") with HttpServerTestContext() as server: url = server.error_url download = FileDownloader(url=url, filename=filename, hash_algorithm='md5') response = IOLoop.current().run_sync( lambda: download.run(IOLoop.current())) assert ['Failed download to %s: HTTP 404: Not Found' % filename ] == download.errors assert response is None assert not os.path.isfile(filename) assert not os.path.isfile(filename + ".part")
def inside_directory_download_file(dirname): filename = os.path.join(dirname, "downloaded-file") with HttpServerTestContext() as server: url = server.new_download_url(download_length=length, hash_algorithm=hash_algorithm) download = FileDownloader(url=url, filename=filename, hash_algorithm=hash_algorithm) response = IOLoop.current().run_sync( lambda: download.run(IOLoop.current())) assert [] == download.errors assert response is not None assert response.code == 200 if hash_algorithm: server_hash = server.server_computed_hash_for_downloaded_url( url) assert download.hash == server_hash statinfo = os.stat(filename) assert statinfo.st_size == length assert not os.path.isfile(filename + ".part")
def inside_directory_fail_to_open_file(dirname): statinfo = os.stat(dirname) try: filename = os.path.join(dirname, "downloaded-file") # make the open fail if platform.system() == 'Windows': # windows does not have read-only directories so we have to # make the file read-only with open(filename + ".part", 'wb') as file: file.write("".encode()) os.chmod(filename + ".part", stat.S_IREAD) else: os.chmod(dirname, stat.S_IREAD) with HttpServerTestContext() as server: url = server.error_url download = FileDownloader(url=url, filename=filename, hash_algorithm='md5') response = IOLoop.current().run_sync( lambda: download.run(IOLoop.current())) filename_with_weird_extra_slashes = filename if platform.system() == 'Windows': # I dunno. that's what Windows gives us. filename_with_weird_extra_slashes = filename.replace( "\\", "\\\\") assert [ "Failed to open %s.part: [Errno 13] Permission denied: '%s.part'" % (filename, filename_with_weird_extra_slashes) ] == download.errors assert response is None assert not os.path.isfile(filename) if platform.system() != 'Windows': # on windows we created this ourselves to cause the open error above assert not os.path.isfile(filename + ".part") finally: # put this back so we don't get an exception cleaning up the directory os.chmod(dirname, statinfo.st_mode) if platform.system() == 'Windows': os.chmod(filename + ".part", stat.S_IWRITE)
def inside_directory_fail_to_create_directory(dirname): def mock_makedirs(name): raise IOError("Cannot create %s" % name) monkeypatch.setattr( 'anaconda_project.internal.makedirs.makedirs_ok_if_exists', mock_makedirs) filename = os.path.join(dirname, "downloaded-file") with HttpServerTestContext() as server: url = server.error_url download = FileDownloader(url=url, filename=filename, hash_algorithm='md5') response = IOLoop.current().run_sync( lambda: download.run(IOLoop.current())) assert [ "Could not create directory '%s': Cannot create %s" % (dirname, dirname) ] == download.errors assert response is None assert not os.path.isfile(filename) assert not os.path.isfile(filename + ".part")
def inside_directory_fail_to_write_file(dirname): filename = os.path.join(dirname, "downloaded-file") with HttpServerTestContext() as server: url = server.new_download_url(download_length=(1024 * 1025), hash_algorithm='md5') download = FileDownloader(url=url, filename=filename, hash_algorithm='md5') def mock_open(filename, mode): return _FakeFileFailsToWrite() if sys.version_info > (3, 0): monkeypatch.setattr('builtins.open', mock_open) else: monkeypatch.setattr('__builtin__.open', mock_open) response = IOLoop.current().run_sync( lambda: download.run(IOLoop.current())) assert ["Failed to write to %s: FAIL" % (filename + ".part") ] == download.errors assert response.code == 200 assert not os.path.isfile(filename) assert not os.path.isfile(filename + ".part")