Esempio n. 1
0
def projectConfig(projectDir):
    """
    Return the configuration object representing the project config.

    Parameters
    ----------

    projectDir :str
       Project directory path as string.

    Returns
    -------

    cp : NepidemiXConfigParser
       A configuration object representing the project directory configuration.

    """
    # Check so that the directory exists
    if not os.path.isdir(projectDir):
        raise IOError("Project directory '{0}' does not exist.".format(projectDir))
    # Read the Cluster simulation
    cp = NepidemiXConfigParser()
    with open(projectDir +'/' + ClusterSimulation.original_config_file_name, 'r') as fp:
        cp.readfp(fp)
    return cp
Esempio n. 2
0
    def createSimulationConfigs(self):
        """
        Tailor build individual configurations for each simulation case.
        """
        # Go through all settings options. Those not belonging to this
        # class is assumed to belong to the program we want to deploy.
        logger.info("Building parameter lists...")
        paramRangeList, varOptList \
            = buildParamRangeList(self.settings,
                                  excludeSections = self.exclude_sections,
                                  excludeOptions = self.exclude_options,
                                  ignoreSections = self.ignore_sections,
                                  ignoreOptions = self.ignore_options)

        logger.info("Creating project directory at '{0}'".format(self.projectDirPath))
        os.makedirs(self.projectDirPath)
        ncalls = 0
        deployScriptName =  self.projectDirPath +'/' + self.deployScriptName
        logger.info("Creating deploy script '{0}'".format(deployScriptName))
        deployScriptFp = open(deployScriptName, 'w')
        deployScriptFp.write("""
#!/bin/bash
# This script is automatically generated.
# Running it will submit all generated PBS jobs to the queue.
""")
        for ddict, cpath in paramsPaths(self.projectDirPath, 
                                        paramRangeList, self.confDirName,
                                        excludeSections = self.exclude_sections, 
                                        excludeOptions = self.exclude_options,
                                        ignoreSections = self.ignore_sections,
                                        ignoreOptions = self.ignore_options):
            cp = NepidemiXConfigParser()
            os.makedirs(cpath, 0750)
            
            
            fname = cpath + "/{0}_{1}".format(self.fileBaseName, ncalls)
            # Write ini file.
            with open(fname+'.ini', 'w') as fp:
                for (sec, opt), val in ddict.items():
                    if not cp.has_section(sec):
                        cp.add_section(sec)
                    cp.set(sec,opt,val)
                cp.write(fp)
        
            queue_name = self.settings.get(self.CFG_SECTION_PBS,
                                           self.CFG_PARAM_queue,
                                           default = '')
            if len(queue_name) < 1:
                queue_string = ""
            else:
                queue_string ="#PBS -q {queue_name}".format(queue_name=queue_name)

            # Write PBS file.
            with open(fname+'.pbs', 'w') as fp:
                # Header, email and output.
                fp.write("""
#!/bin/bash
#PBS -N {job_name}
{queue_string}
#PBS -M {user_email}
#PBS -m bae
#PBS -j oe
#PBS -o {path_to_log}
#PBS -e {path_to_elog}
""".format(job_name = self.projectName + "_{0}".format(ncalls),
           queue_string = queue_string,
           user_email = self.settings.get(self.CFG_SECTION_PBS,
                                          self.CFG_PARAM_email),
           path_to_log = fname+'.log',
           path_to_elog = fname+'_error.log'))

                # Write -l options.
                
                if self.settings.has_option(self.CFG_SECTION_PBS,
                                            self.CFG_PARAM_pbslopts) == True:
                    for los in self.settings.\
                            parseTuple(self.settings.get(self.CFG_SECTION_PBS,
                                                         self.CFG_PARAM_pbslopts)):
                        fp.write("#PBS -l {0}\n".format(los))


                # Write the execution commands.
                fp.write("""
cd {work_dir}
{command}
""".format(work_dir = cpath,
           command = self.simprogram+" {0}.ini {1}".format(fname, self.reps)))
            
            # Add a command to submit the pbs file to our deployment script.
            deployScriptFp.write("qsub {0}.pbs; sleep 0.2\n".format(fname))
            ncalls = ncalls + 1

        deployScriptFp.close()
        # Make deploy script executable for user.
        os.chmod(deployScriptName, 0744)



        self.settings.set(self.CFG_SECTION_INFO, self.CFG_PARAM_num_configs, 
                          ncalls)
        self.settings.set(self.CFG_SECTION_INFO, self.CFG_PARAM_config_base_name, 
                          self.fileBaseName)
        self.settings.set(self.CFG_SECTION_INFO, self.CFG_PARAM_config_dir_name, 
                          self.confDirName)


        # Write the (extended) settings backt to file so that the project
        # is self contained in some way.
        with open(self.projectDirPath + '/{0}'.format(self.original_config_file_name), 'w') as ofp:
            self.settings.write(ofp)
        logger.info("Done, created {0} individual configurations to be repeated {1} times."\
                        .format(ncalls, self.reps))