[libcalamares] Apply current coding style to all of libcalamares/

This commit is contained in:
Adriaan de Groot 2019-08-04 22:24:55 +02:00
parent fa676c573e
commit 1afa9c4d08
22 changed files with 406 additions and 433 deletions

View File

@ -24,11 +24,11 @@ namespace Calamares
CppJob::CppJob( QObject* parent ) CppJob::CppJob( QObject* parent )
: Job( parent ) : Job( parent )
{} {
}
CppJob::~CppJob() CppJob::~CppJob() {}
{}
void void
@ -44,4 +44,4 @@ CppJob::setConfigurationMap( const QVariantMap& configurationMap )
Q_UNUSED( configurationMap ) Q_UNUSED( configurationMap )
} }
} } // namespace Calamares

View File

@ -45,6 +45,6 @@ protected:
QString m_instanceKey; QString m_instanceKey;
}; };
} } // namespace Calamares
#endif // CALAMARES_CPPJOB_H #endif // CALAMARES_CPPJOB_H

View File

@ -22,11 +22,11 @@
#include <QtCore/qglobal.h> #include <QtCore/qglobal.h>
#ifndef DLLEXPORT #ifndef DLLEXPORT
# if defined (DLLEXPORT_PRO) #if defined( DLLEXPORT_PRO )
# define DLLEXPORT Q_DECL_EXPORT #define DLLEXPORT Q_DECL_EXPORT
# else #else
# define DLLEXPORT Q_DECL_IMPORT #define DLLEXPORT Q_DECL_IMPORT
# endif #endif
#endif #endif
#endif #endif

View File

@ -21,8 +21,8 @@
#include "JobQueue.h" #include "JobQueue.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "utils/Yaml.h"
#include "utils/Units.h" #include "utils/Units.h"
#include "utils/Yaml.h"
#include <QFile> #include <QFile>
#include <QJsonDocument> #include <QJsonDocument>
@ -40,7 +40,8 @@ namespace bp = boost::python;
using CalamaresUtils::operator""_MiB; using CalamaresUtils::operator""_MiB;
namespace Calamares { namespace Calamares
{
GlobalStorage::GlobalStorage() GlobalStorage::GlobalStorage()
: QObject( nullptr ) : QObject( nullptr )
@ -102,13 +103,15 @@ GlobalStorage::debugDump() const
} }
bool bool
GlobalStorage::save(const QString& filename) GlobalStorage::save( const QString& filename )
{ {
QFile f( filename ); QFile f( filename );
if ( !f.open( QFile::WriteOnly ) ) if ( !f.open( QFile::WriteOnly ) )
{
return false; return false;
}
f.write( QJsonDocument::fromVariant( m ).toJson() ) ; f.write( QJsonDocument::fromVariant( m ).toJson() );
f.close(); f.close();
return true; return true;
} }
@ -118,18 +121,24 @@ GlobalStorage::load( const QString& filename )
{ {
QFile f( filename ); QFile f( filename );
if ( !f.open( QFile::ReadOnly ) ) if ( !f.open( QFile::ReadOnly ) )
{
return false; return false;
}
QJsonParseError e; QJsonParseError e;
QJsonDocument d = QJsonDocument::fromJson( f.read( 1_MiB ), &e ); QJsonDocument d = QJsonDocument::fromJson( f.read( 1_MiB ), &e );
if ( d.isNull() ) if ( d.isNull() )
{
cWarning() << filename << e.errorString(); cWarning() << filename << e.errorString();
}
else if ( !d.isObject() ) else if ( !d.isObject() )
{
cWarning() << filename << "Not suitable JSON."; cWarning() << filename << "Not suitable JSON.";
}
else else
{ {
auto map = d.toVariant().toMap(); auto map = d.toVariant().toMap();
for( auto i = map.constBegin() ; i != map.constEnd() ; ++i ) for ( auto i = map.constBegin(); i != map.constEnd(); ++i )
{ {
insert( i.key(), *i ); insert( i.key(), *i );
} }
@ -150,12 +159,14 @@ GlobalStorage::loadYaml( const QString& filename )
bool ok = false; bool ok = false;
auto gs = CalamaresUtils::loadYaml( filename, &ok ); auto gs = CalamaresUtils::loadYaml( filename, &ok );
if ( ok ) if ( ok )
{
m = gs; m = gs;
}
return ok; return ok;
} }
} // namespace Calamares } // namespace Calamares
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
@ -172,7 +183,7 @@ Calamares::GlobalStorage* GlobalStoragePythonWrapper::s_gs_instance = nullptr;
GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs ) GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs )
: m_gs( gs ? gs : s_gs_instance ) : m_gs( gs ? gs : s_gs_instance )
{ {
if (!m_gs) if ( !m_gs )
{ {
s_gs_instance = new Calamares::GlobalStorage; s_gs_instance = new Calamares::GlobalStorage;
m_gs = s_gs_instance; m_gs = s_gs_instance;
@ -194,11 +205,9 @@ GlobalStoragePythonWrapper::count() const
void void
GlobalStoragePythonWrapper::insert( const std::string& key, GlobalStoragePythonWrapper::insert( const std::string& key, const bp::object& value )
const bp::object& value )
{ {
m_gs->insert( QString::fromStdString( key ), m_gs->insert( QString::fromStdString( key ), CalamaresPython::variantFromPyObject( value ) );
CalamaresPython::variantFromPyObject( value ) );
} }
bp::list bp::list
@ -207,7 +216,9 @@ GlobalStoragePythonWrapper::keys() const
bp::list pyList; bp::list pyList;
const auto keys = m_gs->keys(); const auto keys = m_gs->keys();
for ( const QString& key : keys ) for ( const QString& key : keys )
{
pyList.append( key.toStdString() ); pyList.append( key.toStdString() );
}
return pyList; return pyList;
} }
@ -225,6 +236,6 @@ GlobalStoragePythonWrapper::value( const std::string& key ) const
return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) ); return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) );
} }
} // namespace CalamaresPython } // namespace CalamaresPython
#endif // WITH_PYTHON #endif // WITH_PYTHON

View File

@ -34,8 +34,8 @@ namespace api
class object; class object;
} }
class list; class list;
} } // namespace python
} } // namespace boost
#endif #endif
namespace Calamares namespace Calamares
@ -96,7 +96,7 @@ private:
friend DebugWindow; friend DebugWindow;
}; };
} // namespace Calamares } // namespace Calamares
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
namespace CalamaresPython namespace CalamaresPython
@ -124,7 +124,7 @@ private:
static Calamares::GlobalStorage* s_gs_instance; // See globalStorageInstance() static Calamares::GlobalStorage* s_gs_instance; // See globalStorageInstance()
}; };
} // namespace CalamaresPython } // namespace CalamaresPython
#endif #endif
#endif // CALAMARES_GLOBALSTORAGE_H #endif // CALAMARES_GLOBALSTORAGE_H

View File

@ -84,7 +84,8 @@ JobResult::JobResult( const QString& message, const QString& details, int number
: m_message( message ) : m_message( message )
, m_details( details ) , m_details( details )
, m_number( number ) , m_number( number )
{} {
}
Job::Job( QObject* parent ) Job::Job( QObject* parent )
@ -93,8 +94,7 @@ Job::Job( QObject* parent )
} }
Job::~Job() Job::~Job() {}
{}
qreal qreal
@ -118,4 +118,4 @@ Job::prettyStatusMessage() const
} }
} // namespace Calamares } // namespace Calamares

View File

@ -25,7 +25,8 @@
#include <QObject> #include <QObject>
#include <QSharedPointer> #include <QSharedPointer>
namespace Calamares { namespace Calamares
{
class DLLEXPORT JobResult class DLLEXPORT JobResult
{ {
@ -43,7 +44,7 @@ public:
GenericError = -1, GenericError = -1,
PythonUncaughtException = 1, PythonUncaughtException = 1,
InvalidConfiguration = 2 InvalidConfiguration = 2
} ; };
JobResult( const JobResult& rhs ) = delete; JobResult( const JobResult& rhs ) = delete;
JobResult( JobResult&& rhs ); JobResult( JobResult&& rhs );
@ -102,6 +103,6 @@ private:
using job_ptr = QSharedPointer< Job >; using job_ptr = QSharedPointer< Job >;
using JobList = QList< job_ptr >; using JobList = QList< job_ptr >;
} // namespace Calamares } // namespace Calamares
#endif // CALAMARES_JOB_H #endif // CALAMARES_JOB_H

View File

@ -36,7 +36,8 @@ GoodJob::exec()
JobResult JobResult
FailJob::exec() FailJob::exec()
{ {
return JobResult::error( tr( "Job failed (%1)" ).arg( m_name ), tr( "Programmed job failure was explicitly requested." ) ); return JobResult::error( tr( "Job failed (%1)" ).arg( m_name ),
tr( "Programmed job failure was explicitly requested." ) );
} }
} // namespace } // namespace Calamares

View File

@ -21,7 +21,8 @@
#include "Job.h" #include "Job.h"
namespace Calamares { namespace Calamares
{
/** @brief A Job with a name /** @brief A Job with a name
* *
@ -39,9 +40,10 @@ public:
} }
virtual QString prettyName() const override; virtual QString prettyName() const override;
protected: protected:
const QString m_name; const QString m_name;
} ; };
/// @brief Job does nothing, always succeeds /// @brief Job does nothing, always succeeds
class DLLEXPORT GoodJob : public NamedJob class DLLEXPORT GoodJob : public NamedJob
@ -53,7 +55,7 @@ public:
} }
virtual JobResult exec() override; virtual JobResult exec() override;
} ; };
/// @brief Job does nothing, always fails /// @brief Job does nothing, always fails
@ -66,8 +68,8 @@ public:
} }
virtual JobResult exec() override; virtual JobResult exec() override;
} ; };
} // namespace Calamares } // namespace Calamares
#endif // CALAMARES_JOB_EXAMPLE_H #endif // CALAMARES_JOB_EXAMPLE_H

View File

@ -19,8 +19,8 @@
#include "JobQueue.h" #include "JobQueue.h"
#include "Job.h"
#include "GlobalStorage.h" #include "GlobalStorage.h"
#include "Job.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "CalamaresConfig.h" #include "CalamaresConfig.h"
@ -50,14 +50,14 @@ public:
m_jobs = jobs; m_jobs = jobs;
qreal totalJobsWeight = 0.0; qreal totalJobsWeight = 0.0;
for( auto job : m_jobs ) for ( auto job : m_jobs )
{ {
totalJobsWeight += job->getJobWeight(); totalJobsWeight += job->getJobWeight();
} }
for( auto job : m_jobs ) for ( auto job : m_jobs )
{ {
qreal jobWeight = qreal( job->getJobWeight() / totalJobsWeight ); qreal jobWeight = qreal( job->getJobWeight() / totalJobsWeight );
m_jobWeights.append( jobWeight ) ; m_jobWeights.append( jobWeight );
} }
} }
@ -68,7 +68,7 @@ public:
QString details; QString details;
m_jobIndex = 0; m_jobIndex = 0;
for( auto job : m_jobs ) for ( auto job : m_jobs )
{ {
if ( anyFailed && !job->isEmergency() ) if ( anyFailed && !job->isEmergency() )
{ {
@ -87,12 +87,18 @@ public:
details = result.details(); details = result.details();
} }
if ( !anyFailed ) if ( !anyFailed )
{
++m_jobIndex; ++m_jobIndex;
}
} }
if ( anyFailed ) if ( anyFailed )
{
emitFailed( message, details ); emitFailed( message, details );
}
else else
{
emitProgress(); emitProgress();
}
emitFinished(); emitFinished();
} }
@ -109,49 +115,39 @@ private:
jobPercent = qBound( qreal( 0 ), jobPercent, qreal( 1 ) ); jobPercent = qBound( qreal( 0 ), jobPercent, qreal( 1 ) );
int jobCount = m_jobs.size(); int jobCount = m_jobs.size();
QString message = m_jobIndex < jobCount QString message = m_jobIndex < jobCount ? m_jobs.at( m_jobIndex )->prettyStatusMessage() : tr( "Done" );
? m_jobs.at( m_jobIndex )->prettyStatusMessage()
: tr( "Done" );
qreal cumulativeProgress = 0.0; qreal cumulativeProgress = 0.0;
for( auto jobWeight : m_jobWeights.mid( 0, m_jobIndex ) ) for ( auto jobWeight : m_jobWeights.mid( 0, m_jobIndex ) )
{ {
cumulativeProgress += jobWeight; cumulativeProgress += jobWeight;
} }
qreal percent = m_jobIndex < jobCount qreal percent
? cumulativeProgress + ( ( m_jobWeights.at( m_jobIndex ) ) * jobPercent ) = m_jobIndex < jobCount ? cumulativeProgress + ( ( m_jobWeights.at( m_jobIndex ) ) * jobPercent ) : 1.0;
: 1.0;
if (m_jobIndex < jobCount) if ( m_jobIndex < jobCount )
{ {
cDebug(Logger::LOGVERBOSE) << "[JOBQUEUE]: Progress for Job[" << m_jobIndex << "]: " << ( jobPercent * 100 ) << "% completed"; cDebug( Logger::LOGVERBOSE ) << "[JOBQUEUE]: Progress for Job[" << m_jobIndex
cDebug(Logger::LOGVERBOSE) << "[JOBQUEUE]: Progress Overall: " << ( cumulativeProgress * 100 ) << "% (accumulated) + " << "]: " << ( jobPercent * 100 ) << "% completed";
<< ( ( ( m_jobWeights.at( m_jobIndex ) ) * jobPercent ) * 100 ) << "% (this job) = " cDebug( Logger::LOGVERBOSE ) << "[JOBQUEUE]: Progress Overall: " << ( cumulativeProgress * 100 )
<< ( percent * 100 ) << "% (total)"; << "% (accumulated) + "
<< ( ( ( m_jobWeights.at( m_jobIndex ) ) * jobPercent ) * 100 )
<< "% (this job) = " << ( percent * 100 ) << "% (total)";
} }
QMetaObject::invokeMethod( m_queue, "progress", Qt::QueuedConnection, QMetaObject::invokeMethod(
Q_ARG( qreal, percent ), m_queue, "progress", Qt::QueuedConnection, Q_ARG( qreal, percent ), Q_ARG( QString, message ) );
Q_ARG( QString, message )
);
} }
void emitFailed( const QString& message, const QString& details ) void emitFailed( const QString& message, const QString& details )
{ {
QMetaObject::invokeMethod( m_queue, "failed", Qt::QueuedConnection, QMetaObject::invokeMethod(
Q_ARG( QString, message ), m_queue, "failed", Qt::QueuedConnection, Q_ARG( QString, message ), Q_ARG( QString, details ) );
Q_ARG( QString, details )
);
} }
void emitFinished() void emitFinished() { QMetaObject::invokeMethod( m_queue, "finished", Qt::QueuedConnection ); }
{
QMetaObject::invokeMethod( m_queue, "finished", Qt::QueuedConnection );
}
}; };
JobThread::~JobThread() JobThread::~JobThread() {}
{
}
JobQueue* JobQueue::s_instance = nullptr; JobQueue* JobQueue::s_instance = nullptr;
@ -186,8 +182,10 @@ JobQueue::~JobQueue()
if ( m_thread->isRunning() ) if ( m_thread->isRunning() )
{ {
m_thread->terminate(); m_thread->terminate();
if ( !m_thread->wait(300) ) if ( !m_thread->wait( 300 ) )
{
cError() << "Could not terminate job thread (expect a crash now)."; cError() << "Could not terminate job thread (expect a crash now).";
}
delete m_thread; delete m_thread;
} }
@ -222,4 +220,4 @@ JobQueue::enqueue( const JobList& jobs )
emit queueChanged( m_jobs ); emit queueChanged( m_jobs );
} }
} // namespace Calamares } // namespace Calamares

View File

@ -59,6 +59,6 @@ private:
GlobalStorage* m_storage; GlobalStorage* m_storage;
}; };
} } // namespace Calamares
#endif // CALAMARES_JOBQUEUE_H #endif // CALAMARES_JOBQUEUE_H

View File

@ -22,11 +22,11 @@
#include <QtCore/qglobal.h> #include <QtCore/qglobal.h>
#ifndef PLUGINDLLEXPORT #ifndef PLUGINDLLEXPORT
# if defined (PLUGINDLLEXPORT_PRO) #if defined( PLUGINDLLEXPORT_PRO )
# define PLUGINDLLEXPORT Q_DECL_EXPORT #define PLUGINDLLEXPORT Q_DECL_EXPORT
# else #else
# define PLUGINDLLEXPORT Q_DECL_IMPORT #define PLUGINDLLEXPORT Q_DECL_IMPORT
# endif #endif
#endif #endif
#endif #endif

View File

@ -25,7 +25,8 @@
#include <QDir> #include <QDir>
#include <QProcess> #include <QProcess>
namespace Calamares { namespace Calamares
{
ProcessJob::ProcessJob( const QString& command, ProcessJob::ProcessJob( const QString& command,
@ -38,27 +39,24 @@ ProcessJob::ProcessJob( const QString& command,
, m_workingPath( workingPath ) , m_workingPath( workingPath )
, m_runInChroot( runInChroot ) , m_runInChroot( runInChroot )
, m_timeoutSec( secondsTimeout ) , m_timeoutSec( secondsTimeout )
{} {
}
ProcessJob::~ProcessJob() ProcessJob::~ProcessJob() {}
{}
QString QString
ProcessJob::prettyName() const ProcessJob::prettyName() const
{ {
return ( m_runInChroot ? tr( "Run command '%1' in target system." ) : tr( " Run command '%1'." ) ) return ( m_runInChroot ? tr( "Run command '%1' in target system." ) : tr( " Run command '%1'." ) ).arg( m_command );
.arg( m_command );
} }
QString QString
ProcessJob::prettyStatusMessage() const ProcessJob::prettyStatusMessage() const
{ {
return tr( "Running command %1 %2" ) return tr( "Running command %1 %2" ).arg( m_command ).arg( m_runInChroot ? "in chroot." : " ." );
.arg( m_command )
.arg( m_runInChroot ? "in chroot." : " ." );
} }
@ -68,20 +66,16 @@ ProcessJob::exec()
using CalamaresUtils::System; using CalamaresUtils::System;
if ( m_runInChroot ) if ( m_runInChroot )
return CalamaresUtils::System::instance()-> return CalamaresUtils::System::instance()
targetEnvCommand( { m_command }, ->targetEnvCommand( { m_command }, m_workingPath, QString(), m_timeoutSec )
m_workingPath, .explainProcess( m_command, m_timeoutSec );
QString(),
m_timeoutSec )
.explainProcess( m_command, m_timeoutSec );
else else
return return System::runCommand( System::RunLocation::RunInHost,
System::runCommand( System::RunLocation::RunInHost, { "/bin/sh", "-c", m_command },
{ "/bin/sh", "-c", m_command }, m_workingPath,
m_workingPath, QString(),
QString(), m_timeoutSec )
m_timeoutSec )
.explainProcess( m_command, m_timeoutSec ); .explainProcess( m_command, m_timeoutSec );
} }
} // namespace Calamares } // namespace Calamares

View File

@ -24,7 +24,8 @@
#include <chrono> #include <chrono>
namespace Calamares { namespace Calamares
{
class ProcessJob : public Job class ProcessJob : public Job
{ {
@ -33,7 +34,7 @@ public:
explicit ProcessJob( const QString& command, explicit ProcessJob( const QString& command,
const QString& workingPath, const QString& workingPath,
bool runInChroot = false, bool runInChroot = false,
std::chrono::seconds secondsTimeout = std::chrono::seconds(30), std::chrono::seconds secondsTimeout = std::chrono::seconds( 30 ),
QObject* parent = nullptr ); QObject* parent = nullptr );
virtual ~ProcessJob() override; virtual ~ProcessJob() override;
@ -48,6 +49,6 @@ private:
std::chrono::seconds m_timeoutSec; std::chrono::seconds m_timeoutSec;
}; };
} // namespace Calamares } // namespace Calamares
#endif // CALAMARES_PROCESSJOB_H #endif // CALAMARES_PROCESSJOB_H

View File

@ -72,25 +72,39 @@ variantFromPyObject( const boost::python::object& pyObject )
{ {
std::string pyType = bp::extract< std::string >( pyObject.attr( "__class__" ).attr( "__name__" ) ); std::string pyType = bp::extract< std::string >( pyObject.attr( "__class__" ).attr( "__name__" ) );
if ( pyType == "dict" ) if ( pyType == "dict" )
{
return variantMapFromPyDict( bp::extract< bp::dict >( pyObject ) ); return variantMapFromPyDict( bp::extract< bp::dict >( pyObject ) );
}
else if ( pyType == "list" ) else if ( pyType == "list" )
{
return variantListFromPyList( bp::extract< bp::list >( pyObject ) ); return variantListFromPyList( bp::extract< bp::list >( pyObject ) );
}
else if ( pyType == "int" ) else if ( pyType == "int" )
{
return QVariant( bp::extract< int >( pyObject ) ); return QVariant( bp::extract< int >( pyObject ) );
}
else if ( pyType == "float" ) else if ( pyType == "float" )
{
return QVariant( bp::extract< double >( pyObject ) ); return QVariant( bp::extract< double >( pyObject ) );
}
else if ( pyType == "str" ) else if ( pyType == "str" )
{
return QVariant( QString::fromStdString( bp::extract< std::string >( pyObject ) ) ); return QVariant( QString::fromStdString( bp::extract< std::string >( pyObject ) ) );
}
else if ( pyType == "bool" ) else if ( pyType == "bool" )
{
return QVariant( bp::extract< bool >( pyObject ) ); return QVariant( bp::extract< bool >( pyObject ) );
}
else else
{
return QVariant(); return QVariant();
}
} }
@ -99,7 +113,9 @@ variantListToPyList( const QVariantList& variantList )
{ {
bp::list pyList; bp::list pyList;
for ( const QVariant& variant : variantList ) for ( const QVariant& variant : variantList )
{
pyList.append( variantToPyObject( variant ) ); pyList.append( variantToPyObject( variant ) );
}
return pyList; return pyList;
} }
@ -109,7 +125,9 @@ variantListFromPyList( const boost::python::list& pyList )
{ {
QVariantList list; QVariantList list;
for ( int i = 0; i < bp::len( pyList ); ++i ) for ( int i = 0; i < bp::len( pyList ); ++i )
{
list.append( variantFromPyObject( pyList[ i ] ) ); list.append( variantFromPyObject( pyList[ i ] ) );
}
return list; return list;
} }
@ -119,7 +137,9 @@ variantMapToPyDict( const QVariantMap& variantMap )
{ {
bp::dict pyDict; bp::dict pyDict;
for ( auto it = variantMap.constBegin(); it != variantMap.constEnd(); ++it ) for ( auto it = variantMap.constBegin(); it != variantMap.constEnd(); ++it )
{
pyDict[ it.key().toStdString() ] = variantToPyObject( it.value() ); pyDict[ it.key().toStdString() ] = variantToPyObject( it.value() );
}
return pyDict; return pyDict;
} }
@ -152,7 +172,9 @@ variantHashToPyDict( const QVariantHash& variantHash )
{ {
bp::dict pyDict; bp::dict pyDict;
for ( auto it = variantHash.constBegin(); it != variantHash.constEnd(); ++it ) for ( auto it = variantHash.constBegin(); it != variantHash.constEnd(); ++it )
{
pyDict[ it.key().toStdString() ] = variantToPyObject( it.value() ); pyDict[ it.key().toStdString() ] = variantToPyObject( it.value() );
}
return pyDict; return pyDict;
} }
@ -181,17 +203,21 @@ variantHashFromPyDict( const boost::python::dict& pyDict )
} }
Helper* Helper::s_instance = nullptr; Helper* Helper::s_instance = nullptr;
static inline void add_if_lib_exists( const QDir& dir, const char* name, QStringList& list ) static inline void
add_if_lib_exists( const QDir& dir, const char* name, QStringList& list )
{ {
if ( ! ( dir.exists() && dir.isReadable() ) ) if ( !( dir.exists() && dir.isReadable() ) )
{
return; return;
}
QFileInfo fi( dir.absoluteFilePath( name ) ); QFileInfo fi( dir.absoluteFilePath( name ) );
if ( fi.exists() && fi.isReadable() ) if ( fi.exists() && fi.isReadable() )
{
list.append( fi.dir().absolutePath() ); list.append( fi.dir().absolutePath() );
}
} }
Helper::Helper( QObject* parent ) Helper::Helper( QObject* parent )
@ -201,7 +227,9 @@ Helper::Helper( QObject* parent )
if ( !s_instance ) if ( !s_instance )
{ {
if ( !Py_IsInitialized() ) if ( !Py_IsInitialized() )
{
Py_Initialize(); Py_Initialize();
}
m_mainModule = bp::import( "__main__" ); m_mainModule = bp::import( "__main__" );
m_mainNamespace = m_mainModule.attr( "__dict__" ); m_mainNamespace = m_mainModule.attr( "__dict__" );
@ -209,8 +237,7 @@ Helper::Helper( QObject* parent )
// If we're running from the build dir // If we're running from the build dir
add_if_lib_exists( QDir::current(), "libcalamares.so", m_pythonPaths ); add_if_lib_exists( QDir::current(), "libcalamares.so", m_pythonPaths );
QDir calaPythonPath( CalamaresUtils::systemLibDir().absolutePath() + QDir calaPythonPath( CalamaresUtils::systemLibDir().absolutePath() + QDir::separator() + "calamares" );
QDir::separator() + "calamares" );
add_if_lib_exists( calaPythonPath, "libcalamares.so", m_pythonPaths ); add_if_lib_exists( calaPythonPath, "libcalamares.so", m_pythonPaths );
bp::object sys = bp::import( "sys" ); bp::object sys = bp::import( "sys" );
@ -251,7 +278,7 @@ Helper::createCleanNamespace()
QString QString
Helper::handleLastError() Helper::handleLastError()
{ {
PyObject* type = nullptr, *val = nullptr, *traceback_p = nullptr; PyObject *type = nullptr, *val = nullptr, *traceback_p = nullptr;
PyErr_Fetch( &type, &val, &traceback_p ); PyErr_Fetch( &type, &val, &traceback_p );
Logger::CDebug debug; Logger::CDebug debug;
@ -264,10 +291,14 @@ Helper::handleLastError()
bp::str pystr( h_type ); bp::str pystr( h_type );
bp::extract< std::string > extracted( pystr ); bp::extract< std::string > extracted( pystr );
if ( extracted.check() ) if ( extracted.check() )
{
typeMsg = QString::fromStdString( extracted() ).trimmed(); typeMsg = QString::fromStdString( extracted() ).trimmed();
}
if ( typeMsg.isEmpty() ) if ( typeMsg.isEmpty() )
{
typeMsg = tr( "Unknown exception type" ); typeMsg = tr( "Unknown exception type" );
}
debug << typeMsg << '\n'; debug << typeMsg << '\n';
} }
@ -278,10 +309,14 @@ Helper::handleLastError()
bp::str pystr( h_val ); bp::str pystr( h_val );
bp::extract< std::string > extracted( pystr ); bp::extract< std::string > extracted( pystr );
if ( extracted.check() ) if ( extracted.check() )
{
valMsg = QString::fromStdString( extracted() ).trimmed(); valMsg = QString::fromStdString( extracted() ).trimmed();
}
if ( valMsg.isEmpty() ) if ( valMsg.isEmpty() )
{
valMsg = tr( "unparseable Python error" ); valMsg = tr( "unparseable Python error" );
}
// Special-case: CalledProcessError has an attribute "output" with the command output, // Special-case: CalledProcessError has an attribute "output" with the command output,
// add that to the printed message. // add that to the printed message.
@ -318,22 +353,32 @@ Helper::handleLastError()
bp::object pystr( bp::str( "\n" ).join( tb_list ) ); bp::object pystr( bp::str( "\n" ).join( tb_list ) );
bp::extract< std::string > extracted( pystr ); bp::extract< std::string > extracted( pystr );
if ( extracted.check() ) if ( extracted.check() )
{
tbMsg = QString::fromStdString( extracted() ).trimmed(); tbMsg = QString::fromStdString( extracted() ).trimmed();
}
if ( tbMsg.isEmpty() ) if ( tbMsg.isEmpty() )
{
tbMsg = tr( "unparseable Python traceback" ); tbMsg = tr( "unparseable Python traceback" );
}
debug << tbMsg << '\n'; debug << tbMsg << '\n';
} }
if ( typeMsg.isEmpty() && valMsg.isEmpty() && tbMsg.isEmpty() ) if ( typeMsg.isEmpty() && valMsg.isEmpty() && tbMsg.isEmpty() )
{
return tr( "Unfetchable Python error." ); return tr( "Unfetchable Python error." );
}
QStringList msgList; QStringList msgList;
if ( !typeMsg.isEmpty() ) if ( !typeMsg.isEmpty() )
{
msgList.append( QString( "<strong>%1</strong>" ).arg( typeMsg.toHtmlEscaped() ) ); msgList.append( QString( "<strong>%1</strong>" ).arg( typeMsg.toHtmlEscaped() ) );
}
if ( !valMsg.isEmpty() ) if ( !valMsg.isEmpty() )
{
msgList.append( valMsg.toHtmlEscaped() ); msgList.append( valMsg.toHtmlEscaped() );
}
if ( !tbMsg.isEmpty() ) if ( !tbMsg.isEmpty() )
{ {
@ -346,4 +391,4 @@ Helper::handleLastError()
} }
} // namespace CalamaresPython } // namespace CalamaresPython

View File

@ -32,17 +32,17 @@
namespace CalamaresPython namespace CalamaresPython
{ {
boost::python::object variantToPyObject( const QVariant& variant ); boost::python::object variantToPyObject( const QVariant& variant );
QVariant variantFromPyObject( const boost::python::object& pyObject ); QVariant variantFromPyObject( const boost::python::object& pyObject );
boost::python::list variantListToPyList( const QVariantList& variantList ); boost::python::list variantListToPyList( const QVariantList& variantList );
QVariantList variantListFromPyList( const boost::python::list& pyList ); QVariantList variantListFromPyList( const boost::python::list& pyList );
boost::python::dict variantMapToPyDict( const QVariantMap& variantMap ); boost::python::dict variantMapToPyDict( const QVariantMap& variantMap );
QVariantMap variantMapFromPyDict( const boost::python::dict& pyDict ); QVariantMap variantMapFromPyDict( const boost::python::dict& pyDict );
boost::python::dict variantHashToPyDict( const QVariantHash& variantHash ); boost::python::dict variantHashToPyDict( const QVariantHash& variantHash );
QVariantHash variantHashFromPyDict( const boost::python::dict& pyDict ); QVariantHash variantHashFromPyDict( const boost::python::dict& pyDict );
class Helper : public QObject class Helper : public QObject
@ -66,6 +66,6 @@ private:
QStringList m_pythonPaths; QStringList m_pythonPaths;
}; };
} // namespace Calamares } // namespace CalamaresPython
#endif // CALAMARES_PYTHONJOBHELPER_H #endif // CALAMARES_PYTHONJOBHELPER_H

View File

@ -19,10 +19,10 @@
#include "PythonJob.h" #include "PythonJob.h"
#include "PythonHelper.h"
#include "utils/Logger.h"
#include "GlobalStorage.h" #include "GlobalStorage.h"
#include "JobQueue.h" #include "JobQueue.h"
#include "PythonHelper.h"
#include "utils/Logger.h"
#include <QDir> #include <QDir>
@ -35,27 +35,19 @@
namespace bp = boost::python; namespace bp = boost::python;
BOOST_PYTHON_FUNCTION_OVERLOADS( mount_overloads, BOOST_PYTHON_FUNCTION_OVERLOADS( mount_overloads, CalamaresPython::mount, 2, 4 );
CalamaresPython::mount, BOOST_PYTHON_FUNCTION_OVERLOADS( target_env_call_str_overloads, CalamaresPython::target_env_call, 1, 3 );
2, 4 ); BOOST_PYTHON_FUNCTION_OVERLOADS( target_env_call_list_overloads, CalamaresPython::target_env_call, 1, 3 );
BOOST_PYTHON_FUNCTION_OVERLOADS( target_env_call_str_overloads, BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_call_str_overloads, CalamaresPython::check_target_env_call, 1, 3 );
CalamaresPython::target_env_call, BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_call_list_overloads, CalamaresPython::check_target_env_call, 1, 3 );
1, 3 );
BOOST_PYTHON_FUNCTION_OVERLOADS( target_env_call_list_overloads,
CalamaresPython::target_env_call,
1, 3 );
BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_call_str_overloads,
CalamaresPython::check_target_env_call,
1, 3 );
BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_call_list_overloads,
CalamaresPython::check_target_env_call,
1, 3 );
BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_output_str_overloads, BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_output_str_overloads,
CalamaresPython::check_target_env_output, CalamaresPython::check_target_env_output,
1, 3 ); 1,
3 );
BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_output_list_overloads, BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_output_list_overloads,
CalamaresPython::check_target_env_output, CalamaresPython::check_target_env_output,
1, 3 ); 1,
3 );
BOOST_PYTHON_MODULE( libcalamares ) BOOST_PYTHON_MODULE( libcalamares )
{ {
bp::object package = bp::scope(); bp::object package = bp::scope();
@ -68,25 +60,24 @@ BOOST_PYTHON_MODULE( libcalamares )
bp::scope().attr( "VERSION_SHORT" ) = CALAMARES_VERSION_SHORT; bp::scope().attr( "VERSION_SHORT" ) = CALAMARES_VERSION_SHORT;
bp::class_< CalamaresPython::PythonJobInterface >( "Job", bp::init< Calamares::PythonJob* >() ) bp::class_< CalamaresPython::PythonJobInterface >( "Job", bp::init< Calamares::PythonJob* >() )
.def_readonly( "module_name", &CalamaresPython::PythonJobInterface::moduleName ) .def_readonly( "module_name", &CalamaresPython::PythonJobInterface::moduleName )
.def_readonly( "pretty_name", &CalamaresPython::PythonJobInterface::prettyName ) .def_readonly( "pretty_name", &CalamaresPython::PythonJobInterface::prettyName )
.def_readonly( "working_path", &CalamaresPython::PythonJobInterface::workingPath ) .def_readonly( "working_path", &CalamaresPython::PythonJobInterface::workingPath )
.def_readonly( "configuration", &CalamaresPython::PythonJobInterface::configuration ) .def_readonly( "configuration", &CalamaresPython::PythonJobInterface::configuration )
.def( .def( "setprogress",
"setprogress", &CalamaresPython::PythonJobInterface::setprogress,
&CalamaresPython::PythonJobInterface::setprogress, bp::args( "progress" ),
bp::args( "progress" ), "Reports the progress status of this job to Calamares, "
"Reports the progress status of this job to Calamares, " "as a real number between 0 and 1." );
"as a real number between 0 and 1."
);
bp::class_< CalamaresPython::GlobalStoragePythonWrapper >( "GlobalStorage", bp::init< Calamares::GlobalStorage* >() ) bp::class_< CalamaresPython::GlobalStoragePythonWrapper >( "GlobalStorage",
.def( "contains", &CalamaresPython::GlobalStoragePythonWrapper::contains ) bp::init< Calamares::GlobalStorage* >() )
.def( "count", &CalamaresPython::GlobalStoragePythonWrapper::count ) .def( "contains", &CalamaresPython::GlobalStoragePythonWrapper::contains )
.def( "insert", &CalamaresPython::GlobalStoragePythonWrapper::insert ) .def( "count", &CalamaresPython::GlobalStoragePythonWrapper::count )
.def( "keys", &CalamaresPython::GlobalStoragePythonWrapper::keys ) .def( "insert", &CalamaresPython::GlobalStoragePythonWrapper::insert )
.def( "remove", &CalamaresPython::GlobalStoragePythonWrapper::remove ) .def( "keys", &CalamaresPython::GlobalStoragePythonWrapper::keys )
.def( "value", &CalamaresPython::GlobalStoragePythonWrapper::value ); .def( "remove", &CalamaresPython::GlobalStoragePythonWrapper::remove )
.def( "value", &CalamaresPython::GlobalStoragePythonWrapper::value );
// libcalamares.utils submodule starts here // libcalamares.utils submodule starts here
bp::object utilsModule( bp::handle<>( bp::borrowed( PyImport_AddModule( "libcalamares.utils" ) ) ) ); bp::object utilsModule( bp::handle<>( bp::borrowed( PyImport_AddModule( "libcalamares.utils" ) ) ) );
@ -95,151 +86,88 @@ BOOST_PYTHON_MODULE( libcalamares )
Q_UNUSED( utilsScope ) Q_UNUSED( utilsScope )
bp::def( bp::def(
"debug", "debug", &CalamaresPython::debug, bp::args( "s" ), "Writes the given string to the Calamares debug stream." );
&CalamaresPython::debug, bp::def( "warning",
bp::args( "s" ), &CalamaresPython::warning,
"Writes the given string to the Calamares debug stream." bp::args( "s" ),
); "Writes the given string to the Calamares warning stream." );
bp::def(
"warning",
&CalamaresPython::warning,
bp::args( "s" ),
"Writes the given string to the Calamares warning stream."
);
bp::def( bp::def( "mount",
"mount", &CalamaresPython::mount,
&CalamaresPython::mount, mount_overloads( bp::args( "device_path", "mount_point", "filesystem_name", "options" ),
mount_overloads( "Runs the mount utility with the specified parameters.\n"
bp::args( "device_path", "Returns the program's exit code, or:\n"
"mount_point", "-1 = QProcess crash\n"
"filesystem_name", "-2 = QProcess cannot start\n"
"options" ), "-3 = bad arguments" ) );
"Runs the mount utility with the specified parameters.\n"
"Returns the program's exit code, or:\n"
"-1 = QProcess crash\n"
"-2 = QProcess cannot start\n"
"-3 = bad arguments"
)
);
bp::def( bp::def(
"target_env_call", "target_env_call",
static_cast< int (*)( const std::string&, static_cast< int ( * )( const std::string&, const std::string&, int ) >( &CalamaresPython::target_env_call ),
const std::string&, target_env_call_str_overloads( bp::args( "command", "stdin", "timeout" ),
int ) >( &CalamaresPython::target_env_call ), "Runs the specified command in the chroot of the target system.\n"
target_env_call_str_overloads( "Returns the program's exit code, or:\n"
bp::args( "command", "-1 = QProcess crash\n"
"stdin", "-2 = QProcess cannot start\n"
"timeout" ), "-3 = bad arguments\n"
"Runs the specified command in the chroot of the target system.\n" "-4 = QProcess timeout" ) );
"Returns the program's exit code, or:\n" bp::def( "target_env_call",
"-1 = QProcess crash\n" static_cast< int ( * )( const bp::list&, const std::string&, int ) >( &CalamaresPython::target_env_call ),
"-2 = QProcess cannot start\n" target_env_call_list_overloads( bp::args( "args", "stdin", "timeout" ),
"-3 = bad arguments\n" "Runs the specified command in the chroot of the target system.\n"
"-4 = QProcess timeout" "Returns the program's exit code, or:\n"
) "-1 = QProcess crash\n"
); "-2 = QProcess cannot start\n"
bp::def( "-3 = bad arguments\n"
"target_env_call", "-4 = QProcess timeout" ) );
static_cast< int (*)( const bp::list&,
const std::string&,
int ) >( &CalamaresPython::target_env_call ),
target_env_call_list_overloads(
bp::args( "args",
"stdin",
"timeout" ),
"Runs the specified command in the chroot of the target system.\n"
"Returns the program's exit code, or:\n"
"-1 = QProcess crash\n"
"-2 = QProcess cannot start\n"
"-3 = bad arguments\n"
"-4 = QProcess timeout"
)
);
bp::def( "check_target_env_call",
static_cast< int ( * )( const std::string&, const std::string&, int ) >(
&CalamaresPython::check_target_env_call ),
check_target_env_call_str_overloads( bp::args( "command", "stdin", "timeout" ),
"Runs the specified command in the chroot of the target system.\n"
"Returns 0, which is program's exit code if the program exited "
"successfully, or raises a subprocess.CalledProcessError." ) );
bp::def( bp::def(
"check_target_env_call", "check_target_env_call",
static_cast< int (*)( const std::string&, static_cast< int ( * )( const bp::list&, const std::string&, int ) >( &CalamaresPython::check_target_env_call ),
const std::string&, check_target_env_call_list_overloads( bp::args( "args", "stdin", "timeout" ),
int ) >( &CalamaresPython::check_target_env_call ), "Runs the specified command in the chroot of the target system.\n"
check_target_env_call_str_overloads( "Returns 0, which is program's exit code if the program exited "
bp::args( "command", "successfully, or raises a subprocess.CalledProcessError." ) );
"stdin",
"timeout" ),
"Runs the specified command in the chroot of the target system.\n"
"Returns 0, which is program's exit code if the program exited "
"successfully, or raises a subprocess.CalledProcessError."
)
);
bp::def(
"check_target_env_call",
static_cast< int (*)( const bp::list&,
const std::string&,
int ) >( &CalamaresPython::check_target_env_call ),
check_target_env_call_list_overloads(
bp::args( "args",
"stdin",
"timeout" ),
"Runs the specified command in the chroot of the target system.\n"
"Returns 0, which is program's exit code if the program exited "
"successfully, or raises a subprocess.CalledProcessError."
)
);
bp::def( bp::def( "check_target_env_output",
"check_target_env_output", static_cast< std::string ( * )( const std::string&, const std::string&, int ) >(
static_cast< std::string (*)( const std::string&, &CalamaresPython::check_target_env_output ),
const std::string&, check_target_env_output_str_overloads( bp::args( "command", "stdin", "timeout" ),
int ) >( &CalamaresPython::check_target_env_output ), "Runs the specified command in the chroot of the target system.\n"
check_target_env_output_str_overloads( "Returns the program's standard output, and raises a "
bp::args( "command", "subprocess.CalledProcessError if something went wrong." ) );
"stdin", bp::def( "check_target_env_output",
"timeout" ), static_cast< std::string ( * )( const bp::list&, const std::string&, int ) >(
"Runs the specified command in the chroot of the target system.\n" &CalamaresPython::check_target_env_output ),
"Returns the program's standard output, and raises a " check_target_env_output_list_overloads( bp::args( "args", "stdin", "timeout" ),
"subprocess.CalledProcessError if something went wrong." "Runs the specified command in the chroot of the target system.\n"
) "Returns the program's standard output, and raises a "
); "subprocess.CalledProcessError if something went wrong." ) );
bp::def( bp::def( "obscure",
"check_target_env_output", &CalamaresPython::obscure,
static_cast< std::string (*)( const bp::list&, bp::args( "s" ),
const std::string&, "Simple string obfuscation function based on KStringHandler::obscure.\n"
int ) >( &CalamaresPython::check_target_env_output ), "Returns a string, generated using a simple symmetric encryption.\n"
check_target_env_output_list_overloads( "Applying the function to a string obscured by this function will result "
bp::args( "args", "in the original string." );
"stdin",
"timeout" ),
"Runs the specified command in the chroot of the target system.\n"
"Returns the program's standard output, and raises a "
"subprocess.CalledProcessError if something went wrong."
)
);
bp::def(
"obscure",
&CalamaresPython::obscure,
bp::args( "s" ),
"Simple string obfuscation function based on KStringHandler::obscure.\n"
"Returns a string, generated using a simple symmetric encryption.\n"
"Applying the function to a string obscured by this function will result "
"in the original string."
);
bp::def( bp::def( "gettext_languages",
"gettext_languages", &CalamaresPython::gettext_languages,
&CalamaresPython::gettext_languages, "Returns list of languages (most to least-specific) for gettext." );
"Returns list of languages (most to least-specific) for gettext."
);
bp::def( bp::def( "gettext_path", &CalamaresPython::gettext_path, "Returns path for gettext search." );
"gettext_path",
&CalamaresPython::gettext_path,
"Returns path for gettext search."
);
} }
namespace Calamares { namespace Calamares
{
PythonJob::PythonJob( const QString& scriptFile, PythonJob::PythonJob( const QString& scriptFile,
@ -255,8 +183,7 @@ PythonJob::PythonJob( const QString& scriptFile,
} }
PythonJob::~PythonJob() PythonJob::~PythonJob() {}
{}
QString QString
@ -270,10 +197,11 @@ QString
PythonJob::prettyStatusMessage() const PythonJob::prettyStatusMessage() const
{ {
if ( m_description.isEmpty() ) if ( m_description.isEmpty() )
return tr( "Running %1 operation." ) return tr( "Running %1 operation." ).arg( QDir( m_workingPath ).dirName() );
.arg( QDir( m_workingPath ).dirName() );
else else
{
return m_description; return m_description;
}
} }
@ -282,24 +210,20 @@ PythonJob::exec()
{ {
// We assume m_scriptFile to be relative to m_workingPath. // We assume m_scriptFile to be relative to m_workingPath.
QDir workingDir( m_workingPath ); QDir workingDir( m_workingPath );
if ( !workingDir.exists() || if ( !workingDir.exists() || !workingDir.isReadable() )
!workingDir.isReadable() )
{ {
return JobResult::error( tr( "Bad working directory path" ), return JobResult::error(
tr( "Working directory %1 for python job %2 is not readable." ) tr( "Bad working directory path" ),
.arg( m_workingPath ) tr( "Working directory %1 for python job %2 is not readable." ).arg( m_workingPath ).arg( prettyName() ) );
.arg( prettyName() ) );
} }
QFileInfo scriptFI( workingDir.absoluteFilePath( m_scriptFile ) ); QFileInfo scriptFI( workingDir.absoluteFilePath( m_scriptFile ) );
if ( !scriptFI.exists() || if ( !scriptFI.exists() || !scriptFI.isFile() || !scriptFI.isReadable() )
!scriptFI.isFile() ||
!scriptFI.isReadable() )
{ {
return JobResult::error( tr( "Bad main script file" ), return JobResult::error( tr( "Bad main script file" ),
tr( "Main script file %1 for python job %2 is not readable." ) tr( "Main script file %1 for python job %2 is not readable." )
.arg( scriptFI.absoluteFilePath() ) .arg( scriptFI.absoluteFilePath() )
.arg( prettyName() ) ); .arg( prettyName() ) );
} }
try try
@ -310,15 +234,14 @@ PythonJob::exec()
bp::dict calamaresNamespace = bp::extract< bp::dict >( calamaresModule.attr( "__dict__" ) ); bp::dict calamaresNamespace = bp::extract< bp::dict >( calamaresModule.attr( "__dict__" ) );
calamaresNamespace[ "job" ] = CalamaresPython::PythonJobInterface( this ); calamaresNamespace[ "job" ] = CalamaresPython::PythonJobInterface( this );
calamaresNamespace[ "globalstorage" ] = CalamaresPython::GlobalStoragePythonWrapper( calamaresNamespace[ "globalstorage" ]
JobQueue::instance()->globalStorage() ); = CalamaresPython::GlobalStoragePythonWrapper( JobQueue::instance()->globalStorage() );
bp::object execResult = bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(), bp::object execResult
scriptNamespace, = bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(), scriptNamespace, scriptNamespace );
scriptNamespace );
bp::object entryPoint = scriptNamespace[ "run" ]; bp::object entryPoint = scriptNamespace[ "run" ];
bp::object prettyNameFunc = scriptNamespace.get("pretty_name", bp::object()); bp::object prettyNameFunc = scriptNamespace.get( "pretty_name", bp::object() );
cDebug() << "Job file" << scriptFI.absoluteFilePath(); cDebug() << "Job file" << scriptFI.absoluteFilePath();
if ( !prettyNameFunc.is_none() ) if ( !prettyNameFunc.is_none() )
@ -337,14 +260,16 @@ PythonJob::exec()
if ( m_description.isEmpty() ) if ( m_description.isEmpty() )
{ {
bp::extract< std::string > entryPoint_doc_attr(entryPoint.attr( "__doc__" ) ); bp::extract< std::string > entryPoint_doc_attr( entryPoint.attr( "__doc__" ) );
if ( entryPoint_doc_attr.check() ) if ( entryPoint_doc_attr.check() )
{ {
m_description = QString::fromStdString( entryPoint_doc_attr() ).trimmed(); m_description = QString::fromStdString( entryPoint_doc_attr() ).trimmed();
auto i_newline = m_description.indexOf('\n'); auto i_newline = m_description.indexOf( '\n' );
if ( i_newline > 0 ) if ( i_newline > 0 )
{
m_description.truncate( i_newline ); m_description.truncate( i_newline );
}
cDebug() << "Job description from __doc__" << prettyName() << "=" << m_description; cDebug() << "Job description from __doc__" << prettyName() << "=" << m_description;
emit progress( 0 ); emit progress( 0 );
} }
@ -356,7 +281,7 @@ PythonJob::exec()
{ {
return JobResult::ok(); return JobResult::ok();
} }
else // Something happened in the Python job else // Something happened in the Python job
{ {
bp::tuple resultTuple = bp::extract< bp::tuple >( runResult ); bp::tuple resultTuple = bp::extract< bp::tuple >( runResult );
QString message = QString::fromStdString( bp::extract< std::string >( resultTuple[ 0 ] ) ); QString message = QString::fromStdString( bp::extract< std::string >( resultTuple[ 0 ] ) );
@ -374,9 +299,7 @@ PythonJob::exec()
bp::handle_exception(); bp::handle_exception();
PyErr_Clear(); PyErr_Clear();
return JobResult::internalError( return JobResult::internalError(
tr( "Boost.Python error in job \"%1\"." ).arg( prettyName() ), tr( "Boost.Python error in job \"%1\"." ).arg( prettyName() ), msg, JobResult::PythonUncaughtException );
msg,
JobResult::PythonUncaughtException );
} }
} }
@ -392,10 +315,12 @@ CalamaresPython::Helper*
PythonJob::helper() PythonJob::helper()
{ {
auto ptr = CalamaresPython::Helper::s_instance; auto ptr = CalamaresPython::Helper::s_instance;
if (!ptr) if ( !ptr )
{
ptr = new CalamaresPython::Helper; ptr = new CalamaresPython::Helper;
}
return ptr; return ptr;
} }
} // namespace Calamares } // namespace Calamares

View File

@ -27,9 +27,10 @@ namespace CalamaresPython
{ {
class PythonJobInterface; class PythonJobInterface;
class Helper; class Helper;
} } // namespace CalamaresPython
namespace Calamares { namespace Calamares
{
class PythonJob : public Job class PythonJob : public Job
{ {
@ -57,6 +58,6 @@ private:
QVariantMap m_configurationMap; QVariantMap m_configurationMap;
}; };
} // namespace Calamares } // namespace Calamares
#endif // CALAMARES_PYTHONJOB_H #endif // CALAMARES_PYTHONJOB_H

View File

@ -20,8 +20,8 @@
#include "PythonJobApi.h" #include "PythonJobApi.h"
#include "PythonHelper.h" #include "PythonHelper.h"
#include "utils/Logger.h"
#include "utils/CalamaresUtilsSystem.h" #include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h"
#include "utils/String.h" #include "utils/String.h"
#include "GlobalStorage.h" #include "GlobalStorage.h"
@ -40,15 +40,19 @@ static int
_handle_check_target_env_call_error( const CalamaresUtils::ProcessResult& ec, const QString& cmd ) _handle_check_target_env_call_error( const CalamaresUtils::ProcessResult& ec, const QString& cmd )
{ {
if ( !ec.first ) if ( !ec.first )
{
return ec.first; 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.first ) .arg( ec.first )
.arg( cmd ); .arg( cmd );
if ( !ec.second.isEmpty() ) if ( !ec.second.isEmpty() )
raise.append( QStringLiteral("e.output = \"\"\"%1\"\"\"\n").arg( ec.second ) ); {
raise.append("raise e"); raise.append( QStringLiteral( "e.output = \"\"\"%1\"\"\"\n" ).arg( ec.second ) );
}
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.first; return ec.first;
@ -63,11 +67,10 @@ mount( const std::string& device_path,
const std::string& filesystem_name, const std::string& filesystem_name,
const std::string& options ) const std::string& options )
{ {
return CalamaresUtils::System::instance()-> return CalamaresUtils::System::instance()->mount( QString::fromStdString( device_path ),
mount( QString::fromStdString( device_path ), QString::fromStdString( mount_point ),
QString::fromStdString( mount_point ), QString::fromStdString( filesystem_name ),
QString::fromStdString( filesystem_name ), QString::fromStdString( options ) );
QString::fromStdString( options ) );
} }
@ -77,66 +80,50 @@ _bp_list_to_qstringlist( const bp::list& args )
QStringList list; QStringList list;
for ( int i = 0; i < bp::len( args ); ++i ) for ( int i = 0; i < bp::len( args ); ++i )
{ {
list.append( QString::fromStdString( list.append( QString::fromStdString( bp::extract< std::string >( args[ i ] ) ) );
bp::extract< std::string >( args[ i ] ) ) );
} }
return list; return list;
} }
static inline CalamaresUtils::ProcessResult static inline CalamaresUtils::ProcessResult
_target_env_command( _target_env_command( const QStringList& args, const std::string& stdin, int timeout )
const QStringList& args,
const std::string& stdin,
int timeout )
{ {
// Since Python doesn't give us the type system for distinguishing // Since Python doesn't give us the type system for distinguishing
// seconds from other integral types, massage to seconds here. // seconds from other integral types, massage to seconds here.
return CalamaresUtils::System::instance()-> return CalamaresUtils::System::instance()->targetEnvCommand(
targetEnvCommand( args, args, QString(), QString::fromStdString( stdin ), std::chrono::seconds( timeout ) );
QString(),
QString::fromStdString( stdin ),
std::chrono::seconds( timeout ) );
} }
int int
target_env_call( const std::string& command, target_env_call( const std::string& command, const std::string& stdin, int timeout )
const std::string& stdin,
int timeout )
{ {
return _target_env_command( return _target_env_command( QStringList { QString::fromStdString( command ) }, stdin, timeout ).first;
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, int timeout )
const std::string& stdin,
int timeout )
{ {
return _target_env_command( return _target_env_command( _bp_list_to_qstringlist( args ), stdin, timeout ).first;
_bp_list_to_qstringlist( args ), stdin, timeout ).first;
} }
int int
check_target_env_call( const std::string& command, check_target_env_call( const std::string& command, const std::string& stdin, int timeout )
const std::string& stdin,
int timeout )
{ {
auto ec = _target_env_command( auto ec = _target_env_command( QStringList { QString::fromStdString( command ) }, stdin, timeout );
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 ) );
} }
int int
check_target_env_call( const bp::list& args, check_target_env_call( const bp::list& args, const std::string& stdin, int timeout )
const std::string& stdin,
int timeout )
{ {
auto ec = _target_env_command( _bp_list_to_qstringlist( args ), stdin, timeout ); auto ec = _target_env_command( _bp_list_to_qstringlist( args ), stdin, timeout );
if ( !ec.first ) if ( !ec.first )
{
return ec.first; 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( ' ' ) );
@ -144,25 +131,19 @@ check_target_env_call( const bp::list& args,
std::string std::string
check_target_env_output( const std::string& command, check_target_env_output( const std::string& command, const std::string& stdin, int timeout )
const std::string& stdin,
int timeout )
{ {
auto ec = _target_env_command( auto ec = _target_env_command( QStringList { QString::fromStdString( command ) }, stdin, timeout );
QStringList{ QString::fromStdString( command ) }, stdin, timeout );
_handle_check_target_env_call_error( ec, QString::fromStdString( command ) ); _handle_check_target_env_call_error( ec, QString::fromStdString( command ) );
return ec.second.toStdString(); return ec.second.toStdString();
} }
std::string std::string
check_target_env_output( const bp::list& args, check_target_env_output( const bp::list& args, const std::string& stdin, int timeout )
const std::string& stdin,
int timeout )
{ {
QStringList list = _bp_list_to_qstringlist( args ); QStringList list = _bp_list_to_qstringlist( args );
auto ec = _target_env_command( auto ec = _target_env_command( list, stdin, timeout );
list, stdin, timeout );
_handle_check_target_env_call_error( ec, list.join( ' ' ) ); _handle_check_target_env_call_error( ec, list.join( ' ' ) );
return ec.second.toStdString(); return ec.second.toStdString();
} }
@ -194,7 +175,9 @@ void
PythonJobInterface::setprogress( qreal progress ) PythonJobInterface::setprogress( qreal progress )
{ {
if ( progress >= 0 && progress <= 1 ) if ( progress >= 0 && progress <= 1 )
{
m_parent->emitProgress( progress ); m_parent->emitProgress( progress );
}
} }
@ -216,7 +199,8 @@ _gettext_languages()
// own GlobalStoragePythonWrapper, which then holds a // own GlobalStoragePythonWrapper, which then holds a
// GlobalStorage object for all of Python. // GlobalStorage object for all of Python.
Calamares::JobQueue* jq = Calamares::JobQueue::instance(); Calamares::JobQueue* jq = Calamares::JobQueue::instance();
Calamares::GlobalStorage* gs = jq ? jq->globalStorage() : CalamaresPython::GlobalStoragePythonWrapper::globalStorageInstance(); Calamares::GlobalStorage* gs
= jq ? jq->globalStorage() : CalamaresPython::GlobalStoragePythonWrapper::globalStorageInstance();
QVariant localeConf_ = gs->value( "localeConf" ); QVariant localeConf_ = gs->value( "localeConf" );
if ( localeConf_.canConvert< QVariantMap >() ) if ( localeConf_.canConvert< QVariantMap >() )
@ -246,7 +230,9 @@ gettext_languages()
{ {
bp::list pyList; bp::list pyList;
for ( auto lang : _gettext_languages() ) for ( auto lang : _gettext_languages() )
{
pyList.append( lang.toStdString() ); pyList.append( lang.toStdString() );
}
return pyList; return pyList;
} }
@ -257,7 +243,9 @@ _add_localedirs( QStringList& pathList, const QString& candidate )
{ {
pathList.prepend( candidate ); pathList.prepend( candidate );
if ( QDir( candidate ).cd( "lang" ) ) if ( QDir( candidate ).cd( "lang" ) )
{
pathList.prepend( candidate + "/lang" ); pathList.prepend( candidate + "/lang" );
}
} }
} }
@ -266,16 +254,19 @@ gettext_path()
{ {
// TODO: distinguish between -d runs and normal runs // TODO: distinguish between -d runs and normal runs
// TODO: can we detect DESTDIR-installs? // TODO: can we detect DESTDIR-installs?
QStringList candidatePaths = QStandardPaths::locateAll( QStandardPaths::GenericDataLocation, "locale", QStandardPaths::LocateDirectory ); QStringList candidatePaths
= QStandardPaths::locateAll( QStandardPaths::GenericDataLocation, "locale", QStandardPaths::LocateDirectory );
QString extra = QCoreApplication::applicationDirPath(); QString extra = QCoreApplication::applicationDirPath();
_add_localedirs( candidatePaths, extra ); // Often /usr/local/bin _add_localedirs( candidatePaths, extra ); // Often /usr/local/bin
if ( !extra.isEmpty() ) if ( !extra.isEmpty() )
{ {
QDir d( extra ); QDir d( extra );
if ( d.cd( "../share/locale" ) ) // Often /usr/local/bin/../share/locale -> /usr/local/share/locale if ( d.cd( "../share/locale" ) ) // Often /usr/local/bin/../share/locale -> /usr/local/share/locale
{
_add_localedirs( candidatePaths, d.canonicalPath() ); _add_localedirs( candidatePaths, d.canonicalPath() );
}
} }
_add_localedirs( candidatePaths, QDir().canonicalPath() ); // . _add_localedirs( candidatePaths, QDir().canonicalPath() ); // .
cDebug() << "Determining gettext path from" << candidatePaths; cDebug() << "Determining gettext path from" << candidatePaths;
@ -296,4 +287,4 @@ gettext_path()
} }
} } // namespace CalamaresPython

View File

@ -35,29 +35,19 @@ int mount( const std::string& device_path,
const std::string& filesystem_name = std::string(), const std::string& filesystem_name = std::string(),
const std::string& options = std::string() ); const std::string& options = std::string() );
int target_env_call( const std::string& command, int target_env_call( const std::string& command, const std::string& stdin = std::string(), int timeout = 0 );
const std::string& stdin = std::string(),
int timeout = 0 );
int target_env_call( const boost::python::list& args, int target_env_call( const boost::python::list& args, const std::string& stdin = std::string(), int timeout = 0 );
const std::string& stdin = std::string(),
int timeout = 0 );
int check_target_env_call( const std::string& command, int check_target_env_call( const std::string& command, const std::string& stdin = std::string(), int timeout = 0 );
const std::string& stdin = std::string(),
int timeout = 0 );
int check_target_env_call( const boost::python::list& args, int check_target_env_call( const boost::python::list& args, const std::string& stdin = std::string(), int timeout = 0 );
const std::string& stdin = std::string(),
int timeout = 0 );
std::string check_target_env_output( const std::string& command, std::string
const std::string& stdin = std::string(), check_target_env_output( const std::string& command, const std::string& stdin = std::string(), int timeout = 0 );
int timeout = 0 );
std::string check_target_env_output( const boost::python::list& args, std::string
const std::string& stdin = std::string(), check_target_env_output( const boost::python::list& args, const std::string& stdin = std::string(), int timeout = 0 );
int timeout = 0 );
std::string obscure( const std::string& string ); std::string obscure( const std::string& string );
@ -85,6 +75,6 @@ private:
Calamares::PythonJob* m_parent; Calamares::PythonJob* m_parent;
}; };
} } // namespace CalamaresPython
#endif // PYTHONJOBAPI_H #endif // PYTHONJOBAPI_H

View File

@ -40,8 +40,10 @@ static QString
requireString( const YAML::Node& config, const char* key ) requireString( const YAML::Node& config, const char* key )
{ {
auto v = config[ key ]; auto v = config[ key ];
if ( hasValue(v) ) if ( hasValue( v ) )
{
return QString::fromStdString( v.as< std::string >() ); return QString::fromStdString( v.as< std::string >() );
}
else else
{ {
cWarning() << Logger::SubEntry << "Required settings.conf key" << key << "is missing."; cWarning() << Logger::SubEntry << "Required settings.conf key" << key << "is missing.";
@ -54,8 +56,10 @@ static bool
requireBool( const YAML::Node& config, const char* key, bool d ) requireBool( const YAML::Node& config, const char* key, bool d )
{ {
auto v = config[ key ]; auto v = config[ key ];
if ( hasValue(v) ) if ( hasValue( v ) )
{
return v.as< bool >(); return v.as< bool >();
}
else else
{ {
cWarning() << Logger::SubEntry << "Required settings.conf key" << key << "is missing."; cWarning() << Logger::SubEntry << "Required settings.conf key" << key << "is missing.";
@ -86,17 +90,17 @@ interpretModulesSearch( const bool debugMode, const QStringList& rawPaths, QStri
// module search path in the build dir. // module search path in the build dir.
if ( debugMode ) if ( debugMode )
{ {
QString buildDirModules = QDir::current().absolutePath() + QString buildDirModules
QDir::separator() + "src" + = QDir::current().absolutePath() + QDir::separator() + "src" + QDir::separator() + "modules";
QDir::separator() + "modules";
if ( QDir( buildDirModules ).exists() ) if ( QDir( buildDirModules ).exists() )
{
output.append( buildDirModules ); output.append( buildDirModules );
}
} }
// Install path is set in CalamaresAddPlugin.cmake // Install path is set in CalamaresAddPlugin.cmake
output.append( CalamaresUtils::systemLibDir().absolutePath() + output.append( CalamaresUtils::systemLibDir().absolutePath() + QDir::separator() + "calamares"
QDir::separator() + "calamares" + + QDir::separator() + "modules" );
QDir::separator() + "modules" );
} }
else else
{ {
@ -106,7 +110,9 @@ interpretModulesSearch( const bool debugMode, const QStringList& rawPaths, QStri
output.append( d.absolutePath() ); output.append( d.absolutePath() );
} }
else else
{
cDebug() << Logger::SubEntry << "module-search entry non-existent" << path; cDebug() << Logger::SubEntry << "module-search entry non-existent" << path;
}
} }
} }
} }
@ -124,15 +130,17 @@ interpretInstances( const YAML::Node& node, Settings::InstanceDescriptionList& c
for ( const QVariant& instancesVListItem : instances ) for ( const QVariant& instancesVListItem : instances )
{ {
if ( instancesVListItem.type() != QVariant::Map ) if ( instancesVListItem.type() != QVariant::Map )
{
continue; continue;
QVariantMap instancesVListItemMap = }
instancesVListItem.toMap(); QVariantMap instancesVListItemMap = instancesVListItem.toMap();
Settings::InstanceDescription instanceMap; Settings::InstanceDescription instanceMap;
for ( auto it = instancesVListItemMap.constBegin(); for ( auto it = instancesVListItemMap.constBegin(); it != instancesVListItemMap.constEnd(); ++it )
it != instancesVListItemMap.constEnd(); ++it )
{ {
if ( it.value().type() != QVariant::String ) if ( it.value().type() != QVariant::String )
{
continue; continue;
}
instanceMap.insert( it.key(), it.value().toString() ); instanceMap.insert( it.key(), it.value().toString() );
} }
customInstances.append( instanceMap ); customInstances.append( instanceMap );
@ -149,37 +157,43 @@ interpretSequence( const YAML::Node& node, Settings::ModuleSequence& moduleSeque
{ {
QVariant sequenceV = CalamaresUtils::yamlToVariant( node ); QVariant sequenceV = CalamaresUtils::yamlToVariant( node );
if ( !( sequenceV.type() == QVariant::List ) ) if ( !( sequenceV.type() == QVariant::List ) )
{
throw YAML::Exception( YAML::Mark(), "sequence key does not have a list-value" ); throw YAML::Exception( YAML::Mark(), "sequence key does not have a list-value" );
}
const auto sequence = sequenceV.toList(); const auto sequence = sequenceV.toList();
for ( const QVariant& sequenceVListItem : sequence ) for ( const QVariant& sequenceVListItem : sequence )
{ {
if ( sequenceVListItem.type() != QVariant::Map ) if ( sequenceVListItem.type() != QVariant::Map )
{
continue; continue;
}
QString thisActionS = sequenceVListItem.toMap().firstKey(); QString thisActionS = sequenceVListItem.toMap().firstKey();
ModuleAction thisAction; ModuleAction thisAction;
if ( thisActionS == "show" ) if ( thisActionS == "show" )
{
thisAction = ModuleAction::Show; thisAction = ModuleAction::Show;
}
else if ( thisActionS == "exec" ) else if ( thisActionS == "exec" )
{
thisAction = ModuleAction::Exec; thisAction = ModuleAction::Exec;
}
else else
{
continue; continue;
}
QStringList thisActionRoster = sequenceVListItem QStringList thisActionRoster = sequenceVListItem.toMap().value( thisActionS ).toStringList();
.toMap() moduleSequence.append( qMakePair( thisAction, thisActionRoster ) );
.value( thisActionS )
.toStringList();
moduleSequence.append( qMakePair( thisAction,
thisActionRoster ) );
} }
} }
else else
{
throw YAML::Exception( YAML::Mark(), "sequence key is missing" ); throw YAML::Exception( YAML::Mark(), "sequence key is missing" );
}
} }
Settings::Settings( const QString& settingsFilePath, Settings::Settings( const QString& settingsFilePath, bool debugMode, QObject* parent )
bool debugMode,
QObject* parent )
: QObject( parent ) : QObject( parent )
, m_debug( debugMode ) , m_debug( debugMode )
, m_doChroot( true ) , m_doChroot( true )
@ -198,7 +212,8 @@ Settings::Settings( const QString& settingsFilePath,
YAML::Node config = YAML::Load( ba.constData() ); YAML::Node config = YAML::Load( ba.constData() );
Q_ASSERT( config.IsMap() ); Q_ASSERT( config.IsMap() );
interpretModulesSearch( debugMode, CalamaresUtils::yamlToStringList( config[ "modules-search" ] ), m_modulesSearchPaths ); interpretModulesSearch(
debugMode, CalamaresUtils::yamlToStringList( config[ "modules-search" ] ), m_modulesSearchPaths );
interpretInstances( config[ "instances" ], m_customModuleInstances ); interpretInstances( config[ "instances" ], m_customModuleInstances );
interpretSequence( config[ "sequence" ], m_modulesSequence ); interpretSequence( config[ "sequence" ], m_modulesSequence );
@ -283,4 +298,4 @@ Settings::disableCancelDuringExec() const
} }
} } // namespace Calamares

View File

@ -36,9 +36,7 @@ class DLLEXPORT Settings : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit Settings( const QString& settingsFilePath, explicit Settings( const QString& settingsFilePath, bool debugMode, QObject* parent = nullptr );
bool debugMode,
QObject* parent = nullptr );
static Settings* instance(); static Settings* instance();
@ -88,6 +86,6 @@ private:
bool m_disableCancelDuringExec; bool m_disableCancelDuringExec;
}; };
} } // namespace Calamares
#endif // SETTINGS_H #endif // SETTINGS_H