def safe_get_file(self, digest, path, descr_path=None): """Get file from FileCacher ensuring that the digest is correct. digest (string): the digest of the file to retrieve. path (string): the path where to save the file. descr_path (string): the path where to save the description. return (bool): True if all ok, False if something wrong. """ # TODO - Probably this method could be merged in FileCacher # First get the file try: self.file_cacher.get_file_to_path(digest, path) except Exception: logger.error("File %s could not retrieved from file server.", digest, exc_info=True) return False # Then check the digest calc_digest = path_digest(path) if digest != calc_digest: logger.critical("File %s has wrong hash %s.", digest, calc_digest) return False # If applicable, retrieve also the description if descr_path is not None: with open(descr_path, 'wt', encoding='utf-8') as fout: fout.write(self.file_cacher.describe(digest)) return True
def safe_put_file(self, path, descr_path): """Put a file to FileCacher signaling every error (including digest mismatch). path (string): the path from which to load the file. descr_path (string): same for description. return (bool): True if all ok, False if something wrong. """ # TODO - Probably this method could be merged in FileCacher # First read the description. try: with io.open(descr_path, 'rt', encoding='utf-8') as fin: description = fin.read() except IOError: description = '' # Put the file. try: digest = self.file_cacher.put_file_from_path(path, description) except Exception as error: logger.critical( "File %s could not be put to file server (%r), " "aborting.", path, error) return False # Then check the digest. calc_digest = path_digest(path) if digest != calc_digest: logger.critical( "File %s has hash %s, but the server returned %s, " "aborting.", path, calc_digest, digest) return False return True
def safe_put_file(self, path, descr_path): """Put a file to FileCacher signaling every error (including digest mismatch). path (string): the path from which to load the file. descr_path (string): same for description. return (bool): True if all ok, False if something wrong. """ # TODO - Probably this method could be merged in FileCacher # First read the description. try: with io.open(descr_path, 'rt', encoding='utf-8') as fin: description = fin.read() except IOError: description = '' # Put the file. try: digest = self.file_cacher.put_file_from_path(path, description) except Exception as error: logger.critical("File %s could not be put to file server (%r), " "aborting.", path, error) return False # Then check the digest. calc_digest = path_digest(path) if digest != calc_digest: logger.critical("File %s has hash %s, but the server returned %s, " "aborting.", path, calc_digest, digest) return False return True
def safe_get_file(self, digest, path, descr_path=None): """Get file from FileCacher ensuring that the digest is correct. digest (string): the digest of the file to retrieve. path (string): the path where to save the file. descr_path (string): the path where to save the description. return (bool): True if all ok, False if something wrong. """ # TODO - Probably this method could be merged in FileCacher # First get the file try: self.file_cacher.get_file_to_path(digest, path) except Exception: logger.error("File %s could not retrieved from file server.", digest, exc_info=True) return False # Then check the digest calc_digest = path_digest(path) if digest != calc_digest: logger.critical("File %s has wrong hash %s.", digest, calc_digest) return False # If applicable, retrieve also the description if descr_path is not None: with io.open(descr_path, 'wt', encoding='utf-8') as fout: fout.write(self.file_cacher.describe(digest)) return True
def test_long(self): content = b"0" * 1000000 self.write_file(content) self.assertEqual(path_digest(self.path), bytes_digest(content))
def test_empty(self): self.write_file(b"") self.assertEqual(path_digest(self.path), _EMPTY_DIGEST)
def test_success(self): self.write_file(b"content") self.assertEqual(path_digest(self.path), _CONTENT_DIGEST)
def test_not_found(self): with self.assertRaises(FileNotFoundError): path_digest(self.path)
def test_not_found(self): with self.assertRaises(FileNotFoundError): path_digest(self.path)
def test_long(self): content = b"0" * 1_000_000 self.write_file(self.filename, content) self.assertEqual(path_digest(self.path), bytes_digest(content))
def test_empty(self): self.write_file(self.filename, b"") self.assertEqual(path_digest(self.path), _EMPTY_DIGEST)
def test_success(self): self.write_file(self.filename, b"content") self.assertEqual(path_digest(self.path), _CONTENT_DIGEST)