Salome HOME
Python is no longer initialized by YACSEvalSession.
[modules/yacs.git] / src / evalyfx / YACSEvalSession.cxx
1 // Copyright (C) 2012-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (EDF R&D)
20
21 #include "YACSEvalSession.hxx"
22 #include "YACSEvalSessionInternal.hxx"
23
24 #include "AutoGIL.hxx"
25 #include "Exception.hxx"
26
27 #include <Python.h>
28
29 #include <sstream>
30
31 const char YACSEvalSession::KERNEL_ROOT_DIR[]="KERNEL_ROOT_DIR";
32
33 const char YACSEvalSession::CORBA_CONFIG_ENV_VAR_NAME[]="OMNIORB_CONFIG";
34
35 const char YACSEvalSession::NSPORT_VAR_NAME[]="NSPORT";
36
37 YACSEvalSession::YACSEvalSession():_isAttached(false),_isLaunched(false),_isForcedPyThreadSaved(false),_port(-1),_salomeInstanceModule(0),_salomeInstance(0),_internal(new YACSEvalSessionInternal)
38 {
39   YACS::ENGINE::AutoGIL gal;
40   _salomeInstanceModule=PyImport_ImportModule(const_cast<char *>("salome_instance"));
41 }
42
43 YACSEvalSession::~YACSEvalSession()
44 {
45   delete _internal;
46   YACS::ENGINE::AutoGIL gal;
47   if(isLaunched() && !isAttached())
48     {
49       YACS::ENGINE::AutoPyRef terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("stop")));//new
50       YACS::ENGINE::AutoPyRef res(PyObject_CallObject(terminateSession,0));
51     }
52   Py_XDECREF(_salomeInstance);
53   Py_XDECREF(_salomeInstanceModule);
54 }
55
56 void YACSEvalSession::launch()
57 {
58   if(isLaunched())
59     return ;
60   YACS::ENGINE::AutoGIL gal;
61   PyObject *salomeInstance(PyObject_GetAttrString(_salomeInstanceModule,const_cast<char *>("SalomeInstance")));//new
62   PyObject *startMeth(PyObject_GetAttrString(salomeInstance,const_cast<char *>("start")));
63   Py_XDECREF(salomeInstance);
64   YACS::ENGINE::AutoPyRef myArgs(PyTuple_New(0));//new
65   YACS::ENGINE::AutoPyRef myKWArgs(PyDict_New());//new
66   PyDict_SetItemString(myKWArgs,"shutdown_servers",Py_True);//Py_True ref not stolen
67   _salomeInstance=PyObject_Call(startMeth,myArgs,myKWArgs);//new
68   YACS::ENGINE::AutoPyRef getPortMeth(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("get_port")));//new
69   YACS::ENGINE::AutoPyRef portPy(PyObject_CallObject(getPortMeth,0));//new
70   _port=PyInt_AsLong(portPy);
71   //
72   int dummy;
73   _corbaConfigFileName=GetConfigAndPort(dummy);
74   _isAttached=false; _isLaunched=true;
75 }
76
77 void YACSEvalSession::launchUsingCurrentSession()
78 {
79   if(isLaunched())
80     return ;
81   YACS::ENGINE::AutoGIL gal;
82   _corbaConfigFileName=GetConfigAndPort(_port);
83   _isAttached=true; _isLaunched=true;
84 }
85
86 bool YACSEvalSession::isAlreadyPyThreadSaved() const
87 {
88   return _isForcedPyThreadSaved;
89 }
90
91 void YACSEvalSession::checkLaunched() const
92 {
93   if(!isLaunched())
94     throw YACS::Exception("YACSEvalSession::checkLaunched : not launched !");
95 }
96
97 int YACSEvalSession::getPort() const
98 {
99   checkLaunched();
100   return _port;
101 }
102
103 std::string YACSEvalSession::getCorbaConfigFileName() const
104 {
105   checkLaunched();
106   return _corbaConfigFileName;
107 }
108
109 std::string YACSEvalSession::GetPathToAdd()
110 {
111   std::string ret;
112   YACS::ENGINE::AutoGIL gal;
113   YACS::ENGINE::AutoPyRef osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
114   PyObject *kernelRootDir(0);// os.environ["KERNEL_ROOT_DIR"]
115   {
116     YACS::ENGINE::AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
117     YACS::ENGINE::AutoPyRef kernelRootDirStr(PyString_FromString(const_cast<char *>(KERNEL_ROOT_DIR)));//new
118     kernelRootDir=PyObject_GetItem(environPy,kernelRootDirStr);//new
119   }
120   {
121     YACS::ENGINE::AutoPyRef pathPy(PyObject_GetAttrString(osPy,const_cast<char *>("path")));//new
122     YACS::ENGINE::AutoPyRef joinPy(PyObject_GetAttrString(pathPy,const_cast<char *>("join")));//new
123     YACS::ENGINE::AutoPyRef myArgs(PyTuple_New(4));
124     Py_XINCREF(kernelRootDir); PyTuple_SetItem(myArgs,0,kernelRootDir);
125     PyTuple_SetItem(myArgs,1,PyString_FromString(const_cast<char *>("bin")));
126     PyTuple_SetItem(myArgs,2,PyString_FromString(const_cast<char *>("salome")));
127     PyTuple_SetItem(myArgs,3,PyString_FromString(const_cast<char *>("appliskel")));
128     YACS::ENGINE::AutoPyRef res(PyObject_CallObject(joinPy,myArgs));
129     ret=PyString_AsString(res);
130   }
131   Py_XDECREF(kernelRootDir);
132   return ret;
133 }
134
135 std::string YACSEvalSession::GetConfigAndPort(int& port)
136 {
137   YACS::ENGINE::AutoPyRef osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
138   YACS::ENGINE::AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
139   //
140   YACS::ENGINE::AutoPyRef corbaConfigStr(PyString_FromString(const_cast<char *>(CORBA_CONFIG_ENV_VAR_NAME)));//new
141   YACS::ENGINE::AutoPyRef corbaConfigFileNamePy(PyObject_GetItem(environPy,corbaConfigStr));//new
142   std::string ret(PyString_AsString(corbaConfigFileNamePy));
143   //
144   YACS::ENGINE::AutoPyRef nsPortStr(PyString_FromString(const_cast<char *>(NSPORT_VAR_NAME)));//new
145   YACS::ENGINE::AutoPyRef nsPortValuePy(PyObject_GetItem(environPy,nsPortStr));//new
146   std::string portStr(PyString_AsString(nsPortValuePy));
147   std::istringstream iss(portStr);
148   iss >> port;
149   return ret;
150 }