Python: add checked and stdout-returning API
This commit is contained in:
parent
995f6c8ce3
commit
d0dd4b765c
@ -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
|
||||||
|
|
||||||
/** @namespace
|
/** @namespace
|
||||||
@ -386,26 +408,20 @@ check_target_env_call( const List& args, const std::string& input, int timeout )
|
|||||||
const auto commandList = stringListFromPyList( args );
|
const auto commandList = stringListFromPyList( args );
|
||||||
auto ec = Calamares::System::instance()->targetEnvCommand(
|
auto ec = Calamares::System::instance()->targetEnvCommand(
|
||||||
commandList, QString(), QString::fromStdString( input ), std::chrono::seconds( timeout ) );
|
commandList, QString(), QString::fromStdString( input ), std::chrono::seconds( timeout ) );
|
||||||
|
return raise_on_error( ec, commandList );
|
||||||
if ( ec.first == 0 )
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString raise = QString( "import subprocess\n"
|
std::string
|
||||||
"e = subprocess.CalledProcessError(%1,\"%2\")\n" )
|
check_target_env_output( const List& args, const std::string& input, int timeout )
|
||||||
.arg( ec.first )
|
|
||||||
.arg( commandList.join( ' ' ) );
|
|
||||||
if ( !ec.second.isEmpty() )
|
|
||||||
{
|
{
|
||||||
raise.append( QStringLiteral( "e.output = \"\"\"%1\"\"\"\n" ).arg( ec.second ) );
|
const auto commandList = stringListFromPyList( args );
|
||||||
}
|
auto ec = Calamares::System::instance()->targetEnvCommand(
|
||||||
raise.append( "raise e" );
|
commandList, QString(), QString::fromStdString( input ), std::chrono::seconds( timeout ) );
|
||||||
py::exec( raise.toStdString() );
|
raise_on_error( ec, commandList );
|
||||||
py::error_already_set();
|
return ec.second.toStdString();
|
||||||
return ec.first;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JobProxy::JobProxy( Calamares::Python::Job* parent )
|
JobProxy::JobProxy( Calamares::Python::Job* parent )
|
||||||
: prettyName( parent->prettyName().toStdString() )
|
: prettyName( parent->prettyName().toStdString() )
|
||||||
, workingPath( parent->workingPath().toStdString() )
|
, workingPath( parent->workingPath().toStdString() )
|
||||||
@ -523,6 +539,12 @@ PYBIND11_EMBEDDED_MODULE( utils, m )
|
|||||||
py::arg( "command_list" ),
|
py::arg( "command_list" ),
|
||||||
py::arg( "input" ) = std::string(),
|
py::arg( "input" ) = std::string(),
|
||||||
py::arg( "timeout" ) = 0 );
|
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",
|
m.def( "gettext_languages",
|
||||||
&Calamares::Python::gettext_languages,
|
&Calamares::Python::gettext_languages,
|
||||||
|
@ -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 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 );
|
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;
|
class Job;
|
||||||
|
Loading…
Reference in New Issue
Block a user