Changeset ff2a11c in flex_extract.git


Ignore:
Timestamp:
Nov 29, 2018, 9:05:05 PM (5 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
38e83ba
Parents:
b2e95c3
Message:

changed generation of final job_scripts by using genshi templates

Location:
source/python
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • source/python/classes/ControlFile.py

    r6cda7c1 rff2a11c  
    541541                        stot += s + ' '
    542542
    543                     l.append("%s %s" % (item[0], stot))
     543                    l.append("%s %s\n" % (item[0], stot))
    544544                else:
    545                     l.append("%s %s" % item)
     545                    l.append("%s %s\n" % item)
    546546
    547547        return sorted(l)
  • source/python/install.py

    r6cda7c1 rff2a11c  
    567567                                          _config.FLEXEXTRACT_DIRNAME,
    568568                                          _config.PATH_REL_PYTHON_SRC)
     569    if '$' in fp_root_path_to_python:
     570        ind = fp_root_path_to_python.index('$')
     571        fp_root_path_to_python = fp_root_path_to_python[0:ind] + '$' + \
     572                                 fp_root_path_to_python[ind:]
    569573
    570574    try:
  • source/python/submit.py

    re0e99a5 rff2a11c  
    107107
    108108def submit(jtemplate, c, queue):
    109     '''Prepares the job script and submit it to the specified queue.
     109    '''Prepares the job script and submits it to the specified queue.
    110110
    111111    Parameters
     
    130130    '''
    131131
    132     # read template file and get index for CONTROL input
    133     with open(os.path.join(_config.PATH_TEMPLATES, jtemplate)) as f:
    134         lftext = f.read().split('\n')
    135     insert_point = lftext.index('EOF')
    136 
    137132    if not c.basetime:
    138133    # --------- create on demand job script ------------------------------------
     
    141136        else:
    142137            print('---- On-demand mode! ----')
     138
    143139        job_file = os.path.join(_config.PATH_JOBSCRIPTS,
    144                                 jtemplate[:-4] + 'ksh')
     140                                jtemplate[:-5] + '.ksh')
     141
    145142        clist = c.to_list()
    146143
    147         lftextondemand = lftext[:insert_point] + clist + lftext[insert_point:]
    148 
    149         with open(job_file, 'w') as f:
    150             f.write('\n'.join(lftextondemand))
    151 
    152         job_id = submit_job_to_ecserver(queue, job_file)
     144        mk_jobscript(jtemplate, job_file, clist)
    153145
    154146    else:
    155147    # --------- create operational job script ----------------------------------
    156148        print('---- Operational mode! ----')
     149
    157150        job_file = os.path.join(_config.PATH_JOBSCRIPTS,
    158151                                jtemplate[:-5] + 'oper.ksh')
    159 
    160         if c.maxstep:
    161             mt = int(c.maxstep)
    162         else:
    163             mt = 0
    164152
    165153        c.start_date = '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}'
    166154        c.end_date = '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}'
    167155        c.base_time = '${MSJ_BASETIME}'
    168         if mt > 24:
     156        if c.maxstep > 24:
    169157            c.time = '${MSJ_BASETIME} {MSJ_BASETIME}'
    170158
    171         colist = c.to_list()
    172 
    173         lftextoper = lftext[:insert_point] + colist + lftext[insert_point + 2:]
    174 
    175         with open(job_file, 'w') as f:
    176             f.write('\n'.join(lftextoper))
    177 
    178         job_id = submit_job_to_ecserver(queue, job_file)
    179 
    180     # --------------------------------------------------------------------------
     159        clist = c.to_list()
     160
     161        mk_jobscript(jtemplate, job_file, clist)
     162
     163    # --------- submit the job_script to the ECMWF server
     164    job_id = submit_job_to_ecserver(queue, job_file)
    181165    print('The job id is: ' + str(job_id.strip()))
    182166    print('You should get an email with subject flex.hostname.pid')
     
    184168    return
    185169
     170def mk_jobscript(jtemplate, job_file, clist):
     171    '''Creates the job script from template.
     172
     173    Parameters
     174    ----------
     175    jtemplate : :obj:`string`
     176        Job template file from sub-directory "_templates" for
     177        submission to ECMWF. It contains all necessary
     178        module and variable settings for the ECMWF environment as well as
     179        the job call and mail report instructions.
     180        Default is "job.temp".
     181
     182    job_file : :obj:`string`
     183        Path to the job script file.
     184
     185    clist : :obj:`list` of :obj:`string`
     186        Contains all necessary parameters for ECMWF CONTROL file.
     187
     188    Return
     189    ------
     190
     191    '''
     192    from genshi.template.text import NewTextTemplate
     193    from genshi.template import  TemplateLoader
     194    from genshi.template.eval import UndefinedError
     195
     196    # load template and insert control content as list
     197    try:
     198        loader = TemplateLoader(_config.PATH_TEMPLATES, auto_reload=False)
     199        control_template = loader.load(jtemplate,
     200                                       cls=NewTextTemplate)
     201
     202        stream = control_template.generate(control_content=clist)
     203    except UndefinedError as e:
     204        print('... ERROR ' + str(e))
     205
     206        sys.exit('\n... error occured while trying to generate jobscript')
     207    except OSError as e:
     208        print('... ERROR CODE: ' + str(e.errno))
     209        print('... ERROR MESSAGE:\n \t ' + str(e.strerror))
     210
     211        sys.exit('\n... error occured while trying to generate jobscript')
     212
     213    # create jobscript file
     214    try:
     215        with open(job_file, 'w') as f:
     216            f.write(stream.render('text'))
     217    except OSError as e:
     218        print('... ERROR CODE: ' + str(e.errno))
     219        print('... ERROR MESSAGE:\n \t ' + str(e.strerror))
     220
     221        sys.exit('\n... error occured while trying to write ' + job_file)
     222
     223    return
     224
    186225if __name__ == "__main__":
    187226    main()
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG