diff --git a/src/calamares/CMakeLists.txt b/src/calamares/CMakeLists.txt index 3be6b92a3..4ecc61230 100644 --- a/src/calamares/CMakeLists.txt +++ b/src/calamares/CMakeLists.txt @@ -64,4 +64,9 @@ if(BUILD_TESTING) add_executable(test_conf test_conf.cpp) target_link_libraries(test_conf PUBLIC yamlcpp::yamlcpp ${qtname}::Core) + + if(WITH_PYBIND11) + target_compile_definitions(loadmodule PRIVATE WITH_PYBIND11=1) + endif() + endif() diff --git a/src/calamares/testmain.cpp b/src/calamares/testmain.cpp index a2175b7d4..1aa11a81a 100644 --- a/src/calamares/testmain.cpp +++ b/src/calamares/testmain.cpp @@ -29,11 +29,16 @@ #include "viewpages/ExecutionViewStep.h" // Optional features of Calamares -// - Python support +// - Python support with pybind11 +// - Python support with older Boost implementation // - QML support #ifdef WITH_PYTHON +#if WITH_PYBIND11 +#include "python/PythonJob.h" +#else #include "PythonJob.h" #endif +#endif #ifdef WITH_QML #include "utils/Qml.h" #endif @@ -472,7 +477,12 @@ main( int argc, char* argv[] ) #ifdef WITH_PYTHON if ( module.m_pythonInjection ) { +#if WITH_PYBIND11 + Calamares::Python::Job::setInjectedPreScript( pythonPreScript ); +#else + // Old Boost approach Calamares::PythonJob::setInjectedPreScript( pythonPreScript ); +#endif } #endif #ifdef WITH_QML diff --git a/src/libcalamares/python/PythonJob.cpp b/src/libcalamares/python/PythonJob.cpp new file mode 100644 index 000000000..bdd6860d3 --- /dev/null +++ b/src/libcalamares/python/PythonJob.cpp @@ -0,0 +1,64 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2023 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ +#include "python/PythonJob.h" + +namespace Calamares +{ +namespace Python +{ + +struct Job::Private +{ +}; + +Job::Job( const QString& scriptFile, + const QString& workingPath, + const QVariantMap& moduleConfiguration, + QObject* parent ) +{ +} + +Job::~Job() {} + +QString +Job::prettyName() const +{ + return QStringLiteral( "Python Pretty" ); +} + +QString +Job::prettyStatusMessage() const +{ + return QStringLiteral( "Python Status" ); +} + +JobResult +Job::exec() +{ + return JobResult::ok(); +} + +/** @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. + */ +void +Job::setInjectedPreScript( const char* script ) +{ +} + +} // namespace Python +} // namespace Calamares diff --git a/src/libcalamares/python/PythonJob.h b/src/libcalamares/python/PythonJob.h new file mode 100644 index 000000000..314329ddb --- /dev/null +++ b/src/libcalamares/python/PythonJob.h @@ -0,0 +1,61 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2023 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ + +#ifndef CALAMARES_PYTHON_PYTHONJOB_H +#define CALAMARES_PYTHON_PYTHONJOB_H + +// This file is called PythonJob.h because it would otherwise +// clashwith the Job.h in libcalamares proper. + +#include "Job.h" + +#include + +#include + +namespace Calamares +{ +namespace Python +{ + +class Job : public ::Calamares::Job +{ + Q_OBJECT +public: + explicit Job( const QString& scriptFile, + const QString& workingPath, + const QVariantMap& moduleConfiguration = QVariantMap(), + QObject* parent = nullptr ); + ~Job() override; + + QString prettyName() const override; + QString prettyStatusMessage() const override; + ::Calamares::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; + std::unique_ptr< Private > m_d; +}; + +} // namespace Python +} // namespace Calamares +#endif diff --git a/src/libcalamaresui/CMakeLists.txt b/src/libcalamaresui/CMakeLists.txt index 41305e249..2e36b26a4 100644 --- a/src/libcalamaresui/CMakeLists.txt +++ b/src/libcalamaresui/CMakeLists.txt @@ -64,6 +64,10 @@ if(WITH_QML) target_link_libraries(calamaresui PUBLIC ${qtname}::QuickWidgets) endif() +if(WITH_PYBIND11) + target_compile_definitions(calamaresui PRIVATE WITH_PYBIND11=1) +endif() + add_library(Calamares::calamaresui ALIAS calamaresui) ### Installation diff --git a/src/libcalamaresui/modulesystem/PythonJobModule.cpp b/src/libcalamaresui/modulesystem/PythonJobModule.cpp index 42acc0c69..1936f41e7 100644 --- a/src/libcalamaresui/modulesystem/PythonJobModule.cpp +++ b/src/libcalamaresui/modulesystem/PythonJobModule.cpp @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * SPDX-FileCopyrightText: 2014 Teo Mrnjavac + * SPDX-FileCopyrightText: 2023 Adriaan de Groot * SPDX-License-Identifier: GPL-3.0-or-later * * Calamares is Free Software: see the License-Identifier above. @@ -9,7 +10,14 @@ #include "PythonJobModule.h" +#if WITH_PYBIND11 +#include "python/PythonJob.h" +using JobType = Calamares::Python::Job; +#else +// Old Boost::Python version #include "PythonJob.h" +using JobType = Calamares::PythonJob; +#endif #include @@ -40,7 +48,7 @@ PythonJobModule::loadSelf() return; } - m_job = Calamares::job_ptr( new PythonJob( m_scriptFileName, m_workingPath, m_configurationMap ) ); + m_job = Calamares::job_ptr( new JobType( m_scriptFileName, m_workingPath, m_configurationMap ) ); m_loaded = true; }