def test_abort(self): # Given r_content = b"some data" path = os.path.join(self.tempdir, "some_data.bin") # When with atomic_file(path) as fp: fp.write(r_content[:2]) temp_name = fp._name fp.abort() # Then self.assertFalse(os.path.exists(path)) self.assertFalse(os.path.exists(temp_name))
def test_simple(self): # Given r_content = b"some data" path = os.path.join(self.tempdir, "some_data.bin") # When with atomic_file(path) as fp: fp.write(r_content) # Then self.assertTrue(os.path.exists(path)) with open(path, "rb") as fp: content = fp.read() self.assertEqual(content, r_content)
def test_failure(self): # Given r_content = b"some data" path = os.path.join(self.tempdir, "some_data.bin") # When try: with atomic_file(path) as fp: fp.write(r_content[:2]) raise KeyboardInterrupt() except BaseException: pass # Then self.assertFalse(os.path.exists(path))
def download(self, url, target=None): """ Safely download the content at the give url. Safely here is understood as not leaving stalled content if the download fails or is canceled. It uses streaming as so not to use too much memory. """ resp = self.fetch(url) if target is None: target = os.path.basename(urlparse(url).path) with atomic_file(target) as fp: for chunk in resp.iter_content(1024): fp.write(chunk) return target
def download(self, url, target=None): """ Safely download the content at the give url. Safely here is understood as not leaving stalled content if the download fails or is canceled. It uses streaming as so not to use too much memory. """ resp = self._raw.get(url, stream=True) resp.raise_for_status() if target is None: target = os.path.basename(urllib.parse.urlparse(url).path) with atomic_file(target) as fp: for chunk in resp.iter_content(1024): fp.write(chunk) return target
def checked_content(filename, expected_md5): """ A simple context manager ensure data written to filename match the given md5. Parameters ---------- filename : str The path to write to expected_checksum : str The expected checksum Returns ------- fp : MD5File A file-like MD5File instance. Example ------- A simple example:: with checked_content("foo.bin", expected_md5) as fp: fp.write(data) # An InvalidChecksum will be raised if the checksum does not match # expected_md5 The checksum may be disabled by setting up abort to fp:: with checked_content("foo.bin", expected_md5) as fp: fp.write(data) fp.abort = True # no checksum is getting validated """ with atomic_file(filename) as target: checked_target = MD5File(target) yield checked_target if checked_target.is_aborted: target.abort() return else: if expected_md5 != checked_target.checksum: raise InvalidChecksum(filename, expected_md5, checked_target.checksum)
def test_exists_with_failure(self): # Given r_content = b"nono" path = os.path.join(self.tempdir, "some_data.bin") with open(path, "wb") as fp: fp.write(r_content) # When try: with atomic_file(path) as fp: fp.write(b"some data") raise ValueError("some random failure") except BaseException: pass # Then self.assertTrue(os.path.exists(path)) with open(path, "rb") as fp: content = fp.read() self.assertEqual(content, r_content)