[libcalamares] Expand the output-processing API

- support host and target runs
- add stdin and timeout values
- allow automatic output to list
This commit is contained in:
Adriaan de Groot 2021-11-03 13:45:15 +01:00
parent 3120abbce8
commit 2800128a8d
3 changed files with 51 additions and 19 deletions

View File

@ -36,6 +36,11 @@ BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_output_list_overloads,
CalamaresPython::check_target_env_output, CalamaresPython::check_target_env_output,
1, 1,
3 ); 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 ) BOOST_PYTHON_MODULE( libcalamares )
{ {
@ -140,14 +145,12 @@ BOOST_PYTHON_MODULE( libcalamares )
"subprocess.CalledProcessError if something went wrong." ) ); "subprocess.CalledProcessError if something went wrong." ) );
bp::def( "target_env_process_output", bp::def( "target_env_process_output",
&CalamaresPython::target_env_process_output, &CalamaresPython::target_env_process_output,
bp::args( "command", "callback" ), target_env_process_output_overloads( bp::args( "command", "callback", "stdin", "timeout" ),
"Runs the specified command in the target system, and " "Runs the specified @p command in the target system." ) );
"calls the callback function with each line of output." );
bp::def( "host_env_process_output", bp::def( "host_env_process_output",
&CalamaresPython::host_env_process_output, &CalamaresPython::host_env_process_output,
bp::args( "command", "callback" ), host_env_process_output_overloads( bp::args( "command", "callback", "stdin", "timeout" ),
"Runs the specified command in the host system, and " "Runs the specified command in the host system." ) );
"calls the callback function with each line of output." );
bp::def( "obscure", bp::def( "obscure",
&CalamaresPython::obscure, &CalamaresPython::obscure,

View File

@ -175,16 +175,37 @@ PythonJobInterface::setprogress( qreal progress )
static inline int static inline int
_process_output( Calamares::Utils::RunLocation location, _process_output( Calamares::Utils::RunLocation location,
const boost::python::list& args, 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 ) ); Calamares::Utils::Runner r( _bp_list_to_qstringlist( args ) );
r.setLocation( location ); r.setLocation( location );
if ( !callback.is_none() ) 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(); 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(); auto result = r.run();
if ( result.getExitCode() ) if ( result.getExitCode() )
@ -195,19 +216,21 @@ _process_output( Calamares::Utils::RunLocation location,
} }
int 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( return _process_output( Calamares::Utils::RunLocation::RunInTarget, args, callback, stdin, timeout );
Calamares::Utils::RunLocation::RunInTarget, args, callback );
} }
int 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( return _process_output( Calamares::Utils::RunLocation::RunInHost, args, callback, stdin, timeout );
Calamares::Utils::RunLocation::RunInHost, args, callback );
} }

View File

@ -42,9 +42,15 @@ check_target_env_output( const std::string& command, const std::string& stdin =
std::string std::string
check_target_env_output( const boost::python::list& args, const std::string& stdin = std::string(), int timeout = 0 ); 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 ); std::string obscure( const std::string& string );