CentOS 8: update fm-common to use python3

1. change python2 rpms to python3 version.
2. update Cpython code to use new API in python3

Test:
Could pass rpm package build for fm-common

Story: 2007065
Task: 37942

Change-Id: Ie0490118bcc891c2e994b3eeb6efab24ac7951cb
Signed-off-by: Shuicheng Lin <shuicheng.lin@intel.com>
(cherry picked from commit a2af71b867)
This commit is contained in:
Shuicheng Lin 2020-01-03 09:16:43 +08:00 committed by albailey
parent 53977f2898
commit 83f5caa9b7
4 changed files with 63 additions and 16 deletions

View File

@ -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 -lpython2.7 -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

View File

@ -15,7 +15,7 @@
#include <iostream>
#include <assert.h>
#include <arpa/inet.h>
#include <python2.7/Python.h>
#include <Python.h>
#include "fmMutex.h"
@ -611,15 +611,32 @@ bool fm_db_util_sync_event_suppression(void){
FILE* file;
int argc;
char * argv[2];
FM_INFO_LOG("Starting event suppression synchronization...\n");
argc = 2;
argv[0] = (char*)FM_DB_SYNC_EVENT_SUPPRESSION;
argv[1] = (char*)db_conn;
Py_SetProgramName(argv[0]);
#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) {
FM_ERROR_LOG("Fatal error: cannot decode argv[0]\n");
return false;
}
py_argv[1] = Py_DecodeLocale(db_conn, NULL);
if (py_argv[1] == NULL) {
PyMem_RawFree(py_argv[0]);
FM_ERROR_LOG("Fatal error: cannot decode argv[1]\n");
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();
#if PY_MAJOR_VERSION >= 3
PySys_SetArgv(argc, py_argv);
@ -630,6 +647,10 @@ bool fm_db_util_sync_event_suppression(void){
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");

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
# Copyright (c) 2016-2018 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0

View File

@ -4,8 +4,13 @@
// SPDX-License-Identifier: Apache-2.0
//
#include <python2.7/Python.h>
#if PY_MAJOR_VERSION >= 3
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#define PyString_FromString PyUnicode_FromString
#else
#include <Python.h>
#endif
#include <stdio.h>
#include "fmAPI.h"
#include "fmAlarmUtils.h"
@ -313,6 +318,7 @@ static PyMethodDef _methods [] = {
{ NULL, NULL, 0, NULL }
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef cModPyDem =
{
PyModuleDef_HEAD_INIT,
@ -321,15 +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;
}
PyObject *m = Py_InitModule("fm_core", _methods);
if (m == NULL){
PySys_WriteStderr("Failed to initialize fm_core");
return;
}
}
#endif