Esempio n. 1
0
    def run(self):
        "Represents threads acitivity: working through the experiment script and writing job-files"

        while 1:
            # Idling...
            self.__busy = False
            self.event_lock.wait()
            self.__busy = True

            # Telling other threads we are still parsing
            self.__error_occured = None
            self.__stop_experiment = False

            # Quit thread?
            if self.quit_main_loop: break

            self.__jobs_written = None

            # Import needed libraries
            from Experiment import Experiment
            reset()

            # Remove leading/trailing whitespaces and substituting print with self.gui.new_log_message(text, "ES")
            experiment_script_string = self.gui.get_experiment_script().strip()

            # Running Preprocessor

            # Syntax ok? If not, tell other threads an error occured
            if self.check_syntax(experiment_script_string):
                self.__error_occured = False
            else:
                self.__error_occured = True


            # Waiting for GUI & other Threads
            while self.__ok_to_start is None:
                if self.quit_main_loop: return
                if self.__stop_experiment: break
                self.event.wait(0.2)

            if self.__stop_experiment: break

            # Error occured -> __ok_to_start will be set False (from the GUI)
            if not self.__ok_to_start:
                self.__ok_to_start = None
                self.event_lock.clear()
                continue

            # Bind script to self
            try:
                exec experiment_script_string in locals()
                self.experiment_script = experiment_script

            except Exception, e:
                tb_infos=traceback.extract_tb(sys.exc_info()[2])
                self.gui.show_error_dialog("Experiment Script Execution Error", "Experiment Script:\nerror during execution in line %d (function %s):\n"%tb_infos[-1][1:3]+str(e))
                self.gui.new_log_message("Execution Error: Error during execution in line %d (function %s):\n"%tb_infos[-1][1:3]+str(e), "ES")
                self.event_lock.clear()
                self.__ok_to_start = None
                continue

            # Run script
            try:
                for exp in self.experiment_script(self):

                    job_file = file(os.path.join(self.path, "job.tmp"), "w")
                    job_file.write(exp.write_xml_string())
                    job_file.close()
                    os.rename(os.path.join(self.path, "job.tmp"), os.path.join(self.path, self.filename % exp.get_job_id()))

                    self.__job_id_written_last = exp.get_job_id()

                    # Experiment started with sync-flag?
                    if self.__write_jobs_synchronously:
                        while not bool(self.__write_another_job):
                            self.event.wait(0.1)
                            if self.__stop_experiment: break

                        self.__write_another_job -= 1
                        

                    # Stop Experiment?
                    if self.__stop_experiment: break

                end_job = Experiment()

                end_job_file = file(os.path.join(self.path, "job.tmp"), "w")
                end_job_file.write(end_job.write_quit_job())
                end_job_file.close()
                os.rename(os.path.join(self.path, "job.tmp"), os.path.join(self.path, self.filename % end_job.get_job_id()))

            except Exception, e:
                tb_infos=traceback.extract_tb(sys.exc_info()[2])
                self.gui.show_error_dialog("Execution Error In Experiment Script", "Experiment Script:\nerror during execution in line %d (function %s):\n"%tb_infos[-1][1:3]+str(e))
                self.gui.new_log_message("Execution Error: Error during execution in line %d (function %s):\n"%tb_infos[-1][1:3]+str(e), "ES")
                self.gui.stop_experiment(None)