[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:
parent
3120abbce8
commit
2800128a8d
@ -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,
|
||||
|
@ -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 );
|
||||
}
|
||||
|
||||
|
||||
|
@ -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 );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user