From f90bf469dd7f5e916fc4d823623e709e75112167 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Fri, 8 Aug 2014 14:12:53 +0200 Subject: [PATCH] Add chrootOutput/check_chroot_output to libcalamares utils API. --- src/libcalamares/PythonJobApi.cpp | 37 +++++++++++ src/libcalamares/PythonJobApi.h | 9 +++ .../utils/CalamaresUtilsSystem.cpp | 62 ++++++++++++++----- src/libcalamares/utils/CalamaresUtilsSystem.h | 10 +++ 4 files changed, 103 insertions(+), 15 deletions(-) diff --git a/src/libcalamares/PythonJobApi.cpp b/src/libcalamares/PythonJobApi.cpp index d2532b6af..beab1513a 100644 --- a/src/libcalamares/PythonJobApi.cpp +++ b/src/libcalamares/PythonJobApi.cpp @@ -104,6 +104,43 @@ check_chroot_call( const bp::list& args, } +std::string +check_chroot_output( const std::string& command, + const std::string& stdin, + int timeout ) +{ + QString output; + int ec = CalamaresUtils::chrootOutput( QString::fromStdString( command ), + output, + QString::fromStdString( stdin ), + timeout ); + _handle_check_chroot_call_error( ec, QString::fromStdString( command ) ); + return output.toStdString(); +} + + +std::string +check_chroot_output( const bp::list& args, + const std::string& stdin, + int timeout ) +{ + QString output; + QStringList list; + for ( int i = 0; i < bp::len( args ); ++i ) + { + list.append( QString::fromStdString( + bp::extract< std::string >( args[ i ] ) ) ); + } + + int ec = CalamaresUtils::chrootOutput( list, + output, + QString::fromStdString( stdin ), + timeout ); + _handle_check_chroot_call_error( ec, list.join( ' ' ) ); + return output.toStdString(); +} + + int _handle_check_chroot_call_error( int ec, const QString& cmd ) { diff --git a/src/libcalamares/PythonJobApi.h b/src/libcalamares/PythonJobApi.h index 96e0d5efa..46d194dd5 100644 --- a/src/libcalamares/PythonJobApi.h +++ b/src/libcalamares/PythonJobApi.h @@ -50,6 +50,15 @@ int check_chroot_call( const boost::python::list& args, const std::string& stdin = std::string(), int timeout = 0 ); +std::string check_chroot_output( const std::string& command, + const std::string& stdin = std::string(), + int timeout = 0 ); + +std::string check_chroot_output( const boost::python::list& args, + const std::string& stdin = std::string(), + int timeout = 0 ); + + inline int _handle_check_chroot_call_error( int ec, const QString& cmd ); void debug( const std::string& s ); diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.cpp b/src/libcalamares/utils/CalamaresUtilsSystem.cpp index 8f00eda06..ce958a3a8 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.cpp +++ b/src/libcalamares/utils/CalamaresUtilsSystem.cpp @@ -28,10 +28,11 @@ namespace CalamaresUtils { -int mount( const QString& devicePath, - const QString& mountPoint, - const QString& filesystemName, - const QString& options ) +int +mount( const QString& devicePath, + const QString& mountPoint, + const QString& filesystemName, + const QString& options ) { if ( devicePath.isEmpty() || mountPoint.isEmpty() ) return -3; @@ -56,10 +57,38 @@ int mount( const QString& devicePath, return QProcess::execute( program, args ); } -int chrootCall( const QStringList& args, - const QString& stdInput, - int timeoutSec ) +int +chrootCall( const QStringList& args, + const QString& stdInput, + int timeoutSec ) { + QString discard; + return chrootOutput( args, + discard, + stdInput, + timeoutSec ); +} + + +int +chrootCall( const QString& command, + const QString& stdInput, + int timeoutSec ) +{ + return chrootCall( QStringList() = { command }, + stdInput, + timeoutSec ); +} + + +int +chrootOutput( const QStringList& args, + QString& output, + const QString& stdInput, + int timeoutSec ) +{ + output.clear(); + if ( !Calamares::JobQueue::instance() ) return -3; @@ -107,8 +136,7 @@ int chrootCall( const QStringList& args, return -4; } - cLog() << "Output:"; - cLog() << process.readAllStandardOutput(); + output.append( QString::fromLocal8Bit( process.readAllStandardOutput() ).trimmed() ); if ( process.exitStatus() == QProcess::CrashExit ) { @@ -121,14 +149,18 @@ int chrootCall( const QStringList& args, } -int chrootCall( const QString& command, - const QString& stdInput, - int timeoutSec ) +int +chrootOutput( const QString& command, + QString& output, + const QString& stdInput, + int timeoutSec ) { - return chrootCall( QStringList() = { command }, - stdInput, - timeoutSec ); + return chrootOutput( QStringList() = { command }, + output, + stdInput, + timeoutSec ); } + } diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.h b/src/libcalamares/utils/CalamaresUtilsSystem.h index 901329cc6..7b8c551e9 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.h +++ b/src/libcalamares/utils/CalamaresUtilsSystem.h @@ -51,6 +51,16 @@ DLLEXPORT int chrootCall( const QStringList& args, DLLEXPORT int chrootCall( const QString& command, const QString& stdInput = QString(), int timeoutSec = 0 ); + +DLLEXPORT int chrootOutput( const QStringList& args, + QString& output, + const QString& stdInput = QString(), + int timeoutSec = 0 ); + +DLLEXPORT int chrootOutput( const QString& command, + QString& output, + const QString& stdInput = QString(), + int timeoutSec = 0 ); } #endif // CALAMARESUTILSSYSTEM_H