From 3120abbce8c6c4b3e5021660ce60d80ef81dd504 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 3 Nov 2021 12:23:39 +0100 Subject: [PATCH] [libcalamares] Introduce host_env_process_output, too - run commands consistently, with optional output-processing, in host or target; - raises exception on error, like the check_* functions. --- src/libcalamares/PythonJob.cpp | 7 +++++- src/libcalamares/PythonJobApi.cpp | 38 ++++++++++++++++++++++++++++--- src/libcalamares/PythonJobApi.h | 2 ++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index cba190287..8d00ba80f 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -139,10 +139,15 @@ BOOST_PYTHON_MODULE( libcalamares ) "Returns the program's standard output, and raises a " "subprocess.CalledProcessError if something went wrong." ) ); bp::def( "target_env_process_output", - static_cast< int ( * )( const bp::list&, bp::object& ) >( &CalamaresPython::target_env_process_output ), + &CalamaresPython::target_env_process_output, bp::args( "command", "callback" ), "Runs the specified command in the target system, and " "calls the callback function with each line of output." ); + bp::def( "host_env_process_output", + &CalamaresPython::host_env_process_output, + bp::args( "command", "callback" ), + "Runs the specified command in the host system, and " + "calls the callback function with each line of output." ); bp::def( "obscure", &CalamaresPython::obscure, diff --git a/src/libcalamares/PythonJobApi.cpp b/src/libcalamares/PythonJobApi.cpp index 7fb8b2af1..37a06f247 100644 --- a/src/libcalamares/PythonJobApi.cpp +++ b/src/libcalamares/PythonJobApi.cpp @@ -17,6 +17,7 @@ #include "utils/CalamaresUtilsSystem.h" #include "utils/Logger.h" #include "utils/RAII.h" +#include "utils/Runner.h" #include "utils/String.h" #include @@ -171,14 +172,45 @@ PythonJobInterface::setprogress( qreal progress ) } } +static inline int +_process_output( Calamares::Utils::RunLocation location, + const boost::python::list& args, + boost::python::object& callback ) +{ + Calamares::Utils::Runner r( _bp_list_to_qstringlist( args ) ); + r.setLocation( location ); + if ( !callback.is_none() ) + { + r.enableOutputProcessing(); + QObject::connect( + &r, &decltype( r )::output, [&callback]( const QString& s ) { callback( s.toStdString() ); } ); + } + auto result = r.run(); + + if ( result.getExitCode() ) + { + return _handle_check_target_env_call_error( result, r.executable() ); + } + return 0; +} + int target_env_process_output( const boost::python::list& args, boost::python::object& callback ) { - cWarning() << "target env" << _bp_list_to_qstringlist( args ); - callback( std::string( "derp" ) ); - return 0; + return _process_output( + + Calamares::Utils::RunLocation::RunInTarget, args, callback ); } +int +host_env_process_output( const boost::python::list& args, boost::python::object& callback ) +{ + return _process_output( + + Calamares::Utils::RunLocation::RunInHost, args, callback ); +} + + std::string obscure( const std::string& string ) { diff --git a/src/libcalamares/PythonJobApi.h b/src/libcalamares/PythonJobApi.h index c20dafe72..5108de823 100644 --- a/src/libcalamares/PythonJobApi.h +++ b/src/libcalamares/PythonJobApi.h @@ -44,6 +44,8 @@ check_target_env_output( const boost::python::list& args, const std::string& std int target_env_process_output( const boost::python::list& args, boost::python::object& callback ); +int host_env_process_output( const boost::python::list& args, boost::python::object& callback ); + std::string obscure( const std::string& string ); boost::python::object gettext_path();