-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtps_pythoninterpreter.cpp
More file actions
196 lines (166 loc) · 4.25 KB
/
gtps_pythoninterpreter.cpp
File metadata and controls
196 lines (166 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* GTlab - Gas Turbine laboratory
* Source File: gtps_pythoninterpreter.cpp
*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: 2024 German Aerospace Center (DLR)
*
* Created on: 20.10.2022
* Author: Marvin Noethen (DLR AT-TWK)
*/
#include "gtps_pythoninterpreter.h"
#include "gt_logging.h"
#include "find_libpython.h"
#include "gtps_systemsettings.h"
#include "gtps_globals.h"
#include <QProcess>
#include <QDir>
#include <QFileInfo>
#include <QTemporaryFile>
GtpsPythonInterpreter::GtpsPythonInterpreter(const QString& pythonExe) :
m_pythonExe{pythonExe}
{
if (isValid())
{
setPythonVersion();
setSharedLibPath();
setSysPaths();
}
}
QString
GtpsPythonInterpreter::runCommand(const QString& pythonCommand, bool* ok) const
{
return runPythonInterpreter(QStringList() << "-c" << pythonCommand, ok);
}
QString
GtpsPythonInterpreter::runScript(const QFile& scriptFile, bool* ok) const
{
// Workaround, since QTemporaryFile().exists() is always false
if (!QFile(scriptFile.fileName()).exists())
{
if (ok != nullptr)
{
*ok = false;
}
return {};
}
return runPythonInterpreter({scriptFile.fileName()}, ok);
}
QString
GtpsPythonInterpreter::runPythonInterpreter(const QStringList& args,
bool* ok) const
{
// temporarily clear the PYTHONPATH and PYTHONHOME variables to
// ensure that the Python executable can be executed without
// configuration problems
QByteArray pySysPaths = gtps::system::pythonPath();
QByteArray pyHome = gtps::system::pythonHome();
gtps::system::clearPythonVars();
// run Python in a separate process
QProcess process;
process.start(m_pythonExe, args);
QByteArray retval;
bool success{process.waitForFinished()};
if (success && process.exitCode() == 0)
{
retval = process.readAll();
}
else
{
retval = process.readAllStandardError();
success = false;
}
if (ok)
{
*ok = success;
}
// set PYTHONPATH and PYTHONHOME variables again
gtps::system::setPythonPath(pySysPaths);
gtps::system::setPythonHome(pyHome);
return {retval};
}
GtpsPythonInterpreter::Status
GtpsPythonInterpreter::status() const
{
return m_status;
}
bool
GtpsPythonInterpreter::isValid()
{
bool ok{false};
runCommand("import sys", &ok);
m_status = ok ? Status::Valid : Status::Invalid;
return ok;
}
const GtVersionNumber&
GtpsPythonInterpreter::version() const
{
return m_pyVersion;
}
const QString&
GtpsPythonInterpreter::sharedLib() const
{
return m_sharedLib;
}
const QStringList&
GtpsPythonInterpreter::sysPaths() const
{
return m_sysPaths;
}
QString
GtpsPythonInterpreter::pythonHomePath() const
{
return QDir::toNativeSeparators(QFileInfo(m_pythonExe).absolutePath());
}
void
GtpsPythonInterpreter::setPythonVersion()
{
QString pyCode{"import sys;"
"sys.stdout.write('%s.%s.%s' % (sys.version_info.major, "
"sys.version_info.minor, sys.version_info.micro))"};
bool ok{false};
auto version = runCommand(pyCode, &ok);
if (ok)
{
m_pyVersion = GtVersionNumber{version};
if (!gtps::python::version::isSupported(m_pyVersion))
{
m_status = Status::NotSupported;
}
}
}
std::unique_ptr<QTemporaryFile>
getFindLibPythonScriptFile()
{
auto file = std::make_unique<QTemporaryFile>();
if (file->open())
{
file->write(findPythonLibCode);
file->flush();
}
return file;
}
void
GtpsPythonInterpreter::setSharedLibPath()
{
bool ok{false};
auto sharedLib = runScript(*getFindLibPythonScriptFile(), &ok);
if (ok)
{
m_sharedLib = sharedLib.replace("\\\\", "\\");
}
}
void
GtpsPythonInterpreter::setSysPaths()
{
QString pyCode{"import sys;print(', '.join([x for x in sys.path if x]), "
"end='')"};
bool ok{false};
auto pyPaths = runCommand(pyCode, &ok);
if (ok)
{
m_sysPaths = pyPaths.split(", ");
auto toNativeSep = [](QString& pyPath){
pyPath = QDir::toNativeSeparators(pyPath);};
std::for_each(m_sysPaths.begin(), m_sysPaths.end(), toNativeSep);
}
}