Skip to content
Snippets Groups Projects
Commit bf48c8a5 authored by Anne Philipp's avatar Anne Philipp
Browse files

introduced a function for subprocess check_call to do error handling once

parent 524ac32c
No related branches found
No related tags found
No related merge requests found
......@@ -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,',
execute_subprocess(['grib_set', '-s', 'edition=2,' +
'productDefinitionTemplateNumber=8',
ofile, ofile + '_2'])
p = subprocess.check_call(['mv', ofile + '_2', ofile])
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',
execute_subprocess(['ectrans', '-overwrite', '-gateway',
c.gateway, '-remote', c.destination,
'-source', ofile])
'-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
......@@ -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
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment