Changeset 54a8a01 in flex_extract.git for python/install.py


Ignore:
Timestamp:
Aug 31, 2018, 7:50:37 AM (6 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
597d4d1
Parents:
e1228f3
Message:

restructuring, documentations and bug fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/install.py

    rff99eae r54a8a01  
    11#!/usr/bin/env python
    22# -*- coding: utf-8 -*-
    3 #************************************************************************
    4 # ToDo AP
    5 # - create a class Installation and divide installation in 3 subdefs for
    6 #   ecgate, local and cca seperatly
    7 # - Change History ist nicht angepasst ans File! Original geben lassen
    8 #************************************************************************
    93#*******************************************************************************
    104# @Author: Leopold Haimberger (University of Vienna)
     
    3731# @Program Content:
    3832#    - main
    39 #    - install_args_and_control
     33#    - get_install_cmdline_arguments
    4034#    - install_via_gateway
     35#    - mk_tarball
     36#    - mk_env_vars
     37#    - mk_compilejob
     38#    - mk_job_template
     39#    - delete_convert_build
     40#    - make_convert_build
    4141#
    4242#*******************************************************************************
     
    4747import os
    4848import sys
    49 import glob
    5049import subprocess
    5150import inspect
     
    5453# software specific classes and modules from flex_extract
    5554from ControlFile import ControlFile
     55from UioFiles import UioFiles
     56from tools import make_dir, put_file_to_ecserver, submit_job_to_ecserver
    5657
    5758# add path to pythonpath so that python finds its buddies
     
    6162    sys.path.append(LOCAL_PYTHON_PATH)
    6263
     64_VERSION_STR = '7.1'
     65
    6366# ------------------------------------------------------------------------------
    6467# FUNCTIONS
     
    7881
    7982    os.chdir(LOCAL_PYTHON_PATH)
    80     args, c = install_args_and_control()
    81 
    82     if args.install_target is not None:
    83         install_via_gateway(c, args.install_target)
    84     else:
    85         print 'Please specify installation target (local|ecgate|cca)'
    86         print 'use -h or --help for help'
    87 
    88     sys.exit()
    89 
    90     return
    91 
    92 
    93 def install_args_and_control():
    94     '''
    95     @Description:
    96         Assigns the command line arguments for installation and reads
    97         CONTROL file content. Apply default values for non mentioned arguments.
     83    args = get_install_cmdline_arguments()
     84
     85    try:
     86        c = ControlFile(args.controlfile)
     87    except IOError:
     88        print 'Could not read CONTROL file "' + args.controlfile + '"'
     89        print 'Either it does not exist or its syntax is wrong.'
     90        print 'Try "' + sys.argv[0].split('/')[-1] + \
     91              ' -h" to print usage information'
     92        exit(1)
     93
     94    c.assign_args_to_control(args)
     95    c.check_install_conditions()
     96
     97    install_via_gateway(c)
     98
     99    return
     100
     101def get_install_cmdline_arguments():
     102    '''
     103    @Description:
     104        Decomposes the command line arguments and assigns them to variables.
     105        Apply default values for non mentioned arguments.
    98106
    99107    @Input:
     
    103111        args: instance of ArgumentParser
    104112            Contains the commandline arguments from script/program call.
    105 
     113    '''
     114    parser = ArgumentParser(description='Install flex_extract software locally or \
     115                            on ECMWF machines',
     116                            formatter_class=ArgumentDefaultsHelpFormatter)
     117
     118    parser.add_argument('--target', dest='install_target', default=None,
     119                        help="Valid targets: local | ecgate | cca , \
     120                        the latter two are at ECMWF")
     121    parser.add_argument("--makefile", dest="makefile", default=None,
     122                        help='Name of Makefile to use for compiling CONVERT2')
     123    parser.add_argument("--ecuid", dest="ecuid", default=None,
     124                        help='user id at ECMWF')
     125    parser.add_argument("--ecgid", dest="ecgid", default=None,
     126                        help='group id at ECMWF')
     127    parser.add_argument("--gateway", dest="gateway", default=None,
     128                        help='name of local gateway server')
     129    parser.add_argument("--destination", dest="destination", default=None,
     130                        help='ecaccess destination, e.g. leo@genericSftp')
     131
     132    parser.add_argument("--flexpart_root_scripts", dest="flexpart_root_scripts",
     133                        default=None, help="FLEXPART root directory on ECMWF \
     134                        servers (to find grib2flexpart and COMMAND file)\n\
     135                        Normally flex_extract resides in the scripts directory \
     136                        of the FLEXPART distribution, thus the:")
     137
     138    # arguments for job submission to ECMWF, only needed by submit.py
     139    parser.add_argument("--job_template", dest='job_template',
     140                        default="job.temp.o",
     141                        help="job template file for submission to ECMWF")
     142
     143    parser.add_argument("--controlfile", dest="controlfile",
     144                        default='CONTROL.temp',
     145                        help="file with CONTROL parameters")
     146
     147    args = parser.parse_args()
     148
     149    return args
     150
     151
     152def install_via_gateway(c):
     153    '''
     154    @Description:
     155        Perform the actual installation on local machine or prepare data
     156        transfer to remote gate and submit a job script which will
     157        install everything on the remote gate.
     158
     159    @Input:
    106160        c: instance of class ControlFile
    107161            Contains all necessary information of a CONTROL file. The parameters
     
    113167            For more information about format and content of the parameter see
    114168            documentation.
    115     '''
    116     parser = ArgumentParser(description='Install ECMWFDATA software locally or \
    117                             on ECMWF machines',
    118                             formatter_class=ArgumentDefaultsHelpFormatter)
    119 
    120     parser.add_argument('--target', dest='install_target',
    121                         help="Valid targets: local | ecgate | cca , \
    122                         the latter two are at ECMWF")
    123     parser.add_argument("--makefile", dest="makefile",
    124                         help='Name of Makefile to use for compiling CONVERT2')
    125     parser.add_argument("--ecuid", dest="ecuid",
    126                         help='user id at ECMWF')
    127     parser.add_argument("--ecgid", dest="ecgid",
    128                         help='group id at ECMWF')
    129     parser.add_argument("--gateway", dest="gateway",
    130                         help='name of local gateway server')
    131     parser.add_argument("--destination", dest="destination",
    132                         help='ecaccess destination, e.g. leo@genericSftp')
    133 
    134     parser.add_argument("--flexpart_root_scripts", dest="flexpart_root_scripts",
    135                         help="FLEXPART root directory on ECMWF servers \
    136                         (to find grib2flexpart and COMMAND file)\n\
    137                         Normally ECMWFDATA resides in the scripts directory \
    138                         of the FLEXPART distribution, thus the:")
    139 
    140 # arguments for job submission to ECMWF, only needed by submit.py
    141     parser.add_argument("--job_template", dest='job_template',
    142                         default="job.temp.o",
    143                         help="job template file for submission to ECMWF")
    144 
    145     parser.add_argument("--controlfile", dest="controlfile",
    146                         default='CONTROL.temp',
    147                         help="file with CONTROL parameters")
    148 
    149     args = parser.parse_args()
    150 
    151     try:
    152         c = ControlFile(args.controlfile)
    153     except IOError:
    154         print 'Could not read CONTROL file "' + args.controlfile + '"'
    155         print 'Either it does not exist or its syntax is wrong.'
    156         print 'Try "' + sys.argv[0].split('/')[-1] + \
    157               ' -h" to print usage information'
    158         exit(1)
    159 
    160     if args.install_target != 'local':
    161         if args.ecgid is None or args.ecuid is None or args.gateway is None \
    162            or args.destination is None:
    163             print 'Please enter your ECMWF user id and group id as well as \
    164                    the \nname of the local gateway and the ectrans \
    165                    destination '
    166             print 'with command line options --ecuid --ecgid \
    167                    --gateway --destination'
    168             print 'Try "' + sys.argv[0].split('/')[-1] + \
    169                   ' -h" to print usage information'
    170             print 'Please consult ecaccess documentation or ECMWF user support \
    171                    for further details'
    172             sys.exit(1)
    173         else:
    174             c.ecuid = args.ecuid
    175             c.ecgid = args.ecgid
    176             c.gateway = args.gateway
    177             c.destination = args.destination
    178 
    179     if args.makefile:
    180         c.makefile = args.makefile
    181 
    182     if args.install_target == 'local':
    183         if args.flexpart_root_scripts is None:
    184             c.flexpart_root_scripts = '../'
    185         else:
    186             c.flexpart_root_scripts = args.flexpart_root_scripts
    187 
    188     if args.install_target != 'local':
    189         if args.flexpart_root_scripts is None:
    190             c.ec_flexpart_root_scripts = '${HOME}'
    191         else:
    192             c.ec_flexpart_root_scripts = args.flexpart_root_scripts
    193 
    194     return args, c
    195 
    196 
    197 def install_via_gateway(c, target):
    198     '''
    199     @Description:
    200         Perform the actual installation on local machine or prepare data
    201         transfer to remote gate and submit a job script which will
    202         install everything on the remote gate.
    203 
    204     @Input:
    205         c: instance of class ControlFile
    206             Contains all necessary information of a CONTROL file. The parameters
    207             are: DAY1, DAY2, DTIME, MAXSTEP, TYPE, TIME, STEP, CLASS, STREAM,
    208             NUMBER, EXPVER, GRID, LEFT, LOWER, UPPER, RIGHT, LEVEL, LEVELIST,
    209             RESOL, GAUSS, ACCURACY, OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA,
    210             SMOOTH, FORMAT, ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS,
    211             ECFSDIR, MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR
    212             For more information about format and content of the parameter see
    213             documentation.
    214 
    215         target: string
    216             The target where the installation should be processed.
    217             E.g. "local", "ecgate" or "cca"
    218 
    219     @Return:
    220         <nothing>
    221     '''
     169
     170    @Return:
     171        <nothing>
     172    '''
     173
    222174    ecd = c.ecmwfdatadir
    223     template = ecd + 'python/compilejob.temp'
    224     job = ecd + 'python/compilejob.ksh'
    225     fo = open(job, 'w')
    226 #AP could do with open(template) as f, open(job, 'w') as fo:
    227 #AP or nested with statements
    228     with open(template) as f:
    229         fdata = f.read().split('\n')
    230         for data in fdata:
    231             if 'MAKEFILE=' in data:
    232                 if c.makefile is not None:
    233                     data = 'export MAKEFILE=' + c.makefile
    234             if 'FLEXPART_ROOT_SCRIPTS=' in data:
    235                 if c.flexpart_root_scripts != '../':
    236                     data = 'export FLEXPART_ROOT_SCRIPTS=' + \
    237                             c.flexpart_root_scripts
    238                 else:
    239                     data = 'export FLEXPART_ROOT_SCRIPTS=$HOME'
    240             if target.lower() != 'local':
    241                 if '--workdir' in data:
    242                     data = '#SBATCH --workdir=/scratch/ms/' + c.ecgid + \
    243                             '/' + c.ecuid
    244                 if '##PBS -o' in data:
    245                     data = '##PBS -o /scratch/ms/' + c.ecgid + '/' + c.ecuid + \
    246                             'flex_ecmwf.$Jobname.$Job_ID.out'
    247                 if 'FLEXPART_ROOT_SCRIPTS=' in data:
    248                     if c.ec_flexpart_root_scripts != '../':
    249                         data = 'export FLEXPART_ROOT_SCRIPTS=' + \
    250                                 c.ec_flexpart_root_scripts
    251                     else:
    252                         data = 'export FLEXPART_ROOT_SCRIPTS=$HOME'
    253             fo.write(data + '\n')
    254     f.close()
    255     fo.close()
    256 
    257     if target.lower() != 'local':
    258         template = ecd + 'python/job.temp.o'
    259 #AP hier eventuell Zeile für Zeile lesen und dann if Entscheidung
    260         with open(template) as f:
    261             fdata = f.read().split('\n')
    262         f.close()
    263         fo = open(template[:-2], 'w')
    264         for data in fdata:
    265             if '--workdir' in data:
    266                 data = '#SBATCH --workdir=/scratch/ms/' + c.ecgid + \
    267                         '/' + c.ecuid
    268             if '##PBS -o' in data:
    269                 data = '##PBS -o /scratch/ms/' + c.ecgid + '/' + \
    270                         c.ecuid + 'flex_ecmwf.$Jobname.$Job_ID.out'
    271             if  'export PATH=${PATH}:' in data:
    272                 data += c.ec_flexpart_root_scripts + '/ECMWFDATA7.1/python'
    273             if 'cat>>' in data or 'cat >>' in data:
    274                 i = data.index('>')
    275                 fo.write(data[:i] + data[i+1:] + '\n')
    276                 fo.write('GATEWAY ' + c.gateway + '\n')
    277                 fo.write('DESTINATION ' + c.destination + '\n')
    278                 fo.write('EOF\n')
    279 
    280             fo.write(data + '\n')
    281         fo.close()
    282 
    283         job = ecd + 'python/ECMWF_ENV'
    284         with open(job, 'w') as fo:
    285             fo.write('ECUID ' + c.ecuid + '\n')
    286             fo.write('ECGID ' + c.ecgid + '\n')
    287             fo.write('GATEWAY ' + c.gateway + '\n')
    288             fo.write('DESTINATION ' + c.destination + '\n')
    289         fo.close()
    290 
    291     if target.lower() == 'local':
    292         # compile CONVERT2
    293         if c.flexpart_root_scripts is None or c.flexpart_root_scripts == '../':
    294             print 'Warning: FLEXPART_ROOT_SCRIPTS has not been specified'
    295             print 'Only CONVERT2 will be compiled in ' + ecd + '/../src'
    296         else:
     175    tarball_name = 'flex_extract_v' + _VERSION_STR + '.tar'
     176    target_dir = 'flex_extract_v' + _VERSION_STR
     177    fortran_executable = 'CONVERT2'
     178
     179    if c.install_target.lower() != 'local':
     180
     181        mk_compilejob(ecd + 'python/compilejob.temp', c.makefile,
     182                      c.install_target, c.ecuid, c.ecgid,
     183                      c.flexpart_root_scripts)
     184
     185        mk_job_template(ecd + 'python/job.temp.o', c.ecuid, c.ecgid, c.gateway,
     186                        c.destination, c.flexpart_root_scripts)
     187
     188        mk_env_vars(ecd, c.ecuid, c.ecgid, c.gateway, c.destination)
     189
     190        #os.chdir('/')
     191
     192        mk_tarball(ecd, tarball_name)
     193
     194        put_file_to_ecserver(ecd, tarball_name, c.install_target,
     195                             c.ecuid, c.ecgid)
     196
     197        submit_job_to_ecserver(ecd + '/python/', c.install_target,
     198                               'compilejob.ksh')
     199
     200        print 'job compilation script has been submitted to ecgate for ' + \
     201              'installation in ' + c.flexpart_root_scripts + \
     202               '/' + target_dir
     203        print 'You should get an email with subject flexcompile within ' + \
     204              'the next few minutes!'
     205
     206    else: #local
     207        if not c.flexpart_root_scripts or c.flexpart_root_scripts == '../':
     208            print 'WARNING: FLEXPART_ROOT_SCRIPTS has not been specified'
     209            print 'There will be only the compilation of ' + \
     210                  ' in ' + ecd + '/src'
     211            os.chdir(ecd + '/src')
     212        else: # creates the target working directory for flex_extract
    297213            c.flexpart_root_scripts = os.path.expandvars(os.path.expanduser(
    298214                c.flexpart_root_scripts))
    299215            if os.path.abspath(ecd) != os.path.abspath(c.flexpart_root_scripts):
    300216                os.chdir('/')
    301                 p = subprocess.check_call(['tar', '-cvf',
    302                                            ecd + '../ECMWFDATA7.1.tar',
    303                                            ecd + 'python',
    304                                            ecd + 'grib_templates',
    305                                            ecd + 'src'])
    306                 try:
    307                     os.makedirs(c.flexpart_root_scripts + '/ECMWFDATA7.1')
    308                 finally:
    309                     pass
    310                 os.chdir(c.flexpart_root_scripts + '/ECMWFDATA7.1')
    311                 p = subprocess.check_call(['tar', '-xvf',
    312                                            ecd + '../ECMWFDATA7.1.tar'])
    313                 os.chdir(c.flexpart_root_scripts + '/ECMWFDATA7.1/src')
    314 
    315         os.chdir('../src')
    316         print(('install ECMWFDATA7.1 software on ' + target + ' in directory '
    317                + os.getcwd()))
    318         if c.makefile is None:
    319             makefile = 'Makefile.local.ifort'
    320         else:
    321             makefile = c.makefile
    322         flist = glob.glob('*.mod') + glob.glob('*.o')
    323         if flist:
    324             p = subprocess.check_call(['rm'] + flist)
    325         try:
    326             print 'Using makefile: ' + makefile
    327             p = subprocess.check_call(['make', '-f', makefile])
    328             p = subprocess.check_call(['ls', '-l', 'CONVERT2'])
    329         except subprocess.CalledProcessError as e:
    330             print 'compile failed with the following error:'
    331             print e.output
    332             print 'please edit ' + makefile + \
     217                mk_tarball(ecd, tarball_name)
     218                make_dir(c.flexpart_root_scripts + '/' + target_dir)
     219                os.chdir(c.flexpart_root_scripts + '/' + target_dir)
     220                print 'Untar ...'
     221                subprocess.check_call(['tar', '-xvf',
     222                                       ecd + '../' + tarball_name])
     223                os.chdir(c.flexpart_root_scripts + '/' + target_dir + '/src')
     224
     225        # Create Fortran executable - CONVERT2
     226        print 'Install ' + target_dir + ' software on ' + \
     227              c.install_target + ' in directory ' + \
     228              os.path.abspath(os.getcwd() + '/../') + '\n'
     229
     230        delete_convert_build('')
     231        make_convert_build('', c.makefile, fortran_executable)
     232
     233    return
     234
     235def mk_tarball(ecd, tarname):
     236    '''
     237    @Description:
     238        Creates a tarball from all files which need to be sent to the
     239        installation directory.
     240        It does not matter if this is local or remote.
     241        Collects all python files, the Fortran source and makefiles,
     242        the ECMWF_ENV file, the CONTROL files as well as
     243        the korn shell and template files.
     244
     245    @Input:
     246        ecd: string
     247            The path were the file is to be stored.
     248
     249        tarname: string
     250            The name of the file to send to the ECMWF server.
     251
     252    @Return:
     253        <nothing>
     254    '''
     255
     256    print 'Create tarball ...'
     257    try:
     258        subprocess.check_call(['tar -cvf '+
     259                               ecd + '../' + tarname + ' ' +
     260                               ecd + 'python/*py ' +
     261                               ecd + 'python/CONTROL* ' +
     262                               ecd + 'python/*ksh ' +
     263                               ecd + 'python/*temp* ' +
     264                               ecd + 'python/ECMWF_ENV ' +
     265                               ecd + 'grib_templates ' +
     266                               ecd + 'src/*.f ' +
     267                               ecd + 'src/*.f90 ' +
     268                               ecd + 'src/*.h ' +
     269                               ecd + 'src/Makefile*'], shell=True)
     270    except subprocess.CalledProcessError as e:
     271        print 'ERROR:'
     272        print e.output
     273        sys.exit('could not make installation tar ball!')
     274
     275    return
     276
     277def mk_env_vars(ecd, ecuid, ecgid, gateway, destination):
     278    '''
     279    @Description:
     280        Creates a file named ECMWF_ENV which contains the
     281        necessary environmental variables at ECMWF servers.
     282
     283    @Input:
     284        ecd: string
     285            The path were the file is to be stored.
     286
     287        ecuid: string
     288            The user id on ECMWF server.
     289
     290        ecgid: string
     291            The group id on ECMWF server.
     292
     293        gateway: string
     294            The gateway server the user is using.
     295
     296        destination: string
     297            The remote destination which is used to transfer files
     298            from ECMWF server to local gateway server.
     299
     300    @Return:
     301        <nothing>
     302    '''
     303
     304    with open(ecd + 'python/ECMWF_ENV', 'w') as fo:
     305        fo.write('ECUID ' + ecuid + '\n')
     306        fo.write('ECGID ' + ecgid + '\n')
     307        fo.write('GATEWAY ' + gateway + '\n')
     308        fo.write('DESTINATION ' + destination + '\n')
     309
     310    return
     311
     312def mk_compilejob(template, makefile, target, ecuid, ecgid, fp_root):
     313    '''
     314    @Description:
     315        Modifies the original job template file so that it is specified
     316        for the user and the environment were it will be applied. Result
     317        is stored in a new file "job.temp" in the python directory.
     318
     319    @Input:
     320        template: string
     321            File which contains the original text for the job template.
     322            It must contain the complete path to the file.
     323
     324        makefile: string
     325            Name of the makefile which should be used to compile FORTRAN
     326            CONVERT2 program.
     327
     328        target: string
     329            The target where the installation should be done, e.g. the queue.
     330
     331        ecuid: string
     332            The user id on ECMWF server.
     333
     334        ecgid: string
     335            The group id on ECMWF server.
     336
     337        fp_root: string
     338           Path to the root directory of FLEXPART environment or flex_extract
     339           environment.
     340
     341    @Return:
     342        <nothing>
     343    '''
     344
     345    with open(template) as f:
     346        fdata = f.read().split('\n')
     347
     348    with open(template[:-4] + 'ksh', 'w') as fo:
     349        for data in fdata:
     350            if 'MAKEFILE=' in data:
     351                data = 'export MAKEFILE=' + makefile
     352            elif 'FLEXPART_ROOT_SCRIPTS=' in data:
     353                if fp_root != '../':
     354                    data = 'export FLEXPART_ROOT_SCRIPTS=' + fp_root
     355                else:
     356                    data = 'export FLEXPART_ROOT_SCRIPTS=$HOME'
     357            elif target.lower() != 'local':
     358                if '--workdir' in data:
     359                    data = '#SBATCH --workdir=/scratch/ms/' + \
     360                            ecgid + '/' + ecuid
     361                elif '##PBS -o' in data:
     362                    data = '##PBS -o /scratch/ms/' + ecgid + '/' + ecuid + \
     363                           'flex_ecmwf.$Jobname.$Job_ID.out'
     364                elif 'FLEXPART_ROOT_SCRIPTS=' in data:
     365                    if fp_root != '../':
     366                        data = 'export FLEXPART_ROOT_SCRIPTS=' + fp_root
     367                    else:
     368                        data = 'export FLEXPART_ROOT_SCRIPTS=$HOME'
     369            fo.write(data + '\n')
     370
     371    return
     372
     373def mk_job_template(template, ecuid, ecgid, gateway, destination, fp_root):
     374    '''
     375    @Description:
     376        Modifies the original job template file so that it is specified
     377        for the user and the environment were it will be applied. Result
     378        is stored in a new file "job.temp" in the python directory.
     379
     380    @Input:
     381        template: string
     382            File which contains the original text for the job template.
     383            It must contain the complete path to the file.
     384
     385        ecuid: string
     386            The user id on ECMWF server.
     387
     388        ecgid: string
     389            The group id on ECMWF server.
     390
     391        gateway: string
     392            The gateway server the user is using.
     393
     394        destination: string
     395            The remote destination which is used to transfer files
     396            from ECMWF server to local gateway server.
     397
     398        fp_root: string
     399           Path to the root directory of FLEXPART environment or flex_extract
     400           environment.
     401
     402    @Return:
     403        <nothing>
     404    '''
     405
     406    with open(template) as f:
     407        fdata = f.read().split('\n')
     408
     409    with open(template[:-2], 'w') as fo:
     410        for data in fdata:
     411            if '--workdir' in data:
     412                data = '#SBATCH --workdir=/scratch/ms/' + ecgid + \
     413                        '/' + ecuid
     414            elif '##PBS -o' in data:
     415                data = '##PBS -o /scratch/ms/' + ecgid + '/' + \
     416                        ecuid + 'flex_ecmwf.$Jobname.$Job_ID.out'
     417            elif  'export PATH=${PATH}:' in data:
     418                data += fp_root + '/flex_extract_v7.1/python'
     419
     420            fo.write(data + '\n')
     421    return
     422
     423def delete_convert_build(ecd):
     424    '''
     425    @Description:
     426        Clean up the Fortran source directory and remove all
     427        build files (e.g. *.o, *.mod and CONVERT2)
     428
     429    @Input:
     430        ecd: string
     431            The path to the Fortran program.
     432
     433    @Return:
     434        <nothing>
     435    '''
     436
     437    modfiles = UioFiles(ecd, '*.mod')
     438    objfiles = UioFiles(ecd, '*.o')
     439    exefile = UioFiles(ecd, 'CONVERT2')
     440
     441    modfiles.delete_files()
     442    objfiles.delete_files()
     443    exefile.delete_files()
     444
     445    return
     446
     447def make_convert_build(ecd, makefile, f_executable):
     448    '''
     449    @Description:
     450        Compiles the Fortran code and generates the executable.
     451
     452    @Input:
     453        ecd: string
     454            The path were the file is to be stored.
     455
     456        makefile: string
     457            The name of the makefile which should be used.
     458
     459        f_executable: string
     460            The name of the executable the Fortran program generates after
     461            compilation.
     462
     463    @Return:
     464        <nothing>
     465    '''
     466
     467    try:
     468        print 'Using makefile: ' + makefile
     469        p = subprocess.Popen(['make', '-f', ecd + makefile],
     470                             stdin=subprocess.PIPE,
     471                             stdout=subprocess.PIPE,
     472                             stderr=subprocess.PIPE,
     473                             bufsize=1)
     474        pout, perr = p.communicate()
     475        print pout
     476        if p.returncode != 0:
     477            print perr
     478            print 'Please edit ' + makefile + \
    333479                  ' or try another Makefile in the src directory.'
    334             print 'most likely GRIB_API_INCLUDE_DIR, GRIB_API_LIB \
    335                    and EMOSLIB must be adapted.'
     480            print 'Most likely GRIB_API_INCLUDE_DIR, GRIB_API_LIB ' \
     481                  'and EMOSLIB must be adapted.'
    336482            print 'Available Makefiles:'
    337             print glob.glob('Makefile*')
    338     elif target.lower() == 'ecgate':
    339         os.chdir('/')
    340         p = subprocess.check_call(['tar', '-cvf',
    341                                    ecd + '../ECMWFDATA7.1.tar',
    342                                    ecd + 'python',
    343                                    ecd + 'grib_templates',
    344                                    ecd + 'src'])
    345         try:
    346             p = subprocess.check_call(['ecaccess-file-put',
    347                                        ecd + '../ECMWFDATA7.1.tar',
    348                                        'ecgate:/home/ms/' + c.ecgid + '/' +
    349                                        c.ecuid + '/ECMWFDATA7.1.tar'])
    350         except subprocess.CalledProcessError as e:
    351             print 'ecaccess-file-put failed! \
    352                    Probably the eccert key has expired.'
    353             exit(1)
    354 
    355         try:
    356             p = subprocess.check_call(['ecaccess-job-submit',
    357                                        '-queueName',
    358                                        target,
    359                                        ecd + 'python/compilejob.ksh'])
    360             print 'compilejob.ksh has been submitted to ecgate for  \
    361                    installation in ' + c.ec_flexpart_root_scripts + \
    362                    '/ECMWFDATA7.1'
    363             print 'You should get an email with subject flexcompile within  \
    364                    the next few minutes'
    365         except subprocess.CalledProcessError as e:
    366             print 'ecaccess-job-submit failed!'
    367             exit(1)
    368 
    369     elif target.lower() == 'cca':
    370         os.chdir('/')
    371         p = subprocess.check_call(['tar', '-cvf',
    372                                    ecd + '../ECMWFDATA7.1.tar',
    373                                    ecd + 'python',
    374                                    ecd + 'grib_templates',
    375                                    ecd + 'src'])
    376         try:
    377             p = subprocess.check_call(['ecaccess-file-put',
    378                                        ecd + '../ECMWFDATA7.1.tar',
    379                                        'cca:/home/ms/' + c.ecgid + '/' +
    380                                        c.ecuid + '/ECMWFDATA7.1.tar'])
    381         except subprocess.CalledProcessError as e:
    382             print 'ecaccess-file-put failed! \
    383                    Probably the eccert key has expired.'
    384             exit(1)
    385 
    386         try:
    387             p = subprocess.check_call(['ecaccess-job-submit',
    388                                        '-queueName',
    389                                        target,
    390                                        ecd + 'python/compilejob.ksh'])
    391             print 'compilejob.ksh has been submitted to cca for installation in ' +\
    392                   c.ec_flexpart_root_scripts + '/ECMWFDATA7.1'
    393             print 'You should get an email with subject flexcompile \
    394                    within the next few minutes'
    395         except subprocess.CalledProcessError as e:
    396             print 'ecaccess-job-submit failed!'
    397             exit(1)
    398 
     483            print UioFiles('.', 'Makefile*')
     484            sys.exit('Compilation failed!')
     485    except ValueError as e:
     486        print 'ERROR: Makefile call failed:'
     487        print e
    399488    else:
    400         print 'ERROR: unknown installation target ', target
    401         print 'Valid targets: ecgate, cca, local'
     489        subprocess.check_call(['ls', '-l', ecd + f_executable])
    402490
    403491    return
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG