From da0ce1d3d650c3408515d1607bf7dbbe4fdaac7c Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 24 Oct 2023 15:29:07 +0200 Subject: [PATCH] libcalamares: move Python module-definitions to the Job --- src/libcalamares/python/Api.cpp | 101 -------------------------- src/libcalamares/python/PythonJob.cpp | 96 ++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 101 deletions(-) diff --git a/src/libcalamares/python/Api.cpp b/src/libcalamares/python/Api.cpp index 36e232e1f..247568542 100644 --- a/src/libcalamares/python/Api.cpp +++ b/src/libcalamares/python/Api.cpp @@ -9,7 +9,6 @@ */ #include "python/Api.h" -#include "CalamaresVersion.h" #include "GlobalStorage.h" #include "JobQueue.h" #include "compat/Variant.h" @@ -577,103 +576,3 @@ GlobalStorageProxy::value( const std::string& key ) const } // namespace Python } // namespace Calamares - -// Not using EMBEDDED_MODULE because that does not let -// use use name "libcalamares.utils" and using just "utils" -// causes command-line use of Python3 followed by `import libcalamares` -// to crash with an error about adding modules after the interpreter -// has been initialized. -static void -populate_utils( py::module_& m ) -{ - m.def( "obscure", &Calamares::Python::obscure, "A function that obscures (encodes) a string" ); - - m.def( "debug", &Calamares::Python::debug, "Log a debug-message" ); - m.def( "warn", &Calamares::Python::warning, "Log a warning-message" ); - m.def( "warning", &Calamares::Python::warning, "Log a warning-message" ); - m.def( "error", &Calamares::Python::error, "Log an error-message" ); - - m.def( "load_yaml", &Calamares::Python::load_yaml, "Loads YAML from a file." ); - - m.def( "target_env_call", - &Calamares::Python::target_env_call, - "Runs command in target, returns exit code.", - py::arg( "command_list" ), - py::arg( "input" ) = std::string(), - py::arg( "timeout" ) = 0 ); - m.def( "check_target_env_call", - &Calamares::Python::check_target_env_call, - "Runs command in target, raises on error exit.", - py::arg( "command_list" ), - py::arg( "input" ) = std::string(), - py::arg( "timeout" ) = 0 ); - m.def( "check_target_env_output", - &Calamares::Python::check_target_env_output, - "Runs command in target, returns standard output or raises on error.", - py::arg( "command_list" ), - py::arg( "input" ) = std::string(), - py::arg( "timeout" ) = 0 ); - m.def( "target_env_process_output", - &Calamares::Python::target_env_process_output, - "Runs command in target, updating callback and returns standard output or raises on error.", - py::arg( "command_list" ), - py::arg( "callback" ) = pybind11::none(), - py::arg( "input" ) = std::string(), - py::arg( "timeout" ) = 0 ); - m.def( "host_env_process_output", - &Calamares::Python::host_env_process_output, - "Runs command in target, updating callback and returns standard output or raises on error.", - py::arg( "command_list" ), - py::arg( "callback" ) = pybind11::none(), - py::arg( "input" ) = std::string(), - py::arg( "timeout" ) = 0 ); - - m.def( "gettext_languages", - &Calamares::Python::gettext_languages, - "Returns list of languages (most to least-specific) for gettext." ); - m.def( "gettext_path", &Calamares::Python::gettext_path, "Returns path for gettext search." ); - - m.def( "mount", - &Calamares::Python::mount, - "Runs the mount utility with the specified parameters.\n" - "Returns the program's exit code, or:\n" - "-1 = QProcess crash\n" - "-2 = QProcess cannot start\n" - "-3 = bad arguments" ); -} - -static void -populate_libcalamares( py::module_& m ) -{ - m.doc() = "Calamares API for Python"; - - m.add_object( "ORGANIZATION_NAME", Calamares::Python::String( CALAMARES_ORGANIZATION_NAME ) ); - m.add_object( "ORGANIZATION_DOMAIN", Calamares::Python::String( CALAMARES_ORGANIZATION_DOMAIN ) ); - m.add_object( "APPLICATION_NAME", Calamares::Python::String( CALAMARES_APPLICATION_NAME ) ); - m.add_object( "VERSION", Calamares::Python::String( CALAMARES_VERSION ) ); - m.add_object( "VERSION_SHORT", Calamares::Python::String( CALAMARES_VERSION_SHORT ) ); - - auto utils = m.def_submodule( "utils", "Calamares Utility API for Python" ); - populate_utils( utils ); - - py::class_< Calamares::Python::JobProxy >( m, "Job" ) - .def_readonly( "module_name", &Calamares::Python::JobProxy::moduleName ) - .def_readonly( "pretty_name", &Calamares::Python::JobProxy::prettyName ) - .def_readonly( "working_path", &Calamares::Python::JobProxy::workingPath ) - .def_readonly( "configuration", &Calamares::Python::JobProxy::configuration ) - .def( "setprogress", &Calamares::Python::JobProxy::setprogress ); - - py::class_< Calamares::Python::GlobalStorageProxy >( m, "GlobalStorage" ) - .def( py::init( []( std::nullptr_t p ) { return new Calamares::Python::GlobalStorageProxy( nullptr ); } ) ) - .def( "contains", &Calamares::Python::GlobalStorageProxy::contains ) - .def( "count", &Calamares::Python::GlobalStorageProxy::count ) - .def( "insert", &Calamares::Python::GlobalStorageProxy::insert ) - .def( "keys", &Calamares::Python::GlobalStorageProxy::keys ) - .def( "remove", &Calamares::Python::GlobalStorageProxy::remove ) - .def( "value", &Calamares::Python::GlobalStorageProxy::value ); -} - -PYBIND11_MODULE( libcalamares, m ) -{ - populate_libcalamares( m ); -} diff --git a/src/libcalamares/python/PythonJob.cpp b/src/libcalamares/python/PythonJob.cpp index d936de95d..0be2d8916 100644 --- a/src/libcalamares/python/PythonJob.cpp +++ b/src/libcalamares/python/PythonJob.cpp @@ -8,6 +8,7 @@ */ #include "python/PythonJob.h" +#include "CalamaresVersion.h" #include "GlobalStorage.h" #include "JobQueue.h" #include "python/Api.h" @@ -74,6 +75,96 @@ getPrettyNameFromScope( const py::dict& scope ) return QString(); } +void +populate_utils( py::module_& m ) +{ + m.def( "obscure", &Calamares::Python::obscure, "A function that obscures (encodes) a string" ); + + m.def( "debug", &Calamares::Python::debug, "Log a debug-message" ); + m.def( "warn", &Calamares::Python::warning, "Log a warning-message" ); + m.def( "warning", &Calamares::Python::warning, "Log a warning-message" ); + m.def( "error", &Calamares::Python::error, "Log an error-message" ); + + m.def( "load_yaml", &Calamares::Python::load_yaml, "Loads YAML from a file." ); + + m.def( "target_env_call", + &Calamares::Python::target_env_call, + "Runs command in target, returns exit code.", + py::arg( "command_list" ), + py::arg( "input" ) = std::string(), + py::arg( "timeout" ) = 0 ); + m.def( "check_target_env_call", + &Calamares::Python::check_target_env_call, + "Runs command in target, raises on error exit.", + py::arg( "command_list" ), + py::arg( "input" ) = std::string(), + py::arg( "timeout" ) = 0 ); + m.def( "check_target_env_output", + &Calamares::Python::check_target_env_output, + "Runs command in target, returns standard output or raises on error.", + py::arg( "command_list" ), + py::arg( "input" ) = std::string(), + py::arg( "timeout" ) = 0 ); + m.def( "target_env_process_output", + &Calamares::Python::target_env_process_output, + "Runs command in target, updating callback and returns standard output or raises on error.", + py::arg( "command_list" ), + py::arg( "callback" ) = pybind11::none(), + py::arg( "input" ) = std::string(), + py::arg( "timeout" ) = 0 ); + m.def( "host_env_process_output", + &Calamares::Python::host_env_process_output, + "Runs command in target, updating callback and returns standard output or raises on error.", + py::arg( "command_list" ), + py::arg( "callback" ) = pybind11::none(), + py::arg( "input" ) = std::string(), + py::arg( "timeout" ) = 0 ); + + m.def( "gettext_languages", + &Calamares::Python::gettext_languages, + "Returns list of languages (most to least-specific) for gettext." ); + m.def( "gettext_path", &Calamares::Python::gettext_path, "Returns path for gettext search." ); + + m.def( "mount", + &Calamares::Python::mount, + "Runs the mount utility with the specified parameters.\n" + "Returns the program's exit code, or:\n" + "-1 = QProcess crash\n" + "-2 = QProcess cannot start\n" + "-3 = bad arguments" ); +} + +void +populate_libcalamares( py::module_& m ) +{ + m.doc() = "Calamares API for Python"; + + m.add_object( "ORGANIZATION_NAME", Calamares::Python::String( CALAMARES_ORGANIZATION_NAME ) ); + m.add_object( "ORGANIZATION_DOMAIN", Calamares::Python::String( CALAMARES_ORGANIZATION_DOMAIN ) ); + m.add_object( "APPLICATION_NAME", Calamares::Python::String( CALAMARES_APPLICATION_NAME ) ); + m.add_object( "VERSION", Calamares::Python::String( CALAMARES_VERSION ) ); + m.add_object( "VERSION_SHORT", Calamares::Python::String( CALAMARES_VERSION_SHORT ) ); + + auto utils = m.def_submodule( "utils", "Calamares Utility API for Python" ); + populate_utils( utils ); + + py::class_< Calamares::Python::JobProxy >( m, "Job" ) + .def_readonly( "module_name", &Calamares::Python::JobProxy::moduleName ) + .def_readonly( "pretty_name", &Calamares::Python::JobProxy::prettyName ) + .def_readonly( "working_path", &Calamares::Python::JobProxy::workingPath ) + .def_readonly( "configuration", &Calamares::Python::JobProxy::configuration ) + .def( "setprogress", &Calamares::Python::JobProxy::setprogress ); + + py::class_< Calamares::Python::GlobalStorageProxy >( m, "GlobalStorage" ) + .def( py::init( []( std::nullptr_t p ) { return new Calamares::Python::GlobalStorageProxy( nullptr ); } ) ) + .def( "contains", &Calamares::Python::GlobalStorageProxy::contains ) + .def( "count", &Calamares::Python::GlobalStorageProxy::count ) + .def( "insert", &Calamares::Python::GlobalStorageProxy::insert ) + .def( "keys", &Calamares::Python::GlobalStorageProxy::keys ) + .def( "remove", &Calamares::Python::GlobalStorageProxy::remove ) + .def( "value", &Calamares::Python::GlobalStorageProxy::value ); +} + } // namespace namespace Calamares @@ -284,3 +375,8 @@ Job::setInjectedPreScript( const char* script ) } // namespace Python } // namespace Calamares + +PYBIND11_MODULE( libcalamares, m ) +{ + populate_libcalamares( m ); +}