Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

385 terminate signals #387

Merged
merged 5 commits into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
set_cur_dataset_attribute, get_cur_dataset_attribute,
has_cur_dataset_attribute, set_variable_attribute, get_variable_attribute,
has_variable_attribute, get_final_filename, set_deflate, set_furtherinfourl,
set_climatology, get_climatology)
set_climatology, get_climatology, set_terminate_signal, get_terminate_signal)

try:
from check_CMOR_compliant import checkCMOR
Expand Down
10 changes: 10 additions & 0 deletions Lib/pywrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
except BaseException:
has_oldma = False

def get_terminate_signal():
""" Return the integer code currently used by CMOR for system signal when issuing an error
-999 means not set yet, once cmor_setup is called -999 is turned to SIGTERM"""
return _cmor.get_terminate_signal()

def set_terminate_signal(signal):
"""Sets the signal code to be used by CMOR when issuing an error
int has to match whateve rint the C uses"""
_cmor.set_terminate_signal(signal)


def time_varying_grid_coordinate(
grid_id, table_entry, units, type='f', missing_value=None):
Expand Down
25 changes: 25 additions & 0 deletions Src/_cmormodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,29 @@ static PyObject *PyCMOR_dataset_json(PyObject * self, PyObject * args)
return (Py_BuildValue("i", ierr));
}

/************************************************************************/
/* PyCMOR_get_terminate_signal() */
/************************************************************************/

static PyObject *PyCMOR_get_terminate_signal(PyObject * self, PyObject * args)
{
return Py_BuildValue("i", cmor_get_terminate_signal());
}
/************************************************************************/
/* PyCMOR_set_terminate_signal() */
/************************************************************************/

static PyObject *PyCMOR_set_terminate_signal(PyObject * self, PyObject * args)
{
int signal;
if (!PyArg_ParseTuple(args, "i", &signal))
return NULL;

cmor_set_terminate_signal(signal);

Py_INCREF(Py_None);
return (Py_None);
}
/************************************************************************/
/* PyCMOR_load_table() */
/************************************************************************/
Expand Down Expand Up @@ -1064,6 +1087,8 @@ static PyMethodDef MyExtractMethods[] = {
{"set_furtherinfourl", PyCMOR_set_furtherinfourl, METH_VARARGS},
{"get_final_filename", PyCMOR_getFinalFilename, METH_VARARGS},
{"set_deflate", PyCMOR_set_deflate, METH_VARARGS},
{"set_terminate_signal", PyCMOR_set_terminate_signal, METH_VARARGS},
{"get_terminate_signal", PyCMOR_get_terminate_signal, METH_VARARGS},
{NULL, NULL} /*sentinel */
};

Expand Down
33 changes: 29 additions & 4 deletions Src/cmor.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const char CMOR_VALID_CALENDARS[CMOR_N_VALID_CALS][CMOR_MAX_STRING] =
};

int CMOR_HAS_BEEN_SETUP = 0;
int CMOR_TERMINATE_SIGNAL = -999; /* not set by default */
int CV_ERROR = 0;
ut_system *ut_read = NULL;
FILE *output_logfile;
Expand Down Expand Up @@ -111,12 +112,27 @@ int bAppendMode = FALSE;

volatile sig_atomic_t stop = 0;

/**************************************************************************/
/* reset signal code
/**************************************************************************/
int cmor_get_terminate_signal() {
return CMOR_TERMINATE_SIGNAL;
}
void cmor_set_terminate_signal_to_sigint() {
CMOR_TERMINATE_SIGNAL = SIGINT;
}
void cmor_set_terminate_signal_to_sigterm() {
CMOR_TERMINATE_SIGNAL = SIGTERM;
}
void cmor_set_terminate_signal( int code) {
CMOR_TERMINATE_SIGNAL = code;
}
/**************************************************************************/
/* cmor_mkdir() */
/**************************************************************************/
void terminate(int signal)
{
if (signal == SIGTERM) {
if (signal == CMOR_TERMINATE_SIGNAL) {
stop = 1;
}
}
Expand Down Expand Up @@ -624,7 +640,7 @@ void cmor_handle_error(char error_msg[CMOR_MAX_STRING], int level)
if ((CMOR_MODE == CMOR_EXIT_ON_WARNING) || (level == CMOR_CRITICAL)) {
fflush(stdout);
fflush(output_logfile);
kill(getpid(), SIGINT);
kill(getpid(), SIGTERM);
}
fflush(output_logfile);
}
Expand Down Expand Up @@ -756,11 +772,20 @@ int cmor_setup(char *path,
struct tm *ptr;
extern FILE *output_logfile;
extern int did_history;

struct sigaction action;


/*********************************************************/
/* set the default signal */
/*********************************************************/

if (CMOR_TERMINATE_SIGNAL == -999) {
CMOR_TERMINATE_SIGNAL = SIGTERM;
}

memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = terminate;
sigaction(SIGTERM, &action, NULL);
sigaction(CMOR_TERMINATE_SIGNAL, &action, NULL);

strcpy(cmor_traceback_info, "");
cmor_add_traceback("cmor_setup");
Expand Down
20 changes: 20 additions & 0 deletions Src/cmor_cfortran_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,29 @@ void cmor_handle_error_cff_(char error_msg[CMOR_MAX_STRING], int *level)
cmor_handle_error(error_msg, *level);
}

/************************************************************************/
/* cmor_get_terminate_signal_cff_() */
/************************************************************************/
int cmor_get_terminate_signal_cff_() {
return cmor_get_terminate_signal();
}

/************************************************************************/
/* cmor_set_terminate_signal_cff() */
/************************************************************************/
void cmor_set_terminate_signal_cff_(int *signal) {
cmor_set_terminate_signal(*signal);
}

/************************************************************************/
/* cmor_setup_cff_nolog_() */
/************************************************************************/
int cmor_setup_cff_nolog_(char *path, int *netcdf, int *verbosity,
int *mode, int *crsub)
{
if (cmor_get_terminate_signal() == -999) {
cmor_set_terminate_signal_to_sigint();
};
return (cmor_setup(path, netcdf, verbosity, mode, NULL, crsub));
}

Expand All @@ -388,6 +405,9 @@ int cmor_setup_cff_nolog_(char *path, int *netcdf, int *verbosity,
int cmor_setup_cff_(char *path, int *netcdf, int *verbosity, int *mode,
char *logfile, int *crsub)
{
if (cmor_get_terminate_signal() == -999) {
cmor_set_terminate_signal_to_sigint();
};
return (cmor_setup(path, netcdf, verbosity, mode, logfile, crsub));
}

Expand Down
20 changes: 20 additions & 0 deletions Src/cmor_fortran_interface.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
module cmor_users_functions
interface
function cmor_get_terminate_signal_cff() result (signal)
integer signal
end function
end interface
interface
subroutine cmor_set_terminate_signal_cff(signal)
integer signal
end subroutine
end interface
interface
function cmor_set_cur_dset_attribute_cff(name, value, optional) result (ierr)
character(*) name
Expand Down Expand Up @@ -6555,6 +6565,16 @@ end function cmor_zfactor_int_0dvalues
!!$ end if
!!$ end function cmor_zfactor_long_0dvalues

subroutine cmor_set_terminate_signal(signal)
integer signal
call cmor_set_terminate_signal_cff(signal)
end subroutine cmor_set_terminate_signal

function cmor_get_terminate_signal() result(signal)
integer signal
signal = cmor_get_terminate_signal_cff()
end function

subroutine cmor_set_table(table_id)
integer table_id
call cmor_set_table_cff(table_id)
Expand Down
28 changes: 9 additions & 19 deletions Test/base_CMIP6_CV.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,32 @@
from __future__ import print_function
import cmor
import unittest
import sys
import os
import tempfile
import signal
import glob

debug = False

class BaseCVsTest(unittest.TestCase):


class BaseCVsTest(unittest.TestCase):
def remove_file_and_directories(self, filename):
os.remove(filename)
filename = os.path.dirname(filename)
while glob.glob(os.path.join(filename, "*")) == []:
os.rmdir(filename)
filename = os.path.dirname(filename)

def signal_handler(self, sig, frame):
if debug: print("Code received SIGINT")
return

def setUp(self, *args, **kwargs):

# --------------
# Create sigint handler
# --------------
signal.signal(signal.SIGINT, self.signal_handler)
# --------------
# Create tmpfile
# --------------
self.tmpfile = tempfile.mkstemp()[1]
if debug: print("TEMP:",self.tmpfile)
if debug:
print("TEMP:", self.tmpfile)
self.delete_files = []

def tearDown(self):
if debug:
print("would be unlinking:",self.tmpfile)
if debug:
print("would be unlinking:", self.tmpfile)
return
os.unlink(self.tmpfile)
for filename in self.delete_files:
Expand All @@ -47,7 +35,8 @@ def tearDown(self):

def assertCV(self, text_to_find, line_trigger='Error:', number_of_lines_to_scan=1):
line_to_scan = ""
if debug: print("LINE TRIGGER:", line_trigger)
if debug:
print("LINE TRIGGER:", line_trigger)
with open(self.tmpfile) as f:
lines = f.readlines()
for i, line in enumerate(lines):
Expand All @@ -56,5 +45,6 @@ def assertCV(self, text_to_find, line_trigger='Error:', number_of_lines_to_scan=
if line_trigger in line:
line_to_scan = scan
break
if debug: print("SCANNED:",line_to_scan)
if debug:
print("SCANNED:", line_to_scan)
self.assertIn(text_to_find, line_to_scan)
48 changes: 22 additions & 26 deletions Test/test_python_CMIP6_CV_HISTORY.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,31 @@ class TestCase(base_CMIP6_CV.BaseCVsTest):

def testCMIP6(self):
''' '''
try:
# -------------------------------------------
# Try to call cmor with a bad institution_ID
# -------------------------------------------
cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile)
cmor.dataset_json("Test/common_user_input.json")
cmor.load_table("CMIP6_Omon.json")

# -------------------------------------------
# Try to call cmor with a bad institution_ID
# -------------------------------------------
cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile)
cmor.dataset_json("Test/common_user_input.json")
cmor.load_table("CMIP6_Omon.json")
cmor.set_cur_dataset_attribute("history", "set for CMIP6 unittest")

cmor.set_cur_dataset_attribute("history", "set for CMIP6 unittest")
# ------------------------------------------
# load Omon table and create masso variable
# ------------------------------------------
cmor.load_table("CMIP6_Omon.json")
itime = cmor.axis(table_entry="time", units='months since 2010',
coord_vals=numpy.array([0, 1, 2, 3, 4.]),
cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.]))
ivar = cmor.variable(table_entry="masso", axis_ids=[itime], units='kg')

# ------------------------------------------
# load Omon table and create masso variable
# ------------------------------------------
cmor.load_table("CMIP6_Omon.json")
itime = cmor.axis(table_entry="time", units='months since 2010',
coord_vals=numpy.array([0, 1, 2, 3, 4.]),
cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.]))
ivar = cmor.variable(table_entry="masso", axis_ids=[itime], units='kg')

data = numpy.random.random(5)
for i in range(0, 5):
cmor.write(ivar, data[i:i])
self.delete_files += [cmor.close(ivar, True)]
f = cdms2.open(cmor.get_final_filename(), "r")
a = f.getglobal("history")
self.assertIn("set for CMIP6 unittest", a)
except KeyboardInterrupt:
raise RuntimeError("Unexpected Error")
data = numpy.random.random(5)
for i in range(0, 5):
cmor.write(ivar, data[i:i])
self.delete_files += [cmor.close(ivar, True)]
f = cdms2.open(cmor.get_final_filename(), "r")
a = f.getglobal("history")
self.assertIn("set for CMIP6 unittest", a)



Expand Down
9 changes: 2 additions & 7 deletions Test/test_python_CMIP6_CV_bad_data_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,11 @@ def testCMIP6(self):
data = numpy.random.random(75)
reshaped_data = data.reshape((5, 5, 3, 1))

# This works:
cmor.write(ua_var_id, reshaped_data)

# This doesn't:
#cmor.write(ta_var_id, reshaped_data)
#cmor.write(ua_var_id, reshaped_data)
cmor.write(ta_var_id, reshaped_data)
cmor.write(ua_var_id, reshaped_data)

#cmor.close()
except KeyboardInterrupt:
raise RuntimeError("Unexpected error")
except BaseException:
pass

Expand Down
2 changes: 0 additions & 2 deletions Test/test_python_CMIP6_CV_baddirectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def test_Directory(self):
cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile)
try:
cmor.dataset_json("Test/baddirectory.json")
except KeyboardInterrupt:
raise RuntimeError("Unexpected Error")
except BaseException:
pass
self.assertCV("unable to create this directory")
Expand Down
2 changes: 0 additions & 2 deletions Test/test_python_CMIP6_CV_badgridgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ def testCMIP6(self):
cmor.write(ivar, data[i:i])
self.delete_files += [cmor.close(ivar, True)]
cmor.close()
except KeyboardInterrupt:
raise RuntimeError("Unexpected Error")
except BaseException:
pass
# ------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions Test/test_python_CMIP6_CV_badgridlabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ def testCMIP6(self):
cmor.write(ivar, data[i:i])
self.delete_files += [cmor.close(ivar, True)]
cmor.close()
except KeyboardInterrupt:
raise RuntimeError("Unexpected Error")
except BaseException:
pass
# ------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions Test/test_python_CMIP6_CV_badgridresolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ def testCMIP6(self):
cmor.write(ivar, data[i:i])
self.delete_files += [cmor.close(ivar, file_name=True)]
cmor.close()
except KeyboardInterrupt:
raise RuntimeError("Unexpected Error")
except BaseException:
pass
self.assertCV("\"335 km\"", "The current input")
Expand Down
2 changes: 0 additions & 2 deletions Test/test_python_CMIP6_CV_badinstitution.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ def testCMIP6(self):
cmor.write(ivar, data[i:i])
self.delete_files += [cmor.close(ivar, True)]
cmor.close()
except KeyboardInterrupt:
raise RuntimeError("Unexpected Error")
except BaseException:
pass
self.assertCV('NCC2" will be replaced with', 'Your input attribute')
Expand Down
Loading