def write(self, file_data, can_overwrite=False): try: mode = "{write_mode}{binary_mode}".format( write_mode="w" if can_overwrite else "x", binary_mode="b" if self.metadata.is_binary else "", ) # It seems pylint cannot process constructing the mode variable and # gives a false positive. # pylint: disable=bad-open-mode with open(self.metadata.path, mode) as my_file: # the lock is released when the file gets closed on leaving the # with statement fcntl.flock(my_file.fileno(), fcntl.LOCK_EX) # Set the ownership and permissions to cover the case when we # just created the file. If the file already existed, make sure # the ownership and permissions are correct before writing any # data into it. if ( self.metadata.owner_user_name is not None or self.metadata.owner_group_name is not None ): try: shutil.chown( self.metadata.path, self.metadata.owner_user_name, self.metadata.owner_group_name, ) except LookupError as e: raise RawFileError( self.metadata, RawFileError.ACTION_CHOWN, str(e) ) except OSError as e: raise RawFileError( self.metadata, RawFileError.ACTION_CHOWN, format_os_error(e), ) if self.metadata.permissions is not None: try: os.chmod(my_file.fileno(), self.metadata.permissions) except OSError as e: raise RawFileError( self.metadata, RawFileError.ACTION_CHMOD, format_os_error(e), ) # Write file data my_file.write( file_data if self.metadata.is_binary else file_data.decode("utf-8") ) except FileExistsError as e: raise FileAlreadyExists(self.metadata) except OSError as e: raise RawFileError( self.metadata, RawFileError.ACTION_WRITE, format_os_error(e) )
def simulate_cib_xml(runner, cib_xml): """ Run crm_simulate to get effects the cib would have on the live cluster CommandRunner runner -- runner string cib_xml -- CIB XML to simulate """ try: with tools.get_tmp_file() as new_cib_file, tools.get_tmp_file( ) as transitions_file: cmd = [ __exec("crm_simulate"), "--simulate", "--save-output", new_cib_file.name, "--save-graph", transitions_file.name, "--xml-pipe", ] stdout, stderr, retval = runner.run(cmd, stdin_string=cib_xml) if retval != 0: raise LibraryError( ReportItem.error( reports.messages.CibSimulateError(stderr.strip()))) new_cib_file.seek(0) transitions_file.seek(0) transitions = transitions_file.read() new_cib = new_cib_file.read() return stdout, transitions, new_cib except OSError as e: raise LibraryError( ReportItem.error( reports.messages.CibSimulateError(format_os_error(e)))) from e
def remove(self, fail_if_file_not_found=True): get_raw_file_error = lambda e: RawFileError( self.metadata, RawFileError.ACTION_REMOVE, format_os_error(e)) try: os.remove(self.metadata.path) except FileNotFoundError as e: if fail_if_file_not_found: raise get_raw_file_error(e) except OSError as e: raise get_raw_file_error(e)
def simulate_cib_xml(runner, cib_xml): """ Run crm_simulate to get effects the cib would have on the live cluster CommandRunner runner -- runner string cib_xml -- CIB XML to simulate """ try: new_cib_file = write_tmpfile(None) transitions_file = write_tmpfile(None) except OSError as e: raise LibraryError( reports.cib_simulate_error(format_os_error(e)) ) cmd = [ __exec("crm_simulate"), "--simulate", "--save-output", new_cib_file.name, "--save-graph", transitions_file.name, "--xml-pipe", ] stdout, stderr, retval = runner.run(cmd, stdin_string=cib_xml) if retval != 0: raise LibraryError( reports.cib_simulate_error(stderr.strip()) ) try: new_cib_file.seek(0) transitions_file.seek(0) new_cib_xml = new_cib_file.read() transitions_xml = transitions_file.read() new_cib_file.close() transitions_file.close() return stdout, transitions_xml, new_cib_xml except OSError as e: raise LibraryError( reports.cib_simulate_error(format_os_error(e)) )
def read(self) -> bytes: try: mode = "rb" if self.metadata.is_binary else "r" with open(self.metadata.path, mode) as my_file: # the lock is released when the file gets closed on leaving the # with statement fcntl.flock(my_file.fileno(), fcntl.LOCK_SH) content = my_file.read() return (content if self.metadata.is_binary else content.encode("utf-8")) except OSError as e: # Specific expection if the file does not exist is not needed, # anyone can and should check that using the exists method. raise RawFileError(self.metadata, RawFileError.ACTION_READ, format_os_error(e)) from e
def __get_raw_file_error(self, e: OSError) -> RawFileError: return RawFileError(self.metadata, RawFileError.ACTION_REMOVE, format_os_error(e))