Python: add checked and stdout-returning API

This commit is contained in:
Adriaan de Groot 2023-09-19 00:46:23 +02:00
parent 995f6c8ce3
commit d0dd4b765c
2 changed files with 41 additions and 18 deletions

View file

@ -263,6 +263,28 @@ _add_localedirs( QStringList& pathList, const QString& candidate )
}
}
int
raise_on_error( const Calamares::ProcessResult& ec, const QStringList& commandList )
{
if ( ec.first == 0 )
{
return 0;
}
QString raise = QString( "import subprocess\n"
"e = subprocess.CalledProcessError(%1,\"%2\")\n" )
.arg( ec.first )
.arg( commandList.join( ' ' ) );
if ( !ec.second.isEmpty() )
{
raise.append( QStringLiteral( "e.output = \"\"\"%1\"\"\"\n" ).arg( ec.second ) );
}
raise.append( "raise e" );
py::exec( raise.toStdString() );
py::error_already_set();
return ec.first;
}
} // namespace
/** @namespace
@ -386,26 +408,20 @@ check_target_env_call( const List& args, const std::string& input, int timeout )
const auto commandList = stringListFromPyList( args );
auto ec = Calamares::System::instance()->targetEnvCommand(
commandList, QString(), QString::fromStdString( input ), std::chrono::seconds( timeout ) );
if ( ec.first == 0 )
{
return 0;
}
QString raise = QString( "import subprocess\n"
"e = subprocess.CalledProcessError(%1,\"%2\")\n" )
.arg( ec.first )
.arg( commandList.join( ' ' ) );
if ( !ec.second.isEmpty() )
{
raise.append( QStringLiteral( "e.output = \"\"\"%1\"\"\"\n" ).arg( ec.second ) );
}
raise.append( "raise e" );
py::exec( raise.toStdString() );
py::error_already_set();
return ec.first;
return raise_on_error( ec, commandList );
}
std::string
check_target_env_output( const List& args, const std::string& input, int timeout )
{
const auto commandList = stringListFromPyList( args );
auto ec = Calamares::System::instance()->targetEnvCommand(
commandList, QString(), QString::fromStdString( input ), std::chrono::seconds( timeout ) );
raise_on_error( ec, commandList );
return ec.second.toStdString();
}
JobProxy::JobProxy( Calamares::Python::Job* parent )
: prettyName( parent->prettyName().toStdString() )
, workingPath( parent->workingPath().toStdString() )
@ -523,6 +539,12 @@ PYBIND11_EMBEDDED_MODULE( utils, m )
py::arg( "command_list" ),
py::arg( "input" ) = std::string(),
py::arg( "timeout" ) = 0 );
m.def( "check_target_env_output",
&Calamares::Python::check_target_env_output,
"Runs command in target, returns standard output or raises on error.",
py::arg( "command_list" ),
py::arg( "input" ) = std::string(),
py::arg( "timeout" ) = 0 );
m.def( "gettext_languages",
&Calamares::Python::gettext_languages,

View file

@ -43,6 +43,7 @@ namespace Python __attribute__( ( visibility( "hidden" ) ) )
int target_env_call( const List& args, const std::string& input = std::string(), int timeout = 0 );
int check_target_env_call( const List& args, const std::string& input = std::string(), int timeout = 0 );
std::string check_target_env_output( const List& args, const std::string& input = std::string(), int timeout = 0 );
class Job;