def test_download_nonexistent(self): with tempfile.TemporaryDirectory() as tmpdir: tmpdir = pathlib.Path(tmpdir) input_filename = tmpdir / "nonexistant" output_filename = tmpdir / "output.foo" for url in ( input_filename.resolve().as_uri(), # local file "http://example.com/nonexistant", # remote file ): with self.assertRaises(OSError): utils.download(url, output_filename) self.assertFalse(output_filename.exists())
def test_download_local_file(self): with tempfile.TemporaryDirectory() as tmpdir: tmpdir = pathlib.Path(tmpdir) input_filename = tmpdir / "input.foo" output_filename = tmpdir / "output.foo" with open(input_filename, "w") as f: print("foofoofoo", file=f) url = input_filename.resolve().as_uri() utils.download(url, output_filename) self.assertTrue(output_filename.exists()) with open(output_filename) as f: contents = f.read() self.assertEqual(contents.strip(), "foofoofoo")
def setUpModule(): destination = pathlib.Path("_test_cache/zipfiles/") for an in stdpopsim.all_annotations(): key = an._cache.cache_path local_file = destination / key if not local_file.exists(): cache_dir = local_file.parent cache_dir.mkdir(exist_ok=True, parents=True) print("Downloading", an.zarr_url) utils.download(an.zarr_url, local_file) # This assertion could fail if we update a file on AWS, # or a developer creates a new annotation with the wrong checksum # (in the latter case, this should at least be caught during CI tests). assert utils.sha256(local_file) == an.zarr_sha256, ( f"SHA256 for {local_file} doesn't match the SHA256 for " f"{an.id}. If you didn't add this SHA256 yourself, " f"try deleting {local_file} and restarting the tests.") saved_urls[key] = an.zarr_url an.zarr_url = local_file.resolve().as_uri() an._cache.url = an.zarr_url
def setUpModule(): destination = pathlib.Path("_test_cache/tarballs") for genetic_map in stdpopsim.all_genetic_maps(): key = genetic_map.id local_file = destination / (key + ".tar.gz") if not local_file.exists(): cache_dir = local_file.parent cache_dir.mkdir(exist_ok=True, parents=True) print("Downloading", genetic_map.url) utils.download(genetic_map.url, local_file) # This assertion could fail if we update a file on AWS, # or a developer creates a new genetic map with the wrong checksum # (in the latter case, this should at least be caught during CI tests). assert utils.sha256(local_file) == genetic_map.sha256, ( f"SHA256 for {local_file} doesn't match the SHA256 for " f"{genetic_map.id}. If you didn't add this SHA256 yourself, " f"try deleting {local_file} and restarting the tests.") saved_urls[key] = genetic_map.url genetic_map.url = local_file.resolve().as_uri() genetic_map._cache.url = genetic_map.url