mirror of
https://github.com/parchlinux/calamares.git
synced 2025-06-30 10:55:37 -04:00
[libcalamares] Factor out Python helper
- the strange construction of Helper and treating it as a singleton can be factored out into a separate singleton-handling instance() function. The Helper should never be destroyed.
This commit is contained in:
parent
3025c5383b
commit
9b5a391c86
4 changed files with 37 additions and 56 deletions
|
@ -204,8 +204,6 @@ variantHashFromPyDict( const boost::python::dict& pyDict )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Helper* Helper::s_instance = nullptr;
|
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
add_if_lib_exists( const QDir& dir, const char* name, QStringList& list )
|
add_if_lib_exists( const QDir& dir, const char* name, QStringList& list )
|
||||||
{
|
{
|
||||||
|
@ -221,48 +219,46 @@ add_if_lib_exists( const QDir& dir, const char* name, QStringList& list )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Helper::Helper( QObject* parent )
|
Helper::Helper()
|
||||||
: QObject( parent )
|
: QObject( nullptr )
|
||||||
{
|
{
|
||||||
// Let's make extra sure we only call Py_Initialize once
|
// Let's make extra sure we only call Py_Initialize once
|
||||||
if ( !s_instance )
|
if ( !Py_IsInitialized() )
|
||||||
{
|
{
|
||||||
if ( !Py_IsInitialized() )
|
Py_Initialize();
|
||||||
{
|
|
||||||
Py_Initialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_mainModule = bp::import( "__main__" );
|
|
||||||
m_mainNamespace = m_mainModule.attr( "__dict__" );
|
|
||||||
|
|
||||||
// If we're running from the build dir
|
|
||||||
add_if_lib_exists( QDir::current(), "libcalamares.so", m_pythonPaths );
|
|
||||||
|
|
||||||
QDir calaPythonPath( CalamaresUtils::systemLibDir().absolutePath() + QDir::separator() + "calamares" );
|
|
||||||
add_if_lib_exists( calaPythonPath, "libcalamares.so", m_pythonPaths );
|
|
||||||
|
|
||||||
bp::object sys = bp::import( "sys" );
|
|
||||||
|
|
||||||
foreach ( QString path, m_pythonPaths )
|
|
||||||
{
|
|
||||||
bp::str dir = path.toLocal8Bit().data();
|
|
||||||
sys.attr( "path" ).attr( "append" )( dir );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cWarning() << "creating PythonHelper more than once. This is very bad.";
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s_instance = this;
|
m_mainModule = bp::import( "__main__" );
|
||||||
|
m_mainNamespace = m_mainModule.attr( "__dict__" );
|
||||||
|
|
||||||
|
// If we're running from the build dir
|
||||||
|
add_if_lib_exists( QDir::current(), "libcalamares.so", m_pythonPaths );
|
||||||
|
|
||||||
|
QDir calaPythonPath( CalamaresUtils::systemLibDir().absolutePath() + QDir::separator() + "calamares" );
|
||||||
|
add_if_lib_exists( calaPythonPath, "libcalamares.so", m_pythonPaths );
|
||||||
|
|
||||||
|
bp::object sys = bp::import( "sys" );
|
||||||
|
|
||||||
|
foreach ( QString path, m_pythonPaths )
|
||||||
|
{
|
||||||
|
bp::str dir = path.toLocal8Bit().data();
|
||||||
|
sys.attr( "path" ).attr( "append" )( dir );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Helper::~Helper()
|
Helper::~Helper() {}
|
||||||
|
|
||||||
|
Helper*
|
||||||
|
Helper::instance()
|
||||||
{
|
{
|
||||||
s_instance = nullptr;
|
static Helper* s_helper = nullptr;
|
||||||
}
|
|
||||||
|
|
||||||
|
if ( !s_helper )
|
||||||
|
{
|
||||||
|
s_helper = new Helper;
|
||||||
|
}
|
||||||
|
return s_helper;
|
||||||
|
}
|
||||||
|
|
||||||
boost::python::dict
|
boost::python::dict
|
||||||
Helper::createCleanNamespace()
|
Helper::createCleanNamespace()
|
||||||
|
|
|
@ -50,16 +50,15 @@ class Helper : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
virtual ~Helper();
|
|
||||||
|
|
||||||
boost::python::dict createCleanNamespace();
|
boost::python::dict createCleanNamespace();
|
||||||
|
|
||||||
QString handleLastError();
|
QString handleLastError();
|
||||||
|
|
||||||
|
static Helper* instance();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend Helper* Calamares::PythonJob::helper();
|
virtual ~Helper();
|
||||||
explicit Helper( QObject* parent = nullptr );
|
explicit Helper();
|
||||||
static Helper* s_instance;
|
|
||||||
|
|
||||||
boost::python::object m_mainModule;
|
boost::python::object m_mainModule;
|
||||||
boost::python::object m_mainNamespace;
|
boost::python::object m_mainNamespace;
|
||||||
|
|
|
@ -233,7 +233,7 @@ PythonJob::exec()
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
bp::dict scriptNamespace = helper()->createCleanNamespace();
|
bp::dict scriptNamespace = CalamaresPython::Helper::instance()->createCleanNamespace();
|
||||||
|
|
||||||
bp::object calamaresModule = bp::import( "libcalamares" );
|
bp::object calamaresModule = bp::import( "libcalamares" );
|
||||||
bp::dict calamaresNamespace = bp::extract< bp::dict >( calamaresModule.attr( "__dict__" ) );
|
bp::dict calamaresNamespace = bp::extract< bp::dict >( calamaresModule.attr( "__dict__" ) );
|
||||||
|
@ -299,7 +299,7 @@ PythonJob::exec()
|
||||||
QString msg;
|
QString msg;
|
||||||
if ( PyErr_Occurred() )
|
if ( PyErr_Occurred() )
|
||||||
{
|
{
|
||||||
msg = helper()->handleLastError();
|
msg = CalamaresPython::Helper::instance()->handleLastError();
|
||||||
}
|
}
|
||||||
bp::handle_exception();
|
bp::handle_exception();
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
@ -315,17 +315,4 @@ PythonJob::emitProgress( qreal progressValue )
|
||||||
emit progress( progressValue );
|
emit progress( progressValue );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CalamaresPython::Helper*
|
|
||||||
PythonJob::helper()
|
|
||||||
{
|
|
||||||
auto ptr = CalamaresPython::Helper::s_instance;
|
|
||||||
if ( !ptr )
|
|
||||||
{
|
|
||||||
ptr = new CalamaresPython::Helper;
|
|
||||||
}
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace Calamares
|
} // namespace Calamares
|
||||||
|
|
|
@ -57,7 +57,6 @@ private:
|
||||||
friend class CalamaresPython::PythonJobInterface;
|
friend class CalamaresPython::PythonJobInterface;
|
||||||
void emitProgress( double progressValue );
|
void emitProgress( double progressValue );
|
||||||
|
|
||||||
CalamaresPython::Helper* helper();
|
|
||||||
QString m_scriptFile;
|
QString m_scriptFile;
|
||||||
QString m_workingPath;
|
QString m_workingPath;
|
||||||
QString m_description;
|
QString m_description;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue