@ -165,6 +165,10 @@ BOOST_PYTHON_MODULE( libcalamares )
namespace Calamares
{
struct PythonJob : : Private
{
bp : : object m_prettyStatusMessage ;
} ;
PythonJob : : PythonJob ( const ModuleSystem : : InstanceKey & instance ,
const QString & scriptFile ,
@ -172,6 +176,7 @@ PythonJob::PythonJob( const ModuleSystem::InstanceKey& instance,
const QVariantMap & moduleConfiguration ,
QObject * parent )
: Job ( parent )
, m_d ( std : : make_unique < Private > ( ) )
, m_scriptFile ( scriptFile )
, m_workingPath ( workingPath )
, m_description ( )
@ -199,6 +204,7 @@ PythonJob::prettyName() const
QString
PythonJob : : prettyStatusMessage ( ) const
{
// The description is updated when progress is reported, see emitProgress()
if ( m_description . isEmpty ( ) )
{
return tr ( " Running %1 operation. " ) . arg ( QDir ( m_workingPath ) . dirName ( ) ) ;
@ -209,6 +215,18 @@ PythonJob::prettyStatusMessage() const
}
}
static QString
pythonStringMethod ( bp : : dict & script , const char * funcName )
{
bp : : object func = script . get ( funcName , bp : : object ( ) ) ;
if ( ! func . is_none ( ) )
{
bp : : extract < std : : string > result ( func ( ) ) ;
return result . check ( ) ? QString : : fromStdString ( result ( ) ) . trimmed ( ) : QString ( ) ;
}
return QString ( ) ;
}
JobResult
PythonJob : : exec ( )
@ -233,7 +251,7 @@ PythonJob::exec()
try
{
bp : : dict scriptNamespace = helper( ) - > createCleanNamespace ( ) ;
bp : : dict scriptNamespace = CalamaresPyt hon: : H elper: : instance ( ) - > createCleanNamespace ( ) ;
bp : : object calamaresModule = bp : : import ( " libcalamares " ) ;
bp : : dict calamaresNamespace = bp : : extract < bp : : dict > ( calamaresModule . attr ( " __dict__ " ) ) ;
@ -242,27 +260,13 @@ PythonJob::exec()
calamaresNamespace [ " globalstorage " ]
= CalamaresPython : : GlobalStoragePythonWrapper ( JobQueue : : instance ( ) - > globalStorage ( ) ) ;
cDebug ( ) < < " Job file " < < scriptFI . absoluteFilePath ( ) ;
bp : : object execResult
= bp : : exec_file ( scriptFI . absoluteFilePath ( ) . toLocal8Bit ( ) . data ( ) , scriptNamespace , scriptNamespace ) ;
bp : : object entryPoint = scriptNamespace [ " run " ] ;
bp : : object prettyNameFunc = scriptNamespace . get ( " pretty_name " , bp : : object ( ) ) ;
cDebug ( ) < < " Job file " < < scriptFI . absoluteFilePath ( ) ;
if ( ! prettyNameFunc . is_none ( ) )
{
bp : : extract < std : : string > prettyNameResult ( prettyNameFunc ( ) ) ;
if ( prettyNameResult . check ( ) )
{
m_description = QString : : fromStdString ( prettyNameResult ( ) ) . trimmed ( ) ;
}
if ( ! m_description . isEmpty ( ) )
{
cDebug ( ) < < " Job description from pretty_name " < < prettyName ( ) < < " = " < < m_description ;
emit progress ( 0 ) ;
}
}
m_d - > m_prettyStatusMessage = scriptNamespace . get ( " pretty_status_message " , bp : : object ( ) ) ;
m_description = pythonStringMethod ( scriptNamespace , " pretty_name " ) ;
if ( m_description . isEmpty ( ) )
{
bp : : extract < std : : string > entryPoint_doc_attr ( entryPoint . attr ( " __doc__ " ) ) ;
@ -275,10 +279,14 @@ PythonJob::exec()
{
m_description . truncate ( i_newline ) ;
}
cDebug ( ) < < " Job description from __doc__ " < < prettyName ( ) < < " = " < < m_description ;
emit progress ( 0 ) ;
cDebug ( ) < < " Job description from __doc__ " < < prettyName ( ) < < ' = ' < < m_description ;
}
}
else
{
cDebug ( ) < < " Job description from pretty_name " < < prettyName ( ) < < ' = ' < < m_description ;
}
emit progress ( 0 ) ;
bp : : object runResult = entryPoint ( ) ;
@ -299,7 +307,7 @@ PythonJob::exec()
QString msg ;
if ( PyErr_Occurred ( ) )
{
msg = helper( ) - > handleLastError ( ) ;
msg = CalamaresPyt hon: : H elper: : instance ( ) - > handleLastError ( ) ;
}
bp : : handle_exception ( ) ;
PyErr_Clear ( ) ;
@ -312,20 +320,21 @@ PythonJob::exec()
void
PythonJob : : emitProgress ( qreal progressValue )
{
emit progress ( progressValue ) ;
}
CalamaresPython : : Helper *
PythonJob : : helper ( )
{
auto ptr = CalamaresPython : : Helper : : s_instance ;
if ( ! ptr )
// This is called from the JobApi (and only from there) from the Job thread,
// so it is safe to call into the Python interpreter. Update the description
// as needed (don't call this from prettyStatusMessage(), which can be
// called from other threads as well).
if ( m_d & & ! m_d - > m_prettyStatusMessage . is_none ( ) )
{
ptr = new CalamaresPython : : Helper ;
QString r ;
bp : : extract < std : : string > result ( m_d - > m_prettyStatusMessage ( ) ) ;
r = result . check ( ) ? QString : : fromStdString ( result ( ) ) . trimmed ( ) : QString ( ) ;
if ( ! r . isEmpty ( ) )
{
m_description = r ;
}
}
return ptr ;
emit progress ( progressValue ) ;
}
} // namespace Calamares