From 2800128a8d341e3be2ebc42948ecd36ab3063711 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 3 Nov 2021 13:45:15 +0100 Subject: [PATCH] [libcalamares] Expand the output-processing API - support host and target runs - add stdin and timeout values - allow automatic output to list --- src/libcalamares/PythonJob.cpp | 15 ++++++----- src/libcalamares/PythonJobApi.cpp | 45 +++++++++++++++++++++++-------- src/libcalamares/PythonJobApi.h | 10 +++++-- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index 8d00ba80f..afeebbe07 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -36,6 +36,11 @@ BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_output_list_overloads, CalamaresPython::check_target_env_output, 1, 3 ); +BOOST_PYTHON_FUNCTION_OVERLOADS( target_env_process_output_overloads, + CalamaresPython::target_env_process_output, + 1, + 4 ); +BOOST_PYTHON_FUNCTION_OVERLOADS( host_env_process_output_overloads, CalamaresPython::host_env_process_output, 1, 4 ); BOOST_PYTHON_MODULE( libcalamares ) { @@ -140,14 +145,12 @@ BOOST_PYTHON_MODULE( libcalamares ) "subprocess.CalledProcessError if something went wrong." ) ); bp::def( "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." ); + target_env_process_output_overloads( bp::args( "command", "callback", "stdin", "timeout" ), + "Runs the specified @p command in the target system." ) ); 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." ); + host_env_process_output_overloads( bp::args( "command", "callback", "stdin", "timeout" ), + "Runs the specified command in the host system." ) ); bp::def( "obscure", &CalamaresPython::obscure, diff --git a/src/libcalamares/PythonJobApi.cpp b/src/libcalamares/PythonJobApi.cpp index 37a06f247..1713569a4 100644 --- a/src/libcalamares/PythonJobApi.cpp +++ b/src/libcalamares/PythonJobApi.cpp @@ -175,16 +175,37 @@ PythonJobInterface::setprogress( qreal progress ) static inline int _process_output( Calamares::Utils::RunLocation location, const boost::python::list& args, - boost::python::object& callback ) + const boost::python::object& callback, + const std::string& stdin, + int timeout ) { Calamares::Utils::Runner r( _bp_list_to_qstringlist( args ) ); r.setLocation( location ); if ( !callback.is_none() ) { + bp::extract< bp::list > x( callback ); + if ( x.check() ) + { + QObject::connect( &r, &decltype( r )::output, [cb = callback.attr( "append" )]( const QString& s ) { + cb( s.toStdString() ); + } ); + } + else + { + QObject::connect( + &r, &decltype( r )::output, [&callback]( const QString& s ) { callback( s.toStdString() ); } ); + } r.enableOutputProcessing(); - QObject::connect( - &r, &decltype( r )::output, [&callback]( const QString& s ) { callback( s.toStdString() ); } ); } + if ( !stdin.empty() ) + { + r.setInput( QString::fromStdString( stdin ) ); + } + if ( timeout > 0 ) + { + r.setTimeout( std::chrono::seconds( timeout ) ); + } + auto result = r.run(); if ( result.getExitCode() ) @@ -195,19 +216,21 @@ _process_output( Calamares::Utils::RunLocation location, } int -target_env_process_output( const boost::python::list& args, boost::python::object& callback ) +target_env_process_output( const boost::python::list& args, + const boost::python::object& callback, + const std::string& stdin, + int timeout ) { - return _process_output( - - Calamares::Utils::RunLocation::RunInTarget, args, callback ); + return _process_output( Calamares::Utils::RunLocation::RunInTarget, args, callback, stdin, timeout ); } int -host_env_process_output( const boost::python::list& args, boost::python::object& callback ) +host_env_process_output( const boost::python::list& args, + const boost::python::object& callback, + const std::string& stdin, + int timeout ) { - return _process_output( - - Calamares::Utils::RunLocation::RunInHost, args, callback ); + return _process_output( Calamares::Utils::RunLocation::RunInHost, args, callback, stdin, timeout ); } diff --git a/src/libcalamares/PythonJobApi.h b/src/libcalamares/PythonJobApi.h index 5108de823..48bd4f87c 100644 --- a/src/libcalamares/PythonJobApi.h +++ b/src/libcalamares/PythonJobApi.h @@ -42,9 +42,15 @@ check_target_env_output( const std::string& command, const std::string& stdin = std::string check_target_env_output( const boost::python::list& args, const std::string& stdin = std::string(), int timeout = 0 ); -int target_env_process_output( const boost::python::list& args, boost::python::object& callback ); +int target_env_process_output( const boost::python::list& args, + const boost::python::object& callback = boost::python::object(), + const std::string& stdin = std::string(), + int timeout = 0 ); -int host_env_process_output( const boost::python::list& args, boost::python::object& callback ); +int host_env_process_output( const boost::python::list& args, + const boost::python::object& callback = boost::python::object(), + const std::string& stdin = std::string(), + int timeout = 0 ); std::string obscure( const std::string& string );