def write_two_phases(filename: str, data: str, io: PyStratumStyle) -> None: """ Writes a file in two phase to the filesystem. First write the data to a temporary file (in the same directory) and than renames the temporary file. If the file already exists and its content is equal to the data that must be written no action is taken. This has the following advantages: * In case of some write error (e.g. disk full) the original file is kep in tact and no file with partially data is written. * Renaming a file is atomic. So, running processes will never read a partially written data. :param str filename: The name of the file were the data must be stored. :param str data: The data that must be written. :param PyStratumStyle io: The output decorator. """ write_flag = True if os.path.exists(filename): with open(filename, 'r') as file: old_data = file.read() if data == old_data: write_flag = False if write_flag: tmp_filename = filename + '.tmp' with open(tmp_filename, 'w+') as file: file.write(data) os.replace(tmp_filename, filename) io.text('Wrote: <fso>{0}</fso>'.format(filename)) else: io.text('File <fso>{0}</fso> is up to date'.format(filename))
def handle(self): """ Executes constants command when PyStratumCommand is activated. """ self.output = PyStratumStyle(self.input, self.output) config_file = self.input.get_argument('config_file') self.run_command(config_file)
def handle(self) -> int: """ Executes wrapper command when PyStratumCommand is activated. """ self.output = PyStratumStyle(self.input, self.output) config_file = self.input.get_argument('config_file') return self.run_command(config_file)
def handle(self) -> int: """ Executes loader command. """ self.output = PyStratumStyle(self.input, self.output) config_file = self.argument('config_file') sources = self.argument('file_names') return self.run_command(config_file, sources)
class PyStratumCommand(Command): """ Loads stored routines and generates a wrapper class stratum {config_file : The stratum configuration file} {file_names?* : Sources with stored routines} """ # ------------------------------------------------------------------------------------------------------------------ def execute(self, input_object: Input, output_object: Output) -> int: """ Executes this command. """ self.input = input_object self.output = output_object return self.handle() # ------------------------------------------------------------------------------------------------------------------ def handle(self) -> int: """ Executes the actual Stratum program. """ self.output = PyStratumStyle(self.input, self.output) command = self.get_application().find('constants') ret = command.execute(self.input, self.output) if ret: return ret command = self.get_application().find('loader') ret = command.execute(self.input, self.output) if ret: return ret command = self.get_application().find('wrapper') ret = command.execute(self.input, self.output) self.output.writeln('') return ret
def handle(self): """ Executes the actual Stratum program. """ self.output = PyStratumStyle(self.input, self.output) command = self.get_application().find('constants') ret = command.execute(self.input, self.output) if ret: return ret command = self.get_application().find('loader') ret = command.execute(self.input, self.output) if ret: return ret command = self.get_application().find('wrapper') ret = command.execute(self.input, self.output) self.output.writeln('') return ret
class PyStratumCommand(Command): """ Loads stored routines and generates a wrapper class stratum {config_file : The audit configuration file} {file_names?* : Sources with stored routines} """ # ------------------------------------------------------------------------------------------------------------------ def execute(self, i, o): self.input = i self.output = o return self.handle() # ------------------------------------------------------------------------------------------------------------------ def handle(self): """ Executes the actual Stratum program. """ self.output = PyStratumStyle(self.input, self.output) command = self.get_application().find('constants') ret = command.execute(self.input, self.output) if ret: return ret command = self.get_application().find('loader') ret = command.execute(self.input, self.output) if ret: return ret command = self.get_application().find('wrapper') ret = command.execute(self.input, self.output) self.output.writeln('') return ret