def test_atomic_context_error(self): outf = self._get_test_out_file(".tar") try: with AtomicFileCreate(outf) as outf_tmp: self._write_test_file(outf_tmp) raise Exception("stop!") except Exception as ex: self.assertEqual(str(ex), "stop!") self.assertFalse(os.path.exists(outf))
def read_file(self, file_id, local_path, symlink=False): # used on non-shared files which will be encrypted if available # checking for JobStoreID existence if not self.file_exists(file_id): raise NoSuchFileException(file_id) with AtomicFileCreate(local_path) as tmpPath: with open(tmpPath, 'wb') as writeable: blob = self.bucket.get_blob(compat_bytes(file_id), encryption_key=self.sseKey) blob.download_to_file(writeable) if getattr(file_id, 'executable', False): os.chmod(local_path, os.stat(local_path).st_mode | stat.S_IXUSR)
def writeSharedFileStream(self, sharedFileName, isProtected=None, encoding=None, errors=None): # the isProtected parameter has no effect on the fileStore self._requireValidSharedFileName(sharedFileName) with AtomicFileCreate( self._getSharedFilePath(sharedFileName)) as tmpSharedFilePath: with open(tmpSharedFilePath, 'wb' if encoding == None else 'wt', encoding=encoding, errors=None) as f: yield f
def test_atomic_context_ok(self): outf = self._get_test_out_file(".tar") with AtomicFileCreate(outf) as outf_tmp: self._write_test_file(outf_tmp) self.assertTrue(os.path.exists(outf))