[libcalamares] Refactor Python target_env calls

This is why the whole refactoring started: to get the process output
and exit code in one spot so we can attach the process output
to the (Python exception) CalledProcessError in all the code
paths, not just those that are explicitly looking for output.
main
Adriaan de Groot 8 years ago
parent 6693d91152
commit a0a8ab0048

@ -37,21 +37,21 @@
namespace bp = boost::python; namespace bp = boost::python;
static int static int
_handle_check_target_env_call_error( int ec, const QString& cmd, const QString& output = QString() ) _handle_check_target_env_call_error( const CalamaresUtils::ProcessResult& ec, const QString& cmd )
{ {
if ( !ec ) if ( !ec.first )
return ec; return ec.first;
QString raise = QString( "import subprocess\n" QString raise = QString( "import subprocess\n"
"e = subprocess.CalledProcessError(%1,\"%2\")\n" ) "e = subprocess.CalledProcessError(%1,\"%2\")\n" )
.arg( ec ) .arg( ec.first )
.arg( cmd ); .arg( cmd );
if ( !output.isEmpty() ) if ( !ec.second.isEmpty() )
raise.append( QStringLiteral("e.output = \"\"\"%1\"\"\"\n").arg( output ) ); raise.append( QStringLiteral("e.output = \"\"\"%1\"\"\"\n").arg( ec.second ) );
raise.append("raise e"); raise.append("raise e");
bp::exec( raise.toStdString().c_str() ); bp::exec( raise.toStdString().c_str() );
bp::throw_error_already_set(); bp::throw_error_already_set();
return ec; return ec.first;
} }
namespace CalamaresPython namespace CalamaresPython
@ -83,29 +83,36 @@ _bp_list_to_qstringlist( const bp::list& args )
return list; return list;
} }
int static inline CalamaresUtils::ProcessResult
target_env_call( const std::string& command, _target_env_command(
const std::string& stdin, const QStringList& args,
int timeout ) const std::string& stdin,
int timeout )
{ {
return CalamaresUtils::System::instance()-> return CalamaresUtils::System::instance()->
targetEnvCall( QString::fromStdString( command ), targetEnvCommand( args,
QString(), QString(),
QString::fromStdString( stdin ), QString::fromStdString( stdin ),
timeout ); timeout );
} }
int
target_env_call( const std::string& command,
const std::string& stdin,
int timeout )
{
return _target_env_command(
QStringList{ QString::fromStdString( command ) }, stdin, timeout ).first;
}
int int
target_env_call( const bp::list& args, target_env_call( const bp::list& args,
const std::string& stdin, const std::string& stdin,
int timeout ) int timeout )
{ {
return CalamaresUtils::System::instance()-> return _target_env_command(
targetEnvCall( _bp_list_to_qstringlist( args ), _bp_list_to_qstringlist( args ), stdin, timeout ).first;
QString(),
QString::fromStdString( stdin ),
timeout );
} }
@ -114,7 +121,8 @@ check_target_env_call( const std::string& command,
const std::string& stdin, const std::string& stdin,
int timeout ) int timeout )
{ {
int ec = target_env_call( command, stdin, timeout ); auto ec = _target_env_command(
QStringList{ QString::fromStdString( command ) }, stdin, timeout );
return _handle_check_target_env_call_error( ec, QString::fromStdString( command ) ); return _handle_check_target_env_call_error( ec, QString::fromStdString( command ) );
} }
@ -124,9 +132,9 @@ check_target_env_call( const bp::list& args,
const std::string& stdin, const std::string& stdin,
int timeout ) int timeout )
{ {
int ec = target_env_call( args, stdin, timeout ); auto ec = _target_env_command( _bp_list_to_qstringlist( args ), stdin, timeout );
if ( !ec ) if ( !ec.first )
return ec; return ec.first;
QStringList failedCmdList = _bp_list_to_qstringlist( args ); QStringList failedCmdList = _bp_list_to_qstringlist( args );
return _handle_check_target_env_call_error( ec, failedCmdList.join( ' ' ) ); return _handle_check_target_env_call_error( ec, failedCmdList.join( ' ' ) );
@ -138,15 +146,10 @@ check_target_env_output( const std::string& command,
const std::string& stdin, const std::string& stdin,
int timeout ) int timeout )
{ {
QString output; auto ec = _target_env_command(
int ec = CalamaresUtils::System::instance()-> QStringList{ QString::fromStdString( command ) }, stdin, timeout );
targetEnvOutput( QString::fromStdString( command ),
output,
QString(),
QString::fromStdString( stdin ),
timeout );
_handle_check_target_env_call_error( ec, QString::fromStdString( command ) ); _handle_check_target_env_call_error( ec, QString::fromStdString( command ) );
return output.toStdString(); return ec.second.toStdString();
} }
@ -155,16 +158,11 @@ check_target_env_output( const bp::list& args,
const std::string& stdin, const std::string& stdin,
int timeout ) int timeout )
{ {
QString output;
QStringList list = _bp_list_to_qstringlist( args ); QStringList list = _bp_list_to_qstringlist( args );
int ec = CalamaresUtils::System::instance()-> auto ec = _target_env_command(
targetEnvOutput( list, list, stdin, timeout );
output,
QString(),
QString::fromStdString( stdin ),
timeout );
_handle_check_target_env_call_error( ec, list.join( ' ' ) ); _handle_check_target_env_call_error( ec, list.join( ' ' ) );
return output.toStdString(); return ec.second.toStdString();
} }
void void

Loading…
Cancel
Save