source: flex_extract.git/python/install.py @ 2fb99de

ctbtodev
Last change on this file since 2fb99de was 2fb99de, checked in by Anne Philipp <anne.philipp@…>, 6 years ago

introduced config with path definitions and changed py files accordingly; Installation works; some tests were added for tarball making; Problems in submission to ecgate

  • Property mode set to 100755
File size: 18.5 KB
RevLine 
[d69b677]1#!/usr/bin/env python
[64cf353]2# -*- coding: utf-8 -*-
[991df6a]3#*******************************************************************************
4# @Author: Leopold Haimberger (University of Vienna)
5#
6# @Date: November 2015
7#
8# @Change History:
9#
10#    February 2018 - Anne Philipp (University of Vienna):
11#        - applied PEP8 style guide
12#        - added documentation
[ff99eae]13#        - moved install_args_and_control in here
[2fb99de]14#        - splitted code in smaller functions
15#        - delete convert build files in here instead of compile job script
16#        - changed static path names to Variables from config file
[991df6a]17#
18# @License:
19#    (C) Copyright 2015-2018.
20#
21#    This software is licensed under the terms of the Apache Licence Version 2.0
22#    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
23#
24# @Program Functionality:
25#    Depending on the selected installation environment (locally or on the
26#    ECMWF server ecgate or cca) the program extracts the commandline
27#    arguments and the CONTROL file parameter and prepares the corresponding
28#    environment. The necessary files are collected in a tar-ball and placed
29#    at the target location. There its untared, the environment variables will
30#    be set and the Fortran code will be compiled. If the ECMWF environment is
31#    selected a job script is prepared and submitted for the remaining
32#    configurations after putting the tar-ball to the target ECMWF server.
33#
34# @Program Content:
35#    - main
[54a8a01]36#    - get_install_cmdline_arguments
[991df6a]37#    - install_via_gateway
[54a8a01]38#    - mk_tarball
[2fb99de]39#    - un_tarball
[54a8a01]40#    - mk_env_vars
41#    - mk_compilejob
42#    - mk_job_template
43#    - delete_convert_build
44#    - make_convert_build
[991df6a]45#
46#*******************************************************************************
47
[64cf353]48# ------------------------------------------------------------------------------
49# MODULES
50# ------------------------------------------------------------------------------
[991df6a]51import os
52import sys
[2fb99de]53import glob
[d69b677]54import subprocess
55import inspect
[ff99eae]56from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
[991df6a]57
58# software specific classes and modules from flex_extract
[2fb99de]59import _config
[991df6a]60from ControlFile import ControlFile
[54a8a01]61from UioFiles import UioFiles
62from tools import make_dir, put_file_to_ecserver, submit_job_to_ecserver
[d69b677]63
[64cf353]64# ------------------------------------------------------------------------------
65# FUNCTIONS
66# ------------------------------------------------------------------------------
[991df6a]67def main():
68    '''
69    @Description:
70        Controls the installation process. Calls the installation function
71        if target is specified.
72
73    @Intput:
74        <nothing>
75
76    @Return:
77        <nothing>
78    '''
79
[2fb99de]80    #os.chdir(_config.PATH_LOCAL_PYTHON)
81
[54a8a01]82    args = get_install_cmdline_arguments()
[991df6a]83
[54a8a01]84    try:
85        c = ControlFile(args.controlfile)
86    except IOError:
[2fb99de]87        print('Could not read CONTROL file "' + args.controlfile + '"')
88        print('Either it does not exist or its syntax is wrong.')
89        print('Try "' + sys.argv[0].split('/')[-1] +
90              ' -h" to print usage information')
[54a8a01]91        exit(1)
[991df6a]92
[54a8a01]93    c.assign_args_to_control(args)
94    c.check_install_conditions()
[991df6a]95
[54a8a01]96    install_via_gateway(c)
[991df6a]97
[54a8a01]98    return
[991df6a]99
[54a8a01]100def get_install_cmdline_arguments():
[efdb01a]101    '''
102    @Description:
[54a8a01]103        Decomposes the command line arguments and assigns them to variables.
104        Apply default values for non mentioned arguments.
[efdb01a]105
106    @Input:
107        <nothing>
108
109    @Return:
110        args: instance of ArgumentParser
111            Contains the commandline arguments from script/program call.
112    '''
[54a8a01]113    parser = ArgumentParser(description='Install flex_extract software locally or \
[efdb01a]114                            on ECMWF machines',
115                            formatter_class=ArgumentDefaultsHelpFormatter)
116
[54a8a01]117    parser.add_argument('--target', dest='install_target', default=None,
[efdb01a]118                        help="Valid targets: local | ecgate | cca , \
119                        the latter two are at ECMWF")
[54a8a01]120    parser.add_argument("--makefile", dest="makefile", default=None,
[efdb01a]121                        help='Name of Makefile to use for compiling CONVERT2')
[54a8a01]122    parser.add_argument("--ecuid", dest="ecuid", default=None,
[efdb01a]123                        help='user id at ECMWF')
[54a8a01]124    parser.add_argument("--ecgid", dest="ecgid", default=None,
[efdb01a]125                        help='group id at ECMWF')
[54a8a01]126    parser.add_argument("--gateway", dest="gateway", default=None,
[efdb01a]127                        help='name of local gateway server')
[54a8a01]128    parser.add_argument("--destination", dest="destination", default=None,
[efdb01a]129                        help='ecaccess destination, e.g. leo@genericSftp')
130
131    parser.add_argument("--flexpart_root_scripts", dest="flexpart_root_scripts",
[54a8a01]132                        default=None, help="FLEXPART root directory on ECMWF \
133                        servers (to find grib2flexpart and COMMAND file)\n\
134                        Normally flex_extract resides in the scripts directory \
[2fb99de]135                        of the FLEXPART distribution.")
[efdb01a]136
[54a8a01]137    # arguments for job submission to ECMWF, only needed by submit.py
[efdb01a]138    parser.add_argument("--job_template", dest='job_template',
139                        default="job.temp.o",
140                        help="job template file for submission to ECMWF")
141
142    parser.add_argument("--controlfile", dest="controlfile",
143                        default='CONTROL.temp',
[991df6a]144                        help="file with CONTROL parameters")
[efdb01a]145
146    args = parser.parse_args()
147
[54a8a01]148    return args
149
[efdb01a]150
[54a8a01]151def install_via_gateway(c):
[a4b6cef]152    '''
[991df6a]153    @Description:
154        Perform the actual installation on local machine or prepare data
155        transfer to remote gate and submit a job script which will
156        install everything on the remote gate.
[a4b6cef]157
[991df6a]158    @Input:
159        c: instance of class ControlFile
160            Contains all necessary information of a CONTROL file. The parameters
161            are: DAY1, DAY2, DTIME, MAXSTEP, TYPE, TIME, STEP, CLASS, STREAM,
162            NUMBER, EXPVER, GRID, LEFT, LOWER, UPPER, RIGHT, LEVEL, LEVELIST,
163            RESOL, GAUSS, ACCURACY, OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA,
164            SMOOTH, FORMAT, ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS,
165            ECFSDIR, MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR
166            For more information about format and content of the parameter see
167            documentation.
168
[54a8a01]169    @Return:
170        <nothing>
171    '''
[2fb99de]172    import tarfile
173
174    ecd = _config.PATH_FLEXEXTRACT_DIR
175    tarball_name = _config.FLEXEXTRACT_DIRNAME + '.tar'
176    tar_file = os.path.join(ecd, tarball_name)
[54a8a01]177
[2fb99de]178    target_dirname = _config.FLEXEXTRACT_DIRNAME
179    fortran_executable = _config.FORTRAN_EXECUTABLE
[54a8a01]180
[97e09f4]181    if c.install_target.lower() != 'local': # ecgate or cca
[54a8a01]182
[2fb99de]183        mk_compilejob(c.makefile, c.install_target, c.ecuid, c.ecgid,
[54a8a01]184                      c.flexpart_root_scripts)
185
[2fb99de]186        mk_job_template(c.ecuid, c.ecgid, c.gateway,
[54a8a01]187                        c.destination, c.flexpart_root_scripts)
188
[2fb99de]189        mk_env_vars(c.ecuid, c.ecgid, c.gateway, c.destination)
[54a8a01]190
[2fb99de]191        mk_tarball(tar_file)
[54a8a01]192
193        put_file_to_ecserver(ecd, tarball_name, c.install_target,
194                             c.ecuid, c.ecgid)
195
[2fb99de]196        submit_job_to_ecserver(c.install_target,
197                               os.path.join(_config.PATH_RELATIVE_JOBSCRIPTS,
198                                            _config.FILE_INSTALL_COMPILEJOB))
[54a8a01]199
[2fb99de]200        print('job compilation script has been submitted to ecgate for ' +
201              'installation in ' + c.flexpart_root_scripts +
202               '/' + target_dirname)
203        print('You should get an email with subject "flexcompile" within ' +
204              'the next few minutes!')
[54a8a01]205
206    else: #local
[2fb99de]207        if c.flexpart_root_scripts == _config.PATH_FLEXEXTRACT_DIR :
208            print('WARNING: FLEXPART_ROOT_SCRIPTS has not been specified')
209            print('flex_extract will be installed in here by compiling the ' +
210                  'Fortran source in ' + _config.PATH_FORTRAN_SRC)
211            os.chdir(_config.PATH_FORTRAN_SRC)
[54a8a01]212        else: # creates the target working directory for flex_extract
213            c.flexpart_root_scripts = os.path.expandvars(os.path.expanduser(
[2fb99de]214                                        c.flexpart_root_scripts))
[54a8a01]215            if os.path.abspath(ecd) != os.path.abspath(c.flexpart_root_scripts):
[2fb99de]216                mk_tarball(tar_file)
217                make_dir(os.path.join(c.flexpart_root_scripts,
218                                      target_dirname))
219                os.chdir(os.path.join(c.flexpart_root_scripts,
220                                      target_dirname))
221                un_tarball(tar_file)
222                os.chdir(os.path.join(c.flexpart_root_scripts,
223                                      target_dirname,
224                                      _config.PATH_RELATIVE_FORTRAN_SRC))
[54a8a01]225
226        # Create Fortran executable - CONVERT2
[2fb99de]227        print('Install ' + target_dirname + ' software at ' +
228              c.install_target + ' in directory ' +
229              os.path.abspath(c.flexpart_root_scripts) + '\n')
230
231        delete_convert_build('.')
232        make_convert_build('.', c.makefile)
[54a8a01]233
[2fb99de]234        os.chdir(ecd)
235        if os.path.isfile(tar_file):
236            os.remove(tar_file)
[54a8a01]237
238    return
239
[2fb99de]240def mk_tarball(tarball_path):
[54a8a01]241    '''
242    @Description:
[2fb99de]243        Creates a tarball with all necessary files which need to be sent to the
[54a8a01]244        installation directory.
245        It does not matter if this is local or remote.
246        Collects all python files, the Fortran source and makefiles,
[2fb99de]247        the ECMWF_ENV file, the CONTROL files as well as the
248        template files.
[54a8a01]249
250    @Input:
[2fb99de]251        tarball_path: string
252            The complete path to the tar file which will contain all
253            relevant data for flex_extract.
[54a8a01]254
255    @Return:
256        <nothing>
257    '''
[2fb99de]258    import tarfile
259    from glob import glob
260
261    print('Create tarball ...')
262
263    # change to FLEXEXTRACT directory so that the tar can contain
264    # relative pathes to the files and directories
265    ecd = _config.PATH_FLEXEXTRACT_DIR + '/'
266    os.chdir(ecd)
267
268    # get lists of the files to be added to the tar file
269    ECMWF_ENV_FILE = [_config.PATH_RELATIVE_ECMWF_ENV]
270    pyfiles = [os.path.relpath(x, ecd)
271               for x in glob(_config.PATH_LOCAL_PYTHON +
272                             os.path.sep + '*py')]
273    controlfiles = [os.path.relpath(x, ecd)
274                    for x in glob(_config.PATH_CONTROLFILES +
275                                  os.path.sep + 'CONTROL*')]
276    tempfiles = [os.path.relpath(x, ecd)
277                 for x in glob(_config.PATH_TEMPLATES +
278                               os.path.sep + '*')]
279    ffiles = [os.path.relpath(x, ecd)
280              for x in glob(_config.PATH_FORTRAN_SRC +
281                            os.path.sep + '*.f*')]
282    hfiles = [os.path.relpath(x, ecd)
283              for x in glob(_config.PATH_FORTRAN_SRC +
284                            os.path.sep + '*.h')]
285    makefiles = [os.path.relpath(x, ecd)
286                 for x in glob(_config.PATH_FORTRAN_SRC +
287                               os.path.sep + 'Makefile*')]
288
289    # concatenate single lists to one for a better looping
290    filelist = pyfiles + controlfiles + tempfiles + ffiles + hfiles + \
291               makefiles + ECMWF_ENV_FILE
292
293    # create installation tar-file
[54a8a01]294    try:
[2fb99de]295        with tarfile.open(tarball_path, "w:gz") as tar_handle:
296            for file in filelist:
297                tar_handle.add(file)
298
[54a8a01]299    except subprocess.CalledProcessError as e:
[2fb99de]300        print('... ERROR CODE:\n ... ' + str(e.returncode))
301        print('... ERROR MESSAGE:\n ... ' + str(e))
302
303        sys.exit('... could not make installation tar ball!')
[54a8a01]304
305    return
306
[2fb99de]307
308def un_tarball(tarball_path):
309    '''
310    @Description:
311        Extracts the given tarball into current directory.
312
313    @Input:
314        tarball_path: string
315            The complete path to the tar file which will contain all
316            relevant data for flex_extract.
317
318    @Return:
319        <nothing>
320    '''
321    import tarfile
322
323    print('Untar ...')
324
325    with tarfile.open(tarball_path) as tar_handle:
326        tar_handle.extractall()
327
328    return
329
330def mk_env_vars(ecuid, ecgid, gateway, destination):
[54a8a01]331    '''
332    @Description:
333        Creates a file named ECMWF_ENV which contains the
334        necessary environmental variables at ECMWF servers.
335
336    @Input:
337        ecuid: string
338            The user id on ECMWF server.
339
340        ecgid: string
341            The group id on ECMWF server.
342
343        gateway: string
344            The gateway server the user is using.
345
346        destination: string
347            The remote destination which is used to transfer files
348            from ECMWF server to local gateway server.
349
350    @Return:
351        <nothing>
352    '''
353
[2fb99de]354    with open(_config.PATH_RELATIVE_ECMWF_ENV, 'w') as fo:
[54a8a01]355        fo.write('ECUID ' + ecuid + '\n')
356        fo.write('ECGID ' + ecgid + '\n')
357        fo.write('GATEWAY ' + gateway + '\n')
358        fo.write('DESTINATION ' + destination + '\n')
359
360    return
361
[2fb99de]362def mk_compilejob(makefile, target, ecuid, ecgid, fp_root):
[54a8a01]363    '''
364    @Description:
365        Modifies the original job template file so that it is specified
366        for the user and the environment were it will be applied. Result
367        is stored in a new file "job.temp" in the python directory.
368
369    @Input:
370        makefile: string
371            Name of the makefile which should be used to compile FORTRAN
372            CONVERT2 program.
373
[991df6a]374        target: string
[54a8a01]375            The target where the installation should be done, e.g. the queue.
376
377        ecuid: string
378            The user id on ECMWF server.
379
380        ecgid: string
381            The group id on ECMWF server.
382
383        fp_root: string
384           Path to the root directory of FLEXPART environment or flex_extract
385           environment.
[a4b6cef]386
[991df6a]387    @Return:
388        <nothing>
389    '''
[54a8a01]390
[2fb99de]391    template = os.path.join(_config.PATH_RELATIVE_TEMPLATES,
392                            _config.TEMPFILE_INSTALL_COMPILEJOB)
[d69b677]393    with open(template) as f:
[a4b6cef]394        fdata = f.read().split('\n')
[54a8a01]395
[2fb99de]396    compilejob = os.path.join(_config.PATH_RELATIVE_JOBSCRIPTS,
397                              _config.FILE_INSTALL_COMPILEJOB)
398    with open(compilejob, 'w') as fo:
[a4b6cef]399        for data in fdata:
400            if 'MAKEFILE=' in data:
[54a8a01]401                data = 'export MAKEFILE=' + makefile
402            elif 'FLEXPART_ROOT_SCRIPTS=' in data:
403                if fp_root != '../':
404                    data = 'export FLEXPART_ROOT_SCRIPTS=' + fp_root
[a4b6cef]405                else:
[ff99eae]406                    data = 'export FLEXPART_ROOT_SCRIPTS=$HOME'
[54a8a01]407            elif target.lower() != 'local':
[a4b6cef]408                if '--workdir' in data:
[54a8a01]409                    data = '#SBATCH --workdir=/scratch/ms/' + \
410                            ecgid + '/' + ecuid
411                elif '##PBS -o' in data:
412                    data = '##PBS -o /scratch/ms/' + ecgid + '/' + ecuid + \
413                           'flex_ecmwf.$Jobname.$Job_ID.out'
414                elif 'FLEXPART_ROOT_SCRIPTS=' in data:
415                    if fp_root != '../':
416                        data = 'export FLEXPART_ROOT_SCRIPTS=' + fp_root
[a4b6cef]417                    else:
418                        data = 'export FLEXPART_ROOT_SCRIPTS=$HOME'
419            fo.write(data + '\n')
[54a8a01]420
421    return
422
[2fb99de]423def mk_job_template(ecuid, ecgid, gateway, destination, fp_root):
[54a8a01]424    '''
425    @Description:
426        Modifies the original job template file so that it is specified
427        for the user and the environment were it will be applied. Result
[2fb99de]428        is stored in a new file.
[54a8a01]429
430    @Input:
431        ecuid: string
432            The user id on ECMWF server.
433
434        ecgid: string
435            The group id on ECMWF server.
436
437        gateway: string
438            The gateway server the user is using.
439
440        destination: string
441            The remote destination which is used to transfer files
442            from ECMWF server to local gateway server.
443
444        fp_root: string
445           Path to the root directory of FLEXPART environment or flex_extract
446           environment.
447
448    @Return:
449        <nothing>
450    '''
[2fb99de]451    fp_root_path_to_python = os.path.join(fp_root, _config.FLEXEXTRACT_DIRNAME,
452                         _config.PATH_RELATIVE_PYTHON)
[54a8a01]453
[2fb99de]454    template = os.path.join(_config.PATH_RELATIVE_TEMPLATES,
455                            _config.TEMPFILE_INSTALL_JOB)
[54a8a01]456    with open(template) as f:
457        fdata = f.read().split('\n')
458
[2fb99de]459    jobfile_temp = os.path.join(_config.PATH_RELATIVE_TEMPLATES,
460                                _config.TEMPFILE_JOB)
461    with open(jobfile_temp, 'w') as fo:
[a4b6cef]462        for data in fdata:
463            if '--workdir' in data:
[2fb99de]464                data = '#SBATCH --workdir=/scratch/ms/' + ecgid + '/' + ecuid
[54a8a01]465            elif '##PBS -o' in data:
466                data = '##PBS -o /scratch/ms/' + ecgid + '/' + \
467                        ecuid + 'flex_ecmwf.$Jobname.$Job_ID.out'
468            elif  'export PATH=${PATH}:' in data:
[2fb99de]469                data += fp_root_path_to_python
[a4b6cef]470
471            fo.write(data + '\n')
[54a8a01]472    return
473
[2fb99de]474def delete_convert_build(src_path):
[54a8a01]475    '''
476    @Description:
477        Clean up the Fortran source directory and remove all
478        build files (e.g. *.o, *.mod and CONVERT2)
479
480    @Input:
[2fb99de]481        src_path: string
482            Path to the fortran source directory.
[54a8a01]483
484    @Return:
485        <nothing>
486    '''
487
[2fb99de]488    modfiles = UioFiles(src_path, '*.mod')
489    objfiles = UioFiles(src_path, '*.o')
490    exefile = UioFiles(src_path, _config.FORTRAN_EXECUTABLE)
[54a8a01]491
492    modfiles.delete_files()
493    objfiles.delete_files()
494    exefile.delete_files()
495
496    return
497
[2fb99de]498def make_convert_build(src_path, makefile):
[54a8a01]499    '''
500    @Description:
501        Compiles the Fortran code and generates the executable.
502
503    @Input:
[2fb99de]504        src_path: string
505            Path to the fortran source directory.
[54a8a01]506
507        makefile: string
508            The name of the makefile which should be used.
509
510    @Return:
511        <nothing>
512    '''
513
514    try:
[2fb99de]515        print('Using makefile: ' + makefile)
516        p = subprocess.Popen(['make', '-f',
517                              os.path.join(src_path, makefile)],
[54a8a01]518                             stdin=subprocess.PIPE,
519                             stdout=subprocess.PIPE,
520                             stderr=subprocess.PIPE,
521                             bufsize=1)
522        pout, perr = p.communicate()
[2fb99de]523        print(pout)
[54a8a01]524        if p.returncode != 0:
[2fb99de]525            print(perr)
526            print('Please edit ' + makefile +
527                  ' or try another Makefile in the src directory.')
528            print('Most likely GRIB_API_INCLUDE_DIR, GRIB_API_LIB '
529                  'and EMOSLIB must be adapted.')
530            print('Available Makefiles:')
531            print(UioFiles(src_path, 'Makefile*'))
[54a8a01]532            sys.exit('Compilation failed!')
533    except ValueError as e:
[2fb99de]534        print('ERROR: Makefile call failed:')
535        print(e)
[d69b677]536    else:
[2fb99de]537        subprocess.check_call(['ls', '-l',
538                               os.path.join(src_path,
539                                            _config.FORTRAN_EXECUTABLE)])
[a4b6cef]540
[d69b677]541    return
542
[a4b6cef]543
[d69b677]544if __name__ == "__main__":
545    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG