[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.
This commit is contained in:
Adriaan de Groot 2021-06-15 23:21:32 +02:00
parent 629fc83f21
commit cc0e39db56

View File

@ -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 );