2013-06-21 17:21:11 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
2016-01-18 08:33:28 -06:00
|
|
|
// Copyright (C) 2013 - 2016, The pgAdmin Development Team
|
2013-06-21 17:21:11 -05:00
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
// Server.cpp - Thread in which the web server will run.
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "pgAdmin4.h"
|
|
|
|
|
|
|
|
// Must be before QT
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
// QT headers
|
2013-06-21 17:32:32 -05:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
2013-06-21 17:21:11 -05:00
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
// App headers
|
|
|
|
#include "Server.h"
|
|
|
|
|
2016-06-20 12:33:57 -05:00
|
|
|
static void add_to_path(QString &python_path, QString path, bool prepend=false)
|
2016-06-20 10:52:41 -05:00
|
|
|
{
|
|
|
|
if (!python_path.contains(path))
|
|
|
|
{
|
|
|
|
if (!prepend)
|
|
|
|
{
|
|
|
|
if (!python_path.isEmpty() && !python_path.endsWith(";"))
|
|
|
|
python_path.append(";");
|
|
|
|
|
|
|
|
python_path.append(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!python_path.isEmpty() && !python_path.startsWith(";"))
|
|
|
|
python_path.prepend(";");
|
|
|
|
|
|
|
|
python_path.prepend(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 11:12:10 -05:00
|
|
|
Server::Server(quint16 port)
|
2016-06-02 07:56:56 -05:00
|
|
|
{
|
2013-10-04 11:12:10 -05:00
|
|
|
// Appserver port
|
|
|
|
m_port = port;
|
2016-01-18 08:33:28 -06:00
|
|
|
m_wcAppName = NULL;
|
2013-10-04 11:12:10 -05:00
|
|
|
|
2013-06-21 17:21:11 -05:00
|
|
|
// Initialise Python
|
2016-01-18 08:33:28 -06:00
|
|
|
Py_NoSiteFlag=1;
|
2016-06-16 05:30:23 -05:00
|
|
|
Py_DontWriteBytecodeFlag=1;
|
2016-01-18 08:33:28 -06:00
|
|
|
|
|
|
|
// Python3 requires conversion of char * to wchar_t *, so...
|
|
|
|
#ifdef PYTHON2
|
2013-06-21 18:49:40 -05:00
|
|
|
Py_SetProgramName(PGA_APP_NAME.toUtf8().data());
|
2016-01-18 08:33:28 -06:00
|
|
|
#else
|
|
|
|
char *appName = PGA_APP_NAME.toUtf8().data();
|
|
|
|
const size_t cSize = strlen(appName)+1;
|
|
|
|
m_wcAppName = new wchar_t[cSize];
|
|
|
|
mbstowcs (m_wcAppName, appName, cSize);
|
|
|
|
Py_SetProgramName(m_wcAppName);
|
|
|
|
#endif
|
|
|
|
|
2014-12-16 06:53:09 -06:00
|
|
|
// Setup the search path
|
|
|
|
QSettings settings;
|
|
|
|
QString python_path = settings.value("PythonPath").toString();
|
|
|
|
|
2016-06-20 10:52:41 -05:00
|
|
|
// Get the application directory
|
|
|
|
QString app_dir = qApp->applicationDirPath();
|
2016-06-21 04:31:09 -05:00
|
|
|
QString path_env = qgetenv("PATH");;
|
2016-06-20 10:52:41 -05:00
|
|
|
|
|
|
|
#ifdef Q_OS_MAC
|
2016-06-02 07:56:56 -05:00
|
|
|
// In the case we're running in a release appbundle, we need to ensure the
|
|
|
|
// bundled virtual env is included in the Python path. We include it at the
|
|
|
|
// end, so expert users can override the path, but we do not save it, because
|
|
|
|
// if users move the app bundle, we'll end up with dead entries
|
|
|
|
|
|
|
|
// Build (and canonicalise) the virtual environment path
|
2016-06-20 10:52:41 -05:00
|
|
|
QFileInfo venvBinPath(app_dir + "/../Resources/venv/bin");
|
|
|
|
QFileInfo venvLibPath(app_dir + "/../Resources/venv/lib/python");
|
|
|
|
QFileInfo venvDynLibPath(app_dir + "/../Resources/venv/lib/python/lib-dynload");
|
|
|
|
QFileInfo venvSitePackagesPath(app_dir + "/../Resources/venv/lib/python/site-packages");
|
2016-06-02 07:56:56 -05:00
|
|
|
|
2016-06-20 10:52:41 -05:00
|
|
|
// Prepend the bin directory to the path
|
|
|
|
add_to_path(path_env, venvBinPath.canonicalFilePath(), true);
|
2016-06-02 07:56:56 -05:00
|
|
|
// Append the path, if it's not already there
|
2016-06-20 10:52:41 -05:00
|
|
|
add_to_path(python_path, venvLibPath.canonicalFilePath());
|
|
|
|
add_to_path(python_path, venvDynLibPath.canonicalFilePath());
|
|
|
|
add_to_path(python_path, venvSitePackagesPath.canonicalFilePath());
|
2016-06-21 05:15:08 -05:00
|
|
|
#elif defined(Q_OS_WIN)
|
2016-06-16 07:07:35 -05:00
|
|
|
|
|
|
|
// In the case we're running in a release application, we need to ensure the
|
|
|
|
// bundled virtual env is included in the Python path. We include it at the
|
|
|
|
// end, so expert users can override the path, but we do not save it.
|
|
|
|
|
|
|
|
// Build (and canonicalise) the virtual environment path
|
2016-06-21 07:08:15 -05:00
|
|
|
QFileInfo venvBinPath(app_dir + "/../venv");
|
|
|
|
QFileInfo venvLibPath(app_dir + "/../venv/Lib");
|
|
|
|
QFileInfo venvDLLsPath(app_dir + "/../venv/DLLs");
|
|
|
|
QFileInfo venvSitePackagesPath(app_dir + "/../venv/Lib/site-packages");
|
2016-06-16 07:07:35 -05:00
|
|
|
|
2016-06-20 10:52:41 -05:00
|
|
|
// Prepend the bin directory to the path
|
|
|
|
add_to_path(path_env, venvBinPath.canonicalFilePath(), true);
|
2016-06-16 10:21:55 -05:00
|
|
|
// Append paths, if they're not already there
|
2016-06-20 10:52:41 -05:00
|
|
|
add_to_path(python_path, venvLibPath.canonicalFilePath());
|
|
|
|
add_to_path(python_path, venvDLLsPath.canonicalFilePath());
|
|
|
|
add_to_path(python_path, venvSitePackagesPath.canonicalFilePath());
|
|
|
|
#else
|
|
|
|
// Build (and canonicalise) the virtual environment path
|
|
|
|
QFileInfo venvBinPath(app_dir + "/../venv/bin");
|
|
|
|
QFileInfo venvLibPath(app_dir + "/../venv/lib/python");
|
|
|
|
QFileInfo venvDynLibPath(app_dir + "/../venv/lib/python/lib-dynload");
|
|
|
|
QFileInfo venvSitePackagesPath(app_dir + "/../venv/lib/python/site-packages");
|
2016-06-16 10:21:55 -05:00
|
|
|
|
2016-06-20 10:52:41 -05:00
|
|
|
// Prepend the bin directory to the path
|
|
|
|
add_to_path(path_env, venvBinPath.canonicalFilePath(), true);
|
|
|
|
// Append the path, if it's not already there
|
|
|
|
add_to_path(python_path, venvLibPath.canonicalFilePath());
|
|
|
|
add_to_path(python_path, venvDynLibPath.canonicalFilePath());
|
|
|
|
add_to_path(python_path, venvSitePackagesPath.canonicalFilePath());
|
2016-06-02 07:56:56 -05:00
|
|
|
#endif
|
|
|
|
|
2016-06-21 04:31:09 -05:00
|
|
|
qputenv("PATH", path_env.toUtf8().data());
|
2016-06-20 10:52:41 -05:00
|
|
|
|
2014-12-16 06:53:09 -06:00
|
|
|
if (python_path.length() > 0)
|
|
|
|
{
|
2016-01-12 07:50:49 -06:00
|
|
|
// Split the path setting into individual entries
|
|
|
|
QStringList path_list = python_path.split(";", QString::SkipEmptyParts);
|
2016-06-21 04:31:09 -05:00
|
|
|
python_path = QString();
|
2016-01-12 07:50:49 -06:00
|
|
|
|
|
|
|
// Add new additional path elements
|
2016-01-18 08:33:28 -06:00
|
|
|
for (int i = path_list.size() - 1; i >= 0 ; --i)
|
|
|
|
{
|
2016-06-21 04:31:09 -05:00
|
|
|
python_path.append(path_list.at(i));
|
|
|
|
if (i > 0)
|
|
|
|
{
|
2016-06-21 05:15:08 -05:00
|
|
|
#if defined(Q_OS_WIN)
|
2016-06-21 04:31:09 -05:00
|
|
|
python_path.append(";");
|
2016-01-18 08:33:28 -06:00
|
|
|
#else
|
2016-06-21 04:31:09 -05:00
|
|
|
python_path.append(":");
|
2016-01-18 08:33:28 -06:00
|
|
|
#endif
|
2016-06-21 04:31:09 -05:00
|
|
|
}
|
2016-01-18 08:33:28 -06:00
|
|
|
}
|
2016-06-21 04:31:09 -05:00
|
|
|
qputenv("PYTHONPATH", python_path.toUtf8().data());
|
2014-12-16 06:53:09 -06:00
|
|
|
}
|
2016-06-16 10:21:55 -05:00
|
|
|
|
|
|
|
qDebug() << "Full Python path: " << python_path;
|
|
|
|
|
2016-06-02 07:56:56 -05:00
|
|
|
python_path = settings.value("PythonPath").toString();
|
2016-06-16 10:21:55 -05:00
|
|
|
qDebug() << "User Python path: " << python_path;
|
2016-06-21 04:31:09 -05:00
|
|
|
|
|
|
|
Py_Initialize();
|
2013-06-21 17:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Server::~Server()
|
|
|
|
{
|
2016-01-18 08:33:28 -06:00
|
|
|
if (m_wcAppName)
|
|
|
|
delete m_wcAppName;
|
|
|
|
|
2013-06-21 17:21:11 -05:00
|
|
|
// Shutdown Python
|
|
|
|
Py_Finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Server::Init()
|
|
|
|
{
|
2016-01-19 04:26:01 -06:00
|
|
|
QSettings settings;
|
|
|
|
|
2013-06-21 17:21:11 -05:00
|
|
|
// Find the webapp
|
|
|
|
QStringList paths;
|
2016-02-02 07:16:01 -06:00
|
|
|
paths.append("../web/"); // Linux source tree
|
|
|
|
paths.append("../../web/"); // Windows source tree
|
2016-05-10 10:38:28 -05:00
|
|
|
paths.append("../../../../web/"); // Mac source tree (in a dev env)
|
2016-06-21 07:08:15 -05:00
|
|
|
#ifdef Q_OS_MAC
|
2016-05-10 10:38:28 -05:00
|
|
|
paths.append("../Resources/web/"); // Mac source tree (in a release app bundle)
|
2016-06-02 07:56:56 -05:00
|
|
|
#endif
|
2016-01-19 04:26:01 -06:00
|
|
|
paths.append(settings.value("ApplicationPath").toString()); // System configured value
|
2013-06-21 17:21:11 -05:00
|
|
|
paths.append(""); // Should be last!
|
|
|
|
|
|
|
|
for (int i = 0; i < paths.size(); ++i)
|
|
|
|
{
|
|
|
|
QDir dir(QCoreApplication::applicationDirPath() + "/" + paths[i]);
|
|
|
|
m_appfile = dir.canonicalPath() + "/pgAdmin4.py";
|
|
|
|
|
|
|
|
if (QFile::exists(m_appfile))
|
|
|
|
{
|
|
|
|
qDebug() << "Webapp path: " << m_appfile;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!QFile::exists(m_appfile))
|
|
|
|
{
|
2013-10-04 12:16:31 -05:00
|
|
|
setError(tr("Failed to locate pgAdmin4.py, terminating server thread."));
|
2013-06-21 17:21:11 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Server::run()
|
|
|
|
{
|
|
|
|
// Open the application code and run it.
|
|
|
|
FILE *cp = fopen(m_appfile.toUtf8().data(), "r");
|
|
|
|
if (!cp)
|
|
|
|
{
|
2013-10-04 12:16:31 -05:00
|
|
|
setError(QString(tr("Failed to open the application file: %1, server thread exiting.")).arg(m_appfile));
|
2013-06-21 17:21:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-04 11:12:10 -05:00
|
|
|
// Set the port number
|
|
|
|
PyRun_SimpleString(QString("PGADMIN_PORT = %1").arg(m_port).toLatin1());
|
|
|
|
|
2016-06-02 07:56:56 -05:00
|
|
|
// Run the app!
|
2016-02-02 07:16:01 -06:00
|
|
|
#ifdef PYTHON2
|
|
|
|
PyObject* PyFileObject = PyFile_FromString(m_appfile.toUtf8().data(), (char *)"r");
|
|
|
|
if (PyRun_SimpleFile(PyFile_AsFile(PyFileObject), m_appfile.toUtf8().data()) != 0)
|
|
|
|
setError(tr("Failed to launch the application server, server thread exiting."));
|
|
|
|
#else
|
|
|
|
int fd = fileno(cp);
|
|
|
|
PyObject* PyFileObject = PyFile_FromFd(fd, m_appfile.toUtf8().data(), (char *)"r", -1, NULL, NULL,NULL,1);
|
|
|
|
if (PyRun_SimpleFile(fdopen(PyObject_AsFileDescriptor(PyFileObject),"r"), m_appfile.toUtf8().data()) != 0)
|
2013-10-04 12:16:31 -05:00
|
|
|
setError(tr("Failed to launch the application server, server thread exiting."));
|
2016-02-02 07:16:01 -06:00
|
|
|
#endif
|
2013-06-21 17:21:11 -05:00
|
|
|
|
|
|
|
fclose(cp);
|
|
|
|
}
|