def test_get_unzip_strip_root(self): """Test that the strip_root mechanism from the underlying unzip is called if I call the tools.get by checking that the exception of an invalid zip to flat is raised""" zip_folder = temp_folder() def mock_download(*args, **kwargs): tmp_folder = temp_folder() with chdir(tmp_folder): ori_files_dir = os.path.join(tmp_folder, "subfolder-1.2.3") file1 = os.path.join(ori_files_dir, "file1") file2 = os.path.join(ori_files_dir, "folder", "file2") # !!! This file is not under the root "subfolder-1.2.3" file3 = os.path.join("file3") save(file1, "") save(file2, "") save(file3, "") zip_file = os.path.join(zip_folder, "file.zip") zipdir(tmp_folder, zip_file) with six.assertRaisesRegex( self, ConanException, "The zip file contains more than 1 " "folder in the root"): with patch('conans.client.tools.net.download', new=mock_download): with chdir(zip_folder): tools.get("file.zip", strip_root=True)
def source(self): download_url = "https://dl.bintray.com/boostorg/release/{}/source/{}".format( self.version, self.boost_zip_name ) self.output.warn("Downloading '{}'...".format(download_url)) tools.get( download_url, filename=self.boost_zip_name, sha256="7f6130bc3cf65f56a618888ce9d5ea704fa10b462be126ad053e80e553d6d8b7", )
def source(self): download_url = ( f"https://github.com/gabime/spdlog/archive/{self.spdlog_tar_gz_name}" ) self.output.info(f"Downloading {download_url} ...") tools.get( download_url, filename=self.spdlog_tar_gz_name, sha256="867a4b7cedf9805e6f76d3ca41889679054f7e5a3b67722fe6d0eae41852a767", )
def get_gunzip_test(self): # Create a tar file to be downloaded from server tmp = temp_folder() filepath = os.path.join(tmp, "test.txt.gz") import gzip with gzip.open(filepath, "wb") as f: f.write(b"hello world zipped!") thread = StoppableThreadBottle() @thread.server.get("/test.txt.gz") def get_file(): return static_file(os.path.basename(filepath), root=os.path.dirname(filepath), mimetype="application/octet-stream") thread.run_server() out = TestBufferConanOutput() with tools.chdir(tools.mkdir_tmp()): tools.get("http://localhost:%s/test.txt.gz" % thread.port, requester=requests, output=out) self.assertTrue(os.path.exists("test.txt")) self.assertEqual(load("test.txt"), "hello world zipped!") with tools.chdir(tools.mkdir_tmp()): tools.get("http://localhost:%s/test.txt.gz" % thread.port, requester=requests, output=out, destination="myfile.doc") self.assertTrue(os.path.exists("myfile.doc")) self.assertEqual(load("myfile.doc"), "hello world zipped!") with tools.chdir(tools.mkdir_tmp()): tools.get("http://localhost:%s/test.txt.gz" % thread.port, requester=requests, output=out, destination="mytemp/myfile.txt") self.assertTrue(os.path.exists("mytemp/myfile.txt")) self.assertEqual(load("mytemp/myfile.txt"), "hello world zipped!") thread.stop()
def get_filename_download_test(self): # Create a tar file to be downloaded from server with tools.chdir(tools.mkdir_tmp()): import tarfile tar_file = tarfile.open("sample.tar.gz", "w:gz") mkdir("test_folder") tar_file.add(os.path.abspath("test_folder"), "test_folder") tar_file.close() file_path = os.path.abspath("sample.tar.gz") assert(os.path.exists(file_path)) # Instance stoppable thread server and add endpoints thread = StoppableThreadBottle() @thread.server.get("/this_is_not_the_file_name") def get_file(): return static_file(os.path.basename(file_path), root=os.path.dirname(file_path)) @thread.server.get("/") def get_file2(): self.assertEqual(request.query["file"], "1") return static_file(os.path.basename(file_path), root=os.path.dirname(file_path)) @thread.server.get("/error_url") def error_url(): from bottle import response response.status = 500 return 'This always fail' thread.run_server() out = TestBufferConanOutput() # Test: File name cannot be deduced from '?file=1' with six.assertRaisesRegex(self, ConanException, "Cannot deduce file name form url. Use 'filename' parameter."): tools.get("http://localhost:%s/?file=1" % thread.port, output=out) # Test: Works with filename parameter instead of '?file=1' with tools.chdir(tools.mkdir_tmp()): tools.get("http://localhost:%s/?file=1" % thread.port, filename="sample.tar.gz", requester=requests, output=out) self.assertTrue(os.path.exists("test_folder")) # Test: Use a different endpoint but still not the filename one with tools.chdir(tools.mkdir_tmp()): from zipfile import BadZipfile with self.assertRaises(BadZipfile): tools.get("http://localhost:%s/this_is_not_the_file_name" % thread.port, requester=requests, output=out) tools.get("http://localhost:%s/this_is_not_the_file_name" % thread.port, filename="sample.tar.gz", requester=requests, output=out) self.assertTrue(os.path.exists("test_folder")) thread.stop() with six.assertRaisesRegex(self, ConanException, "Error"): tools.get("http://localhost:%s/error_url" % thread.port, filename="fake_sample.tar.gz", requester=requests, output=out, verify=False, retry=2, retry_wait=0) # Not found error self.assertEqual(str(out).count("Waiting 0 seconds to retry..."), 2)
def test_get_mirror(self, _): """ tools.get must supports a list of URLs. However, only one must be downloaded. """ class MockRequester(object): def __init__(self): self.count = 0 self.fail_first = False self.fail_all = False def get(self, *args, **kwargs): self.count += 1 resp = Response() resp._content = b'{"results": []}' resp.headers = {"Content-Type": "application/json"} resp.status_code = 200 if (self.fail_first and self.count == 1) or self.fail_all: resp.status_code = 408 return resp file = "test.txt.gz" out = TestBufferConanOutput() urls = [ "http://localhost:{}/{}".format(8000 + i, file) for i in range(3) ] # Only the first file must be downloaded with tools.chdir(tools.mkdir_tmp()): requester = MockRequester() tools.get(urls, requester=requester, output=out, retry=0, retry_wait=0) self.assertEqual(1, requester.count) # Fail the first, download only the second with tools.chdir(tools.mkdir_tmp()): requester = MockRequester() requester.fail_first = True tools.get(urls, requester=requester, output=out, retry=0, retry_wait=0) self.assertEqual(2, requester.count) self.assertIn( "WARN: Could not download from the URL {}: Error 408 downloading file {}." " Trying another mirror.".format(urls[0], urls[0]), out) # Fail all downloads with tools.chdir(tools.mkdir_tmp()): requester = MockRequester() requester.fail_all = True with self.assertRaises(ConanException) as error: tools.get(urls, requester=requester, output=out, retry=0, retry_wait=0) self.assertEqual(3, requester.count) self.assertIn("All downloads from (3) URLs have failed.", str(error.exception))
def source(self): self.output.info(f"Downloading GCC sources from {self.source_url}") tools.get(self.source_url) self.output.info("Downloading prerequisites") self.run(f"cd {self.gcc_folder} && ./contrib/download_prerequisites")