Little hack to distribute precompiled cutter_*.py files (#465)

This commit is contained in:
Paul I 2018-04-30 12:45:02 +03:00 committed by xarkes
parent 5e4dff9639
commit eeec725b94
4 changed files with 39 additions and 6 deletions

View File

@ -46,6 +46,7 @@ install:
before_build: before_build:
- cmd: git submodule update --init - cmd: git submodule update --init
- cmd: python scripts\compile_python_resources.py
# Build config # Build config
build_script: build_script:

View File

@ -0,0 +1,10 @@
import compileall
import os
root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
compileall.compile_dir(os.path.join(root, 'src', 'python'), legacy=True, optimize=2)
with open(os.path.join(root, 'src', 'resources.qrc'), 'r+b') as f:
data = f.read()
data = data.replace(b'.py<', b'.pyc<')
f.seek(0)
f.write(data)

View File

@ -1,6 +1,7 @@
#ifdef CUTTER_ENABLE_JUPYTER #ifdef CUTTER_ENABLE_JUPYTER
#include <Python.h> #include <Python.h>
#include <marshal.h>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonArray> #include <QJsonArray>
@ -85,13 +86,23 @@ void JupyterConnection::createCutterJupyterModule()
PyEval_RestoreThread(pyThreadState); PyEval_RestoreThread(pyThreadState);
} }
QFile moduleFile(":/python/cutter_jupyter.py"); QFile moduleFile(":/python/cutter_jupyter.pyc");
bool isBytecode = moduleFile.exists();
if (!isBytecode) {
moduleFile.setFileName(":/python/cutter_jupyter.py");
}
moduleFile.open(QIODevice::ReadOnly); moduleFile.open(QIODevice::ReadOnly);
QByteArray moduleCode = moduleFile.readAll(); QByteArray moduleCode = moduleFile.readAll();
moduleFile.close(); moduleFile.close();
auto moduleCodeObject = Py_CompileString(moduleCode.constData(), "cutter_jupyter.py", PyObject *moduleCodeObject;
Py_file_input); if (isBytecode) {
moduleCodeObject = PyMarshal_ReadObjectFromString(moduleCode.constData() + 12,
moduleCode.size() - 12);
} else {
moduleCodeObject = Py_CompileString(moduleCode.constData(), "cutter_jupyter.py",
Py_file_input);
}
if (!moduleCodeObject) { if (!moduleCodeObject) {
PyErr_Print(); PyErr_Print();
qWarning() << "Could not compile cutter_jupyter."; qWarning() << "Could not compile cutter_jupyter.";

View File

@ -2,6 +2,7 @@
#ifdef CUTTER_ENABLE_JUPYTER #ifdef CUTTER_ENABLE_JUPYTER
#include <Python.h> #include <Python.h>
#include <marshal.h>
#include <QFile> #include <QFile>
#include <csignal> #include <csignal>
@ -19,13 +20,23 @@ NestedIPyKernel *NestedIPyKernel::start(const QStringList &argv)
return nullptr; return nullptr;
} }
QFile moduleFile(":/python/cutter_ipykernel.py"); QFile moduleFile(":/python/cutter_ipykernel.pyc");
bool isBytecode = moduleFile.exists();
if (!isBytecode) {
moduleFile.setFileName(":/python/cutter_ipykernel.py");
}
moduleFile.open(QIODevice::ReadOnly); moduleFile.open(QIODevice::ReadOnly);
QByteArray moduleCode = moduleFile.readAll(); QByteArray moduleCode = moduleFile.readAll();
moduleFile.close(); moduleFile.close();
auto moduleCodeObject = Py_CompileString(moduleCode.constData(), "cutter_ipykernel.py", PyObject *moduleCodeObject;
Py_file_input); if (isBytecode) {
moduleCodeObject = PyMarshal_ReadObjectFromString(moduleCode.constData() + 12,
moduleCode.size() - 12);
} else {
moduleCodeObject = Py_CompileString(moduleCode.constData(), "cutter_ipykernel.py",
Py_file_input);
}
if (!moduleCodeObject) { if (!moduleCodeObject) {
qWarning() << "Could not compile cutter_ipykernel."; qWarning() << "Could not compile cutter_ipykernel.";
return nullptr; return nullptr;