Exemple #1
0
 def write_run_config(self, run_resources):
     statusdir = self.current_cache_directory
     
     xml_full_file_name = run_resources.get('xml_file')
     if xml_full_file_name:
         xml_config = XMLConfiguration(xml_full_file_name)
         xml_file_name = os.path.basename(xml_full_file_name)
         file_name, ext = os.path.splitext(xml_file_name)
         xml_file_name_flattened = os.path.join(statusdir, file_name + "_flattened" + ext)
         xml_config.full_tree.write(xml_file_name_flattened, pretty_print=True)
     
     # # it may be better to save it with the run in services/run_activity database table
     version_numbers = get_opus_version_number()
     version_file = os.path.join(statusdir, "opus_version_number.txt")
     with open(version_file, 'w') as f:
         f.write(version_numbers)
Exemple #2
0
    def run(self):
        # Run the Eugene model using the XML version of the Eugene configuration.
        # This code adapted from opus_core/tools/start_run.py
        # statusdir is a temporary directory into which to write a status file
        # regarding the progress of the simulation - the progress bar reads this file
        statusdir = None
        succeeded = False
        run_id = None

        try:
            config = self.xml_config.get_run_configuration(str(self.scenariotorun))
            # self.xmltreeobject.toolboxbase.opus_core_xml_configuration.get_run_configuration(str(self.scenariotorun))

            cache_directory_root = config['creating_baseyear_cache_configuration'].cache_directory_root
            run_name = self.get_run_name(config = config, run_name = self.run_name)
            config['cache_directory'] = os.path.join(cache_directory_root, run_name)

            #insert_auto_generated_cache_directory_if_needed(config)
            (self.start_year, self.end_year) = config['years']

            run_manager = get_run_manager()

            run_manager.setup_new_run(cache_directory = config['cache_directory'],
                                      configuration = config)
            
            statusdir = run_manager.get_current_cache_directory()
            self.statusfile = os.path.join(statusdir, 'status.txt')
            self.currentLogfileYear = self.start_year
            self.currentLogfileKey = 0
            self.config = config
            config['status_file_for_gui'] = self.statusfile
            self.commandfile = os.path.join(statusdir, 'command.txt')
            config['command_file_for_gui'] = self.commandfile

            # To test delay in writing the first log file entry...
            # time.sleep(5)
            self.running = True
            self.run_name = run_name
            self.run_manager = run_manager
            run_id = run_manager.run_id
            self.startedCallback(run_id = run_id, 
                                 run_name = run_name, 
                                 scenario_name = self.scenariotorun, 
                                 run_resources = config,
                                 status = 'running')
            run_manager.run_run(config, run_name=run_name, 
                                scenario_name=self.scenariotorun)
            self.running = False
            succeeded = True
            
            ## make a copy of the full tree to run directory
            
            xml_file_name = os.path.basename(self.xml_config.full_filename)
            file_name, ext = os.path.splitext(xml_file_name)
            xml_file = os.path.join(statusdir, file_name + "_flattened" + ext)
            self.xml_config.full_tree.write(xml_file, pretty_print = True)
            
            ## it may be better to save it with the run in services/run_activity database table
            from opus_core.version_numbers import get_opus_version_number
            version_numbers = get_opus_version_number()
            version_file = os.path.join(statusdir, "opus_version_number.txt")
            with open(version_file, 'w') as f:
                f.write(version_numbers)
            
        except SimulationRunError:
            self.running = False
            succeeded = False
        except:
            self.running = False
            succeeded = False
            errorInfo = formatExceptionInfo(custom_message = 'Unexpected Error From Model')
            self.errorCallback(errorInfo)
        if self.statusfile is not None:
            gc.collect()
            # adding try/except, windows sometimes has a lock on this file
            # when it does, os.remove will cause the simulation to appear
            # as if it has crashed, when in fact it simply could not delete
            # status.txt
            try:
                os.remove(self.statusfile)
            except:
                pass
        self.finishedCallback(succeeded, run_name = run_name)