From 629fc83f210367b858c0f93a5ca220fadaab1973 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 15 Jun 2021 21:50:59 +0200 Subject: [PATCH] [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). --- src/libcalamares/PythonJob.cpp | 14 ++++++++++++++ src/libcalamares/PythonJob.h | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index 98f284ecc..1a4683c29 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -19,6 +19,8 @@ #include +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 diff --git a/src/libcalamares/PythonJob.h b/src/libcalamares/PythonJob.h index 04a0645ea..af77d741d 100644 --- a/src/libcalamares/PythonJob.h +++ b/src/libcalamares/PythonJob.h @@ -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;