23 #include <QCoreApplication>
34 #define TYMPAN_REL_CYTHON_PATH "cython_d"
36 #define TYMPAN_REL_CYTHON_PATH "cython"
39 #define COMPUTATION_TIMEOUT 10000
43 QStringList env(QProcess::systemEnvironment());
45 QString cythonlibs_path(QCoreApplication::applicationDirPath());
47 cythonlibs_path.append(
"/");
49 cythonlibs_path = QDir::toNativeSeparators(cythonlibs_path);
51 QRegExp pythonpath_regexp(
"^PYTHONPATH=(.*)", Qt::CaseInsensitive);
52 int pythonpath_index = env.indexOf(pythonpath_regexp);
54 if (pythonpath_index > 0)
56 pythonpath = env[pythonpath_index];
58 if (pythonpath !=
"PYTHONPATH=")
60 #if TY_PLATFORM == TY_PLATFORM_WIN32 || TY_PLATFORM == TY_PLATFORM_WIN64
61 pythonpath.append(
";");
63 pythonpath.append(
":");
66 pythonpath.append(cythonlibs_path);
67 env.removeAt(pythonpath_index);
71 pythonpath =
"PYTHONPATH=";
72 pythonpath.append(cythonlibs_path);
74 env.append(pythonpath);
75 #if TY_PLATFORM == TY_PLATFORM_WIN32 || TY_PLATFORM == TY_PLATFORM_WIN64
77 QRegExp path_regexp(
"^Path=(.*)", Qt::CaseInsensitive);
78 int path_index = env.indexOf(path_regexp);
79 QString path = env[path_index];
80 QRegExp equal_regexp(
"=", Qt::CaseInsensitive);
81 int equal_index = path.indexOf(equal_regexp) + 1;
82 QString application_path(QCoreApplication::applicationDirPath());
83 application_path = QDir::toNativeSeparators(application_path);
84 path.insert(equal_index, application_path +
";");
85 env.removeAt(path_index);
93 #if TY_PLATFORM == TY_PLATFORM_WIN32 || TY_PLATFORM == TY_PLATFORM_WIN64
94 QStringList env(QProcess::systemEnvironment());
97 int python_interp_idx = env.indexOf(QRegExp(
"^TYMPAN_PYTHON_INTERP=(.*)"));
98 if (python_interp_idx < 0)
101 "Can't access python interpreter. TYMPAN_PYTHON_INTERP environment variable is not set.");
103 QString python_interp_path = env.at(python_interp_idx).split(
'=')[1].remove(
"\"");
104 QFile python_interp(python_interp_path);
105 if (!python_interp.exists())
108 "variable is not correctly set.");
110 return python_interp_path;
112 return QString(
"python3");
118 std::string variables =
"\nVariables d'environnement:\n";
119 int pythonpath_index = env.indexOf(QRegExp(
"^PYTHONPATH=(.*)", Qt::CaseInsensitive));
120 if (pythonpath_index >= 0)
122 variables += env[pythonpath_index].toStdString() +
"\n";
126 variables +=
"PYTHONPATH absente\n";
128 #if TY_PLATFORM == TY_PLATFORM_WIN32 || TY_PLATFORM == TY_PLATFORM_WIN64
129 int path_index = env.indexOf(QRegExp(
"^Path=(.*)", Qt::CaseInsensitive));
132 variables += env[path_index].toStdString() +
"\n";
136 variables +=
"Path absente\n";
138 int python_interp_index = env.indexOf(QRegExp(
"^TYMPAN_PYTHON_INTERP=(.*)"));
139 if (python_interp_index >= 0)
141 variables += env[python_interp_index].toStdString() +
"\n";
145 variables +=
"TYMPAN_PYTHON_INTERP absente\n";
153 QStringList appli_env(QProcess::systemEnvironment());
154 int tympan_debug_idx = appli_env.indexOf(QRegExp(
"^TYMPAN_DEBUG=(.*)"));
155 if (tympan_debug_idx >= 0)
157 QString debug_option = appli_env[tympan_debug_idx].split(
'=')[1];
158 if (debug_option.contains(
"keep_tmp_files", Qt::CaseInsensitive))
168 if (!tmp_file.open())
175 tmp_file.setAutoRemove(
false);
179 bool python(QStringList args, std::string& error_msg)
184 logger.
debug(
"Lancement du script python: %s", args.join(
" ").toStdString().c_str());
186 float comp_duration(0.);
187 bool comp_finished(
false);
190 python.setEnvironment(env);
193 QString python_interp;
200 error_msg =
"L'interpreteur python n'a pas pu etre trouve.\nVeuillez verifier que la variable "
201 "d'environnement TYMPAN_PYTHON_INTERP est correctement positionnee\n";
205 python.start(python_interp, args);
213 if (
python.error() == QProcess::Timedout)
217 logger.
info(
"Le script python s'execute encore apres %.3f secondes", comp_duration);
225 }
while (!comp_finished);
227 QString std_error(
python.readAllStandardError());
228 int exit_code =
python.exitCode();
229 if (
python.exitStatus() != QProcess::NormalExit || exit_code != 0)
231 error_msg =
"Le sous-process python s'est terminé avec le code d'erreur ";
232 error_msg.append(std::to_string(
static_cast<long long>(exit_code)));
233 error_msg.append(
"\n");
234 error_msg.append(std_error.toStdString());
236 error_msg.append(
"Veuillez lire tympan.log pour plus d'information.\n");
237 logger.
error(error_msg.c_str());
242 logger.
info(
"Le sous-processus Python s'est terminé correctement");
243 if (!std_error.isEmpty())
244 logger.
warning(std_error.toStdString().c_str());
250 unsigned long second = duration.
getTime() / 1000;
251 unsigned long millisecond = duration.
getTime() - second * 1000;
252 logger.
info(
"Temps de calcul : %02ld,%03ld sec. (%ld msec.)", second, millisecond, duration.
getTime());
unsigned long getTime() const
virtual void debug(const char *message,...)
virtual void warning(const char *message,...)
virtual void error(const char *message,...)
static OMessageManager * get()
virtual void info(const char *message,...)
Utilities to handle exceptions and to pretty-print value.
The base exception class for errors due to invalid data.
bool python(QStringList args, std::string &error_msg)
bool must_keep_tmp_files()
bool init_tmp_file(QTemporaryFile &tmp_file, bool keep_file)
QString _get_python_interp()
std::string _read_environment_variables(QStringList env)
QStringList _python_qprocess_environment()
#define TYMPAN_REL_CYTHON_PATH
#define COMPUTATION_TIMEOUT
Utilitaires pour les interactions entre l'application tympan et des sous- processus python.