[libcalamares] Add a pre-script for PythonJobs

This allows injecting arbitrary Python code before
the script of a module is even run. For testing
purposes, that gives us a chance to modify existing
(internal) modules before the script (e.g. to test
subprocess calls).
This commit is contained in:
Adriaan de Groot 2021-06-15 21:50:59 +02:00
parent 69cad09a00
commit 629fc83f21
2 changed files with 27 additions and 0 deletions

View File

@ -19,6 +19,8 @@
#include <QDir>
static const char* s_preScript = nullptr;
namespace bp = boost::python;
BOOST_PYTHON_FUNCTION_OVERLOADS( mount_overloads, CalamaresPython::mount, 2, 4 );
@ -242,6 +244,11 @@ PythonJob::exec()
calamaresNamespace[ "globalstorage" ]
= CalamaresPython::GlobalStoragePythonWrapper( JobQueue::instance()->globalStorage() );
if ( s_preScript )
{
bp::exec( s_preScript, scriptNamespace, scriptNamespace );
}
cDebug() << "Job file" << scriptFI.absoluteFilePath();
bp::object execResult
= bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(), scriptNamespace, scriptNamespace );
@ -319,4 +326,11 @@ PythonJob::emitProgress( qreal progressValue )
emit progress( progressValue );
}
void
PythonJob::setInjectedPreScript( const char* preScript )
{
s_preScript = preScript;
cDebug() << "Python pre-script set to" << Logger::Pointer( preScript );
}
} // namespace Calamares

View File

@ -41,6 +41,19 @@ public:
QString prettyStatusMessage() const override;
JobResult exec() override;
/** @brief Sets the pre-run Python code for all PythonJobs
*
* A PythonJob runs the code from the scriptFile parameter to
* the constructor; the pre-run code is **also** run, before
* even the scriptFile code. Use this in testing mode
* to modify Python internals.
*
* No ownership of @p script is taken: pass in a pointer to
* a character literal or something that lives longer than the
* job. Pass in @c nullptr to switch off pre-run code.
*/
static void setInjectedPreScript( const char* script );
private:
struct Private;