diff --git a/source/python/classes/EcFlexpart.py b/source/python/classes/EcFlexpart.py index 8ebe300b445cf5d032d4a52945425a0be2394a2c..ffdcc8e635e0a5fbec8e9f3c26c76a6bc49b2adc 100644 --- a/source/python/classes/EcFlexpart.py +++ b/source/python/classes/EcFlexpart.py @@ -91,7 +91,8 @@ sys.path.append('../') import _config from GribUtil import GribUtil from mods.tools import (init128, to_param_id, silent_remove, product, - my_error, make_dir, get_informations, get_dimensions) + my_error, make_dir, get_informations, get_dimensions, + execute_subprocess) from MarsRetrieval import MarsRetrieval import mods.disaggregation as disaggregation @@ -1426,8 +1427,10 @@ class EcFlexpart(object): sys.stdout.flush() # Fortran program creates file fort.15 (with u,v,etadot,t,sp,q) - p = subprocess.check_call([os.path.join( - c.exedir, _config.FORTRAN_EXECUTABLE)], shell=True) + execute_subprocess([os.path.join(c.exedir, + _config.FORTRAN_EXECUTABLE)], + error_msg='FORTRAN PROGRAM FAILED!')#shell=True) + os.chdir(pwd) #============================================================================================ # create name of final output file, e.g. EN13040500 (ENYYMMDDHH) @@ -1506,24 +1509,32 @@ class EcFlexpart(object): ofile = os.path.join(self.inputdir, ofile) if c.format.lower() == 'grib2': - p = subprocess.check_call(['grib_set', '-s', 'edition=2,', - 'productDefinitionTemplateNumber=8', - ofile, ofile + '_2']) - p = subprocess.check_call(['mv', ofile + '_2', ofile]) + execute_subprocess(['grib_set', '-s', 'edition=2,' + + 'productDefinitionTemplateNumber=8', + ofile, ofile + '_2'], + error_msg='GRIB2 CONVERSION FAILED!') + + execute_subprocess(['mv', ofile + '_2', ofile], + error_msg='RENAMING FOR NEW GRIB2 FORMAT ' + 'FILES FAILED!') if c.ectrans and not c.ecapi: - p = subprocess.check_call(['ectrans', '-overwrite', '-gateway', - c.gateway, '-remote', c.destination, - '-source', ofile]) + execute_subprocess(['ectrans', '-overwrite', '-gateway', + c.gateway, '-remote', c.destination, + '-source', ofile], + error_msg='TRANSFER TO LOCAL SERVER FAILED!') if c.ecstorage and not c.ecapi: - p = subprocess.check_call(['ecp', '-o', ofile, - os.path.expandvars(c.ecfsdir)]) + execute_subprocess(['ecp', '-o', ofile, + os.path.expandvars(c.ecfsdir)], + error_msg='COPY OF FILES TO ECSTORAGE ' + 'AREA FAILED!') if c.outputdir != c.inputdir: - p = subprocess.check_call(['mv', - os.path.join(c.inputdir, ofile), - c.outputdir]) + execute_subprocess(['mv', os.path.join(c.inputdir, ofile), + c.outputdir], + error_msg='RELOCATION OF OUTPUT FILES ' + 'TO OUTPUTDIR FAILED!') return @@ -1603,9 +1614,9 @@ class EcFlexpart(object): # change to outputdir and start the grib2flexpart run # afterwards switch back to the working dir os.chdir(c.outputdir) - p = subprocess.check_call([ - os.path.expandvars(os.path.expanduser(c.flexpartdir)) - + '/../FLEXPART_PROGRAM/grib2flexpart', 'useAvailable', '.']) + cmd = [os.path.expandvars(os.path.expanduser(c.flexpartdir)) + + '/../FLEXPART_PROGRAM/grib2flexpart', 'useAvailable', '.'] + execute_subprocess(cmd) os.chdir(pwd) return diff --git a/source/python/install.py b/source/python/install.py index 3998d934a18740b46b5043e8c6296039e8382982..7246cb90fdfeddcc8addd7ab474e2adf265c0b32 100755 --- a/source/python/install.py +++ b/source/python/install.py @@ -61,7 +61,7 @@ import _config from classes.ControlFile import ControlFile from classes.UioFiles import UioFiles from mods.tools import (make_dir, put_file_to_ecserver, submit_job_to_ecserver, - silent_remove) + silent_remove, execute_subprocess) # ------------------------------------------------------------------------------ # FUNCTIONS @@ -676,9 +676,9 @@ def mk_convert_build(src_path, makefile): print('ERROR: Makefile call failed:') print(e) else: - subprocess.check_call(['ls', '-l', - os.path.join(src_path, - _config.FORTRAN_EXECUTABLE)]) + execute_subprocess(['ls', '-l', os.path.join(src_path, + _config.FORTRAN_EXECUTABLE)], error_msg= + 'FORTRAN EXECUTABLE COULD NOT BE FOUND!') return diff --git a/source/python/mods/tools.py b/source/python/mods/tools.py index 03ae957a96b19e9d183dfbe79ef3062995a987f4..00c7b885d9ee38aa1de7466d8195ba02cf487b9f 100644 --- a/source/python/mods/tools.py +++ b/source/python/mods/tools.py @@ -742,3 +742,39 @@ def get_dimensions(info, purefc, dtime, index_vals, start_date, end_date): it = len(index_vals[2]) * len(index_vals[1]) * len(index_vals[0]) return (ix, jy, it) + + +def execute_subprocess(cmd_list, error_msg='SUBPROCESS FAILED!'): + '''Executes a command line instruction via a subprocess. + + Error handling is done if an error occures. + + Parameters + ---------- + cmd_list : :obj:`list` of `:obj:`string` + A list of the components for the command line execution. Each + list entry is a single part of the command which is seperated from + the rest by a blank space. + E.g. ['mv', file1, file2] + + Return + ------ + error_msg : :obj:`string`, optional + The possible error message if the subprocess failed. + By default it will just tell "SUBPROCESS FAILED!". + ''' + + try: + subprocess.check_call(cmd_list) + except subprocess.CalledProcessError as e: + print('... ERROR CODE: ' + str(e.returncode)) + print('... ERROR MESSAGE:\n \t ' + str(e)) + + sys.exit('... ' + error_msg) + except OSError as e: + print('... ERROR CODE: ' + str(e.errno)) + print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) + + sys.exit('... ' + error_msg) + + return