[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.
This commit is contained in:
Adriaan de Groot 2021-11-03 12:23:39 +01:00
parent e5323ec487
commit 3120abbce8
3 changed files with 43 additions and 4 deletions

View File

@ -139,10 +139,15 @@ BOOST_PYTHON_MODULE( libcalamares )
"Returns the program's standard output, and raises a " "Returns the program's standard output, and raises a "
"subprocess.CalledProcessError if something went wrong." ) ); "subprocess.CalledProcessError if something went wrong." ) );
bp::def( "target_env_process_output", 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" ), bp::args( "command", "callback" ),
"Runs the specified command in the target system, and " "Runs the specified command in the target system, and "
"calls the callback function with each line of output." ); "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", bp::def( "obscure",
&CalamaresPython::obscure, &CalamaresPython::obscure,

View File

@ -17,6 +17,7 @@
#include "utils/CalamaresUtilsSystem.h" #include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "utils/RAII.h" #include "utils/RAII.h"
#include "utils/Runner.h"
#include "utils/String.h" #include "utils/String.h"
#include <QCoreApplication> #include <QCoreApplication>
@ -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 int
target_env_process_output( const boost::python::list& args, boost::python::object& callback ) target_env_process_output( const boost::python::list& args, boost::python::object& callback )
{ {
cWarning() << "target env" << _bp_list_to_qstringlist( args ); return _process_output(
callback( std::string( "derp" ) );
return 0; 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 std::string
obscure( const std::string& string ) obscure( const std::string& string )
{ {

View File

@ -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 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 ); std::string obscure( const std::string& string );
boost::python::object gettext_path(); boost::python::object gettext_path();