From cc0e39db562af8b3a324505bea5767f820cda767 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 15 Jun 2021 23:21:32 +0200 Subject: [PATCH] [calamares] Inject a pre-script when testing Python modules Co-opt the subprocess module and replace call and check_call functions with something that logs the call and does nothing. --- src/calamares/testmain.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/calamares/testmain.cpp b/src/calamares/testmain.cpp index 4cde80300..6dae0af68 100644 --- a/src/calamares/testmain.cpp +++ b/src/calamares/testmain.cpp @@ -18,6 +18,7 @@ #include "GlobalStorage.h" #include "Job.h" #include "JobQueue.h" +#include "PythonJob.h" #include "Settings.h" #include "ViewManager.h" #include "modulesystem/Module.h" @@ -365,6 +366,27 @@ createApplication( int& argc, char* argv[] ) return new QCoreApplication( argc, argv ); } +static const char pythonPreScript[] = R"( +# This is Python code executed by Python modules *before* the +# script file (e.g. main.py) is executed. Beware " before ) +# because it's a C++ raw-string. +_calamares_subprocess = __import__("subprocess", globals(), locals(), [], 0) +import sys +import libcalamares +class fake_subprocess(object): + @staticmethod + def call(*args, **kwargs): + libcalamares.utils.debug("subprocess.call(%r,%r) X run normally" % (args, kwargs)) + return 0 + @staticmethod + def check_call(*args, **kwargs): + libcalamares.utils.debug("subprocess.check_call(%r,%r) X subverted to call" % (args, kwargs)) + return 0 +sys.modules["subprocess"] = fake_subprocess +libcalamares.utils.debug('pre-script for testing purposes injected') + +)"; + int main( int argc, char* argv[] ) { @@ -397,6 +419,7 @@ main( int argc, char* argv[] ) #ifdef WITH_QML CalamaresUtils::initQmlModulesDir(); // don't care if failed #endif + Calamares::PythonJob::setInjectedPreScript(pythonPreScript); cDebug() << "Calamares module-loader testing" << module.moduleName(); Calamares::Module* m = load_module( module );