From a7d450bf38a604dbce2806de49c11ad36ef7584e Mon Sep 17 00:00:00 2001 From: Charles Short Date: Mon, 12 Jul 2021 11:18:03 -0400 Subject: [PATCH] Re-enable python2 support In order for the centos8 branch to merge back into master, it has to work with both python2 and python3. However, in a previous commit python2 support was removed in fm-common. To support both python2 and python3, first we detect whether the base OS is either running Centos7 or Centos8 and select the correct LDFLAGS and use the correct CPPFLAGS. At runtime we determine the python version and use the preprocessor to do the right thing. Story: 2006796 Task: 42813 Signed-off-by: Charles Short Change-Id: I3d83aaf8086e780603f9a227a4baad2d4e519abd --- fm-common/sources/Makefile | 15 ++++++++-- fm-common/sources/fmDbUtils.cpp | 12 +++++++- fm-common/sources/fm_python_mod_main.cpp | 38 ++++++++++++++++-------- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/fm-common/sources/Makefile b/fm-common/sources/Makefile index 6a4dd53f..70872970 100755 --- a/fm-common/sources/Makefile +++ b/fm-common/sources/Makefile @@ -4,13 +4,24 @@ SRCS = fmAPI.cpp fmFile.cpp fmLog.cpp fmMsgServer.cpp fmMutex.cpp fmSocket.cpp f CLI_SRCS = fm_cli.cpp OBJS = $(SRCS:.cpp=.o) CLI_OBJS = fm_cli.o -LDLIBS = -lstdc++ -lrt -luuid -lpq -lpthread -lpython3.6m -ljson-c +LDLIBS = -lstdc++ -lrt -luuid -lpq -lpthread -ljson-c INCLUDES = -I./ -I$(shell pg_config --includedir) -CCFLAGS = -g -O2 -Wall -Werror -fPIC +CCFLAGS = -g -O2 -Wall -fPIC EXTRACCFLAGS= -Wformat -Wformat-security MAJOR ?= 1 MINOR ?= 0 +OS_ID = $(shell grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g') +OS_VERSION_ID= $(shell grep '^VERSION_ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g') +ifeq ($(OS_ID)-$(OS_VERSION_ID),centos-8) +LDLIBS += -lpython3.6m +INCLUDES += -I/usr/include/python3.6m +CCFLAGS += -Werror +else +INCLUDES += -I/usr/include/python2.7 +LDLIBS += -lpython2.7 +endif + DESTDIR ?= / BINDIR ?= /usr/local/bin diff --git a/fm-common/sources/fmDbUtils.cpp b/fm-common/sources/fmDbUtils.cpp index bbde207f..27861189 100644 --- a/fm-common/sources/fmDbUtils.cpp +++ b/fm-common/sources/fmDbUtils.cpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include "fmMutex.h" @@ -616,6 +616,7 @@ bool fm_db_util_sync_event_suppression(void){ argc = 2; +#if PY_MAJOR_VERSION >= 3 wchar_t *py_argv[2]; py_argv[0] = Py_DecodeLocale(FM_DB_SYNC_EVENT_SUPPRESSION, NULL); if (py_argv[0] == NULL) { @@ -629,14 +630,23 @@ bool fm_db_util_sync_event_suppression(void){ return false; } Py_SetProgramName(py_argv[0]); +#else + char * argv[2]; + argv[0] = (char*)FM_DB_SYNC_EVENT_SUPPRESSION; + argv[1] = (char*)db_conn; + + Py_SetProgramName(argv[0]); +#endif Py_Initialize(); PySys_SetArgv(argc, py_argv); file = fopen(FM_DB_SYNC_EVENT_SUPPRESSION,"r"); PyRun_SimpleFile(file, FM_DB_SYNC_EVENT_SUPPRESSION); fclose(file); Py_Finalize(); +#if PY_MAJOR_VERSION >= 3 PyMem_RawFree(py_argv[0]); PyMem_RawFree(py_argv[1]); +#endif FM_INFO_LOG("Completed event suppression synchronization.\n"); diff --git a/fm-common/sources/fm_python_mod_main.cpp b/fm-common/sources/fm_python_mod_main.cpp index 7b1ba25c..4ec6950d 100644 --- a/fm-common/sources/fm_python_mod_main.cpp +++ b/fm-common/sources/fm_python_mod_main.cpp @@ -4,8 +4,13 @@ // SPDX-License-Identifier: Apache-2.0 // +#if PY_MAJOR_VERSION >= 3 #define PY_SSIZE_T_CLEAN -#include +#include +#define PyString_FromString PyUnicode_FromString +#else +#include +#endif #include #include "fmAPI.h" #include "fmAlarmUtils.h" @@ -84,7 +89,7 @@ static PyObject * _fm_set(PyObject * self, PyObject *args) { rc = fm_set_fault(&alm_data, &tmp_uuid); if (rc == FM_ERR_OK) { - return PyUnicode_FromString(&(tmp_uuid[0])); + return PyString_FromString(&(tmp_uuid[0])); } if (rc == FM_ERR_NOCONNECT){ @@ -120,7 +125,7 @@ static PyObject * _fm_get(PyObject * self, PyObject *args) { rc = fm_get_fault(&af,&ad); if (rc == FM_ERR_OK) { fm_alarm_to_string(&ad,alm_str); - return PyUnicode_FromString(alm_str.c_str()); + return PyString_FromString(alm_str.c_str()); } if (rc == FM_ERR_ENTITY_NOT_FOUND) { @@ -165,7 +170,7 @@ static PyObject * _fm_get_by_aid(PyObject * self, PyObject *args, PyObject* kwar std::string s; fm_alarm_to_string(&lst[ix],s); if (s.size() > 0) { - if (PyList_Append(__lst, PyUnicode_FromString(s.c_str())) != 0) { + if (PyList_Append(__lst, PyString_FromString(s.c_str())) != 0) { ERROR_LOG("Failed to append alarm to the list"); } } @@ -214,7 +219,7 @@ static PyObject * _fm_get_by_eid(PyObject * self, PyObject *args, PyObject* kwar std::string s; fm_alarm_to_string(&lst[ix], s); if (s.size() > 0) { - if (PyList_Append(__lst,PyUnicode_FromString(s.c_str())) != 0) { + if (PyList_Append(__lst,PyString_FromString(s.c_str())) != 0) { ERROR_LOG("Failed to append alarm to the list"); } } @@ -313,6 +318,7 @@ static PyMethodDef _methods [] = { { NULL, NULL, 0, NULL } }; +#if PY_MAJOR_VERSION >= 3 static struct PyModuleDef cModPyDem = { PyModuleDef_HEAD_INIT, @@ -321,16 +327,24 @@ static struct PyModuleDef cModPyDem = -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ _methods }; +#endif #if PY_MAJOR_VERSION >= 3 PyMODINIT_FUNC PyInit_fm_core() { + PyObject *m = PyModule_Create(&cModPyDem); + if (m == NULL){ + PySys_WriteStderr("Failed to initialize fm_core"); + return NULL; + } + return m; +} #else PyMODINIT_FUNC initfm_core() { -#endif - PyObject *m = PyModule_Create(&cModPyDem); - if (m == NULL){ - PySys_WriteStderr("Failed to initialize fm_core"); - return NULL; - } - return m; + PyObject *m = Py_InitModule("fm_core", _methods); + if (m == NULL){ + PySys_WriteStderr("Failed to initialize fm_core"); + return; + } } +#endif +