Changeset bf48c8a in flex_extract.git


Ignore:
Timestamp:
Dec 14, 2018, 11:27:42 AM (5 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
1eca806
Parents:
524ac32
Message:

introduced a function for subprocess check_call to do error handling once

Location:
source/python
Files:
3 edited

Legend:

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

    r092aaf1 rbf48c8a  
    9292from GribUtil import GribUtil
    9393from mods.tools import (init128, to_param_id, silent_remove, product,
    94                         my_error, make_dir, get_informations, get_dimensions)
     94                        my_error, make_dir, get_informations, get_dimensions,
     95                        execute_subprocess)
    9596from MarsRetrieval import MarsRetrieval
    9697import mods.disaggregation as disaggregation
     
    14271428
    14281429            # Fortran program creates file fort.15 (with u,v,etadot,t,sp,q)
    1429             p = subprocess.check_call([os.path.join(
    1430                 c.exedir, _config.FORTRAN_EXECUTABLE)], shell=True)
     1430            execute_subprocess([os.path.join(c.exedir,
     1431                                _config.FORTRAN_EXECUTABLE)],
     1432                               error_msg='FORTRAN PROGRAM FAILED!')#shell=True)
     1433
    14311434            os.chdir(pwd)
    14321435#============================================================================================
     
    15071510
    15081511            if c.format.lower() == 'grib2':
    1509                 p = subprocess.check_call(['grib_set', '-s', 'edition=2,',
    1510                                            'productDefinitionTemplateNumber=8',
    1511                                            ofile, ofile + '_2'])
    1512                 p = subprocess.check_call(['mv', ofile + '_2', ofile])
     1512                execute_subprocess(['grib_set', '-s', 'edition=2,' +
     1513                                    'productDefinitionTemplateNumber=8',
     1514                                    ofile, ofile + '_2'],
     1515                                   error_msg='GRIB2 CONVERSION FAILED!')
     1516
     1517                execute_subprocess(['mv', ofile + '_2', ofile],
     1518                                   error_msg='RENAMING FOR NEW GRIB2 FORMAT '
     1519                                   'FILES FAILED!')
    15131520
    15141521            if c.ectrans and not c.ecapi:
    1515                 p = subprocess.check_call(['ectrans', '-overwrite', '-gateway',
    1516                                            c.gateway, '-remote', c.destination,
    1517                                            '-source', ofile])
     1522                execute_subprocess(['ectrans', '-overwrite', '-gateway',
     1523                                    c.gateway, '-remote', c.destination,
     1524                                    '-source', ofile],
     1525                                   error_msg='TRANSFER TO LOCAL SERVER FAILED!')
    15181526
    15191527            if c.ecstorage and not c.ecapi:
    1520                 p = subprocess.check_call(['ecp', '-o', ofile,
    1521                                            os.path.expandvars(c.ecfsdir)])
     1528                execute_subprocess(['ecp', '-o', ofile,
     1529                                    os.path.expandvars(c.ecfsdir)],
     1530                                   error_msg='COPY OF FILES TO ECSTORAGE '
     1531                                   'AREA FAILED!')
    15221532
    15231533            if c.outputdir != c.inputdir:
    1524                 p = subprocess.check_call(['mv',
    1525                                            os.path.join(c.inputdir, ofile),
    1526                                            c.outputdir])
     1534                execute_subprocess(['mv', os.path.join(c.inputdir, ofile),
     1535                                    c.outputdir],
     1536                                   error_msg='RELOCATION OF OUTPUT FILES '
     1537                                   'TO OUTPUTDIR FAILED!')
    15271538
    15281539        return
     
    16041615        # afterwards switch back to the working dir
    16051616        os.chdir(c.outputdir)
    1606         p = subprocess.check_call([
    1607             os.path.expandvars(os.path.expanduser(c.flexpartdir))
    1608             + '/../FLEXPART_PROGRAM/grib2flexpart', 'useAvailable', '.'])
     1617        cmd = [os.path.expandvars(os.path.expanduser(c.flexpartdir)) +
     1618         '/../FLEXPART_PROGRAM/grib2flexpart', 'useAvailable', '.']
     1619        execute_subprocess(cmd)
    16091620        os.chdir(pwd)
    16101621
  • source/python/install.py

    r8652e7e rbf48c8a  
    6262from classes.UioFiles import UioFiles
    6363from mods.tools import (make_dir, put_file_to_ecserver, submit_job_to_ecserver,
    64                         silent_remove)
     64                        silent_remove, execute_subprocess)
    6565
    6666# ------------------------------------------------------------------------------
     
    677677        print(e)
    678678    else:
    679         subprocess.check_call(['ls', '-l',
    680                                os.path.join(src_path,
    681                                             _config.FORTRAN_EXECUTABLE)])
     679        execute_subprocess(['ls', '-l', os.path.join(src_path,
     680                            _config.FORTRAN_EXECUTABLE)], error_msg=
     681                           'FORTRAN EXECUTABLE COULD NOT BE FOUND!')
    682682
    683683    return
  • source/python/mods/tools.py

    r9aefaad rbf48c8a  
    743743
    744744    return (ix, jy, it)
     745
     746
     747def execute_subprocess(cmd_list, error_msg='SUBPROCESS FAILED!'):
     748    '''Executes a command line instruction via a subprocess.
     749
     750    Error handling is done if an error occures.
     751
     752    Parameters
     753    ----------
     754    cmd_list : :obj:`list` of `:obj:`string`
     755        A list of the components for the command line execution. Each
     756        list entry is a single part of the command which is seperated from
     757        the rest by a blank space.
     758        E.g. ['mv', file1, file2]
     759
     760    Return
     761    ------
     762    error_msg : :obj:`string`, optional
     763        The possible error message if the subprocess failed.
     764        By default it will just tell "SUBPROCESS FAILED!".
     765    '''
     766
     767    try:
     768        subprocess.check_call(cmd_list)
     769    except subprocess.CalledProcessError as e:
     770        print('... ERROR CODE: ' + str(e.returncode))
     771        print('... ERROR MESSAGE:\n \t ' + str(e))
     772
     773        sys.exit('... ' + error_msg)
     774    except OSError as e:
     775        print('... ERROR CODE: ' + str(e.errno))
     776        print('... ERROR MESSAGE:\n \t ' + str(e.strerror))
     777
     778        sys.exit('... ' + error_msg)
     779
     780    return
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG