Merge branch 'issue-1330'
- When Python modules emit progress, update their status message by calling an optional pretty_status_message() in the Python code. This is polled (later) by the execution progress bar to display the message. FIXES #1330
This commit is contained in:
commit
2891e92d16
@ -105,8 +105,20 @@ public:
|
|||||||
* how much work is (relatively) done.
|
* how much work is (relatively) done.
|
||||||
*/
|
*/
|
||||||
virtual qreal getJobWeight() const;
|
virtual qreal getJobWeight() const;
|
||||||
|
/** @brief The human-readable name of this job
|
||||||
|
*
|
||||||
|
* This should be a very short statement of what the job does.
|
||||||
|
* For status and state information, see prettyStatusMessage().
|
||||||
|
*/
|
||||||
virtual QString prettyName() const = 0;
|
virtual QString prettyName() const = 0;
|
||||||
|
// TODO: Unused
|
||||||
virtual QString prettyDescription() const;
|
virtual QString prettyDescription() const;
|
||||||
|
/** @brief A human-readable status for progress reporting
|
||||||
|
*
|
||||||
|
* This is called from the JobQueue when progress is made, and should
|
||||||
|
* return a not-too-long description of the job's status. This
|
||||||
|
* is made visible in the progress bar of the execution view step.
|
||||||
|
*/
|
||||||
virtual QString prettyStatusMessage() const;
|
virtual QString prettyStatusMessage() const;
|
||||||
virtual JobResult exec() = 0;
|
virtual JobResult exec() = 0;
|
||||||
|
|
||||||
|
@ -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,12 +219,10 @@ 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();
|
||||||
@ -248,21 +244,21 @@ Helper::Helper( QObject* parent )
|
|||||||
bp::str dir = path.toLocal8Bit().data();
|
bp::str dir = path.toLocal8Bit().data();
|
||||||
sys.attr( "path" ).attr( "append" )( dir );
|
sys.attr( "path" ).attr( "append" )( dir );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cWarning() << "creating PythonHelper more than once. This is very bad.";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
s_instance = this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
@ -165,6 +165,10 @@ BOOST_PYTHON_MODULE( libcalamares )
|
|||||||
namespace Calamares
|
namespace Calamares
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct PythonJob::Private
|
||||||
|
{
|
||||||
|
bp::object m_prettyStatusMessage;
|
||||||
|
};
|
||||||
|
|
||||||
PythonJob::PythonJob( const ModuleSystem::InstanceKey& instance,
|
PythonJob::PythonJob( const ModuleSystem::InstanceKey& instance,
|
||||||
const QString& scriptFile,
|
const QString& scriptFile,
|
||||||
@ -172,6 +176,7 @@ PythonJob::PythonJob( const ModuleSystem::InstanceKey& instance,
|
|||||||
const QVariantMap& moduleConfiguration,
|
const QVariantMap& moduleConfiguration,
|
||||||
QObject* parent )
|
QObject* parent )
|
||||||
: Job( parent )
|
: Job( parent )
|
||||||
|
, m_d( std::make_unique< Private >() )
|
||||||
, m_scriptFile( scriptFile )
|
, m_scriptFile( scriptFile )
|
||||||
, m_workingPath( workingPath )
|
, m_workingPath( workingPath )
|
||||||
, m_description()
|
, m_description()
|
||||||
@ -199,6 +204,7 @@ PythonJob::prettyName() const
|
|||||||
QString
|
QString
|
||||||
PythonJob::prettyStatusMessage() const
|
PythonJob::prettyStatusMessage() const
|
||||||
{
|
{
|
||||||
|
// The description is updated when progress is reported, see emitProgress()
|
||||||
if ( m_description.isEmpty() )
|
if ( m_description.isEmpty() )
|
||||||
{
|
{
|
||||||
return tr( "Running %1 operation." ).arg( QDir( m_workingPath ).dirName() );
|
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
|
JobResult
|
||||||
PythonJob::exec()
|
PythonJob::exec()
|
||||||
@ -233,7 +251,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__" ) );
|
||||||
@ -242,27 +260,13 @@ PythonJob::exec()
|
|||||||
calamaresNamespace[ "globalstorage" ]
|
calamaresNamespace[ "globalstorage" ]
|
||||||
= CalamaresPython::GlobalStoragePythonWrapper( JobQueue::instance()->globalStorage() );
|
= CalamaresPython::GlobalStoragePythonWrapper( JobQueue::instance()->globalStorage() );
|
||||||
|
|
||||||
|
cDebug() << "Job file" << scriptFI.absoluteFilePath();
|
||||||
bp::object execResult
|
bp::object execResult
|
||||||
= bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(), scriptNamespace, scriptNamespace );
|
= bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(), scriptNamespace, scriptNamespace );
|
||||||
|
|
||||||
bp::object entryPoint = scriptNamespace[ "run" ];
|
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() )
|
if ( m_description.isEmpty() )
|
||||||
{
|
{
|
||||||
bp::extract< std::string > entryPoint_doc_attr( entryPoint.attr( "__doc__" ) );
|
bp::extract< std::string > entryPoint_doc_attr( entryPoint.attr( "__doc__" ) );
|
||||||
@ -275,10 +279,14 @@ PythonJob::exec()
|
|||||||
{
|
{
|
||||||
m_description.truncate( i_newline );
|
m_description.truncate( i_newline );
|
||||||
}
|
}
|
||||||
cDebug() << "Job description from __doc__" << prettyName() << "=" << m_description;
|
cDebug() << "Job description from __doc__" << prettyName() << '=' << m_description;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cDebug() << "Job description from pretty_name" << prettyName() << '=' << m_description;
|
||||||
|
}
|
||||||
emit progress( 0 );
|
emit progress( 0 );
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bp::object runResult = entryPoint();
|
bp::object runResult = entryPoint();
|
||||||
|
|
||||||
@ -299,7 +307,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();
|
||||||
@ -312,20 +320,21 @@ PythonJob::exec()
|
|||||||
void
|
void
|
||||||
PythonJob::emitProgress( qreal progressValue )
|
PythonJob::emitProgress( qreal progressValue )
|
||||||
{
|
{
|
||||||
|
// 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() )
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
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
|
||||||
|
@ -53,11 +53,12 @@ public:
|
|||||||
virtual qreal getJobWeight() const override;
|
virtual qreal getJobWeight() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class CalamaresPython::Helper;
|
struct Private;
|
||||||
|
|
||||||
friend class CalamaresPython::PythonJobInterface;
|
friend class CalamaresPython::PythonJobInterface;
|
||||||
void emitProgress( double progressValue );
|
void emitProgress( double progressValue );
|
||||||
|
|
||||||
CalamaresPython::Helper* helper();
|
std::unique_ptr< Private > m_d;
|
||||||
QString m_scriptFile;
|
QString m_scriptFile;
|
||||||
QString m_workingPath;
|
QString m_workingPath;
|
||||||
QString m_description;
|
QString m_description;
|
||||||
|
@ -171,7 +171,7 @@ PythonJobInterface::PythonJobInterface( Calamares::PythonJob* parent )
|
|||||||
void
|
void
|
||||||
PythonJobInterface::setprogress( qreal progress )
|
PythonJobInterface::setprogress( qreal progress )
|
||||||
{
|
{
|
||||||
if ( progress >= 0 && progress <= 1 )
|
if ( progress >= 0.0 && progress <= 1.0 )
|
||||||
{
|
{
|
||||||
m_parent->emitProgress( progress );
|
m_parent->emitProgress( progress );
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,10 @@ _ = gettext.translation("calamares-python",
|
|||||||
def pretty_name():
|
def pretty_name():
|
||||||
return _("Dummy python job.")
|
return _("Dummy python job.")
|
||||||
|
|
||||||
|
status = _("Dummy python step {}").format(0)
|
||||||
|
|
||||||
|
def pretty_status_message():
|
||||||
|
return status
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
"""Dummy python job."""
|
"""Dummy python job."""
|
||||||
@ -92,8 +96,10 @@ def run():
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
configlist = ["no list"]
|
configlist = ["no list"]
|
||||||
|
|
||||||
|
global status
|
||||||
c = 1
|
c = 1
|
||||||
for k in configlist:
|
for k in configlist:
|
||||||
|
status = _("Dummy python step {}").format(str(c) + ":" + repr(k))
|
||||||
libcalamares.utils.debug(_("Dummy python step {}").format(str(k)))
|
libcalamares.utils.debug(_("Dummy python step {}").format(str(k)))
|
||||||
sleep(1)
|
sleep(1)
|
||||||
libcalamares.job.setprogress(c * 1.0 / len(configlist))
|
libcalamares.job.setprogress(c * 1.0 / len(configlist))
|
||||||
|
@ -56,6 +56,10 @@ def _change_mode(mode):
|
|||||||
|
|
||||||
|
|
||||||
def pretty_name():
|
def pretty_name():
|
||||||
|
return _("Install packages.")
|
||||||
|
|
||||||
|
|
||||||
|
def pretty_status_message():
|
||||||
if not group_packages:
|
if not group_packages:
|
||||||
if (total_packages > 0):
|
if (total_packages > 0):
|
||||||
# Outside the context of an operation
|
# Outside the context of an operation
|
||||||
@ -332,19 +336,23 @@ class PMDummy(PackageManager):
|
|||||||
backend = "dummy"
|
backend = "dummy"
|
||||||
|
|
||||||
def install(self, pkgs, from_local=False):
|
def install(self, pkgs, from_local=False):
|
||||||
libcalamares.utils.debug("Installing " + str(pkgs))
|
from time import sleep
|
||||||
|
libcalamares.utils.debug("Dummy backend: Installing " + str(pkgs))
|
||||||
|
sleep(3)
|
||||||
|
|
||||||
def remove(self, pkgs):
|
def remove(self, pkgs):
|
||||||
libcalamares.utils.debug("Removing " + str(pkgs))
|
from time import sleep
|
||||||
|
libcalamares.utils.debug("Dummy backend: Removing " + str(pkgs))
|
||||||
|
sleep(3)
|
||||||
|
|
||||||
def update_db(self):
|
def update_db(self):
|
||||||
libcalamares.utils.debug("Updating DB")
|
libcalamares.utils.debug("Dummy backend: Updating DB")
|
||||||
|
|
||||||
def update_system(self):
|
def update_system(self):
|
||||||
libcalamares.utils.debug("Updating System")
|
libcalamares.utils.debug("Dummy backend: Updating System")
|
||||||
|
|
||||||
def run(self, script):
|
def run(self, script):
|
||||||
libcalamares.utils.debug("Running script '" + str(script) + "'")
|
libcalamares.utils.debug("Dummy backend: Running script '" + str(script) + "'")
|
||||||
|
|
||||||
|
|
||||||
class PMPisi(PackageManager):
|
class PMPisi(PackageManager):
|
||||||
@ -502,7 +510,7 @@ def run_operations(pkgman, entry):
|
|||||||
libcalamares.utils.warning("Unknown package-operation key {!s}".format(key))
|
libcalamares.utils.warning("Unknown package-operation key {!s}".format(key))
|
||||||
completed_packages += len(package_list)
|
completed_packages += len(package_list)
|
||||||
libcalamares.job.setprogress(completed_packages * 1.0 / total_packages)
|
libcalamares.job.setprogress(completed_packages * 1.0 / total_packages)
|
||||||
libcalamares.utils.debug(pretty_name())
|
libcalamares.utils.debug("Pretty name: {!s}, setting progress..".format(pretty_name()))
|
||||||
|
|
||||||
group_packages = 0
|
group_packages = 0
|
||||||
_change_mode(None)
|
_change_mode(None)
|
||||||
|
Loading…
Reference in New Issue
Block a user