[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 )
: Job( parent )
{}
{
}
CppJob::~CppJob()
{}
CppJob::~CppJob() {}
void
@ -44,4 +44,4 @@ CppJob::setConfigurationMap( const QVariantMap& configurationMap )
Q_UNUSED( configurationMap )
}
}
} // namespace Calamares

View File

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

View File

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

View File

@ -34,8 +34,8 @@ namespace api
class object;
}
class list;
}
}
} // namespace python
} // namespace boost
#endif
namespace Calamares

View File

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

View File

@ -25,7 +25,8 @@
#include <QObject>
#include <QSharedPointer>
namespace Calamares {
namespace Calamares
{
class DLLEXPORT JobResult
{

View File

@ -36,7 +36,8 @@ GoodJob::exec()
JobResult
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"
namespace Calamares {
namespace Calamares
{
/** @brief A Job with a name
*
@ -39,6 +40,7 @@ public:
}
virtual QString prettyName() const override;
protected:
const QString m_name;
};

View File

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

View File

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

View File

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

View File

@ -24,7 +24,8 @@
#include <chrono>
namespace Calamares {
namespace Calamares
{
class ProcessJob : public Job
{

View File

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

View File

@ -66,6 +66,6 @@ private:
QStringList m_pythonPaths;
};
} // namespace Calamares
} // namespace CalamaresPython
#endif // CALAMARES_PYTHONJOBHELPER_H

View File

@ -19,10 +19,10 @@
#include "PythonJob.h"
#include "PythonHelper.h"
#include "utils/Logger.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "PythonHelper.h"
#include "utils/Logger.h"
#include <QDir>
@ -35,27 +35,19 @@
namespace bp = boost::python;
BOOST_PYTHON_FUNCTION_OVERLOADS( mount_overloads,
CalamaresPython::mount,
2, 4 );
BOOST_PYTHON_FUNCTION_OVERLOADS( target_env_call_str_overloads,
CalamaresPython::target_env_call,
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( mount_overloads, CalamaresPython::mount, 2, 4 );
BOOST_PYTHON_FUNCTION_OVERLOADS( target_env_call_str_overloads, CalamaresPython::target_env_call, 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,
CalamaresPython::check_target_env_output,
1, 3 );
1,
3 );
BOOST_PYTHON_FUNCTION_OVERLOADS( check_target_env_output_list_overloads,
CalamaresPython::check_target_env_output,
1, 3 );
1,
3 );
BOOST_PYTHON_MODULE( libcalamares )
{
bp::object package = bp::scope();
@ -72,15 +64,14 @@ BOOST_PYTHON_MODULE( libcalamares )
.def_readonly( "pretty_name", &CalamaresPython::PythonJobInterface::prettyName )
.def_readonly( "working_path", &CalamaresPython::PythonJobInterface::workingPath )
.def_readonly( "configuration", &CalamaresPython::PythonJobInterface::configuration )
.def(
"setprogress",
.def( "setprogress",
&CalamaresPython::PythonJobInterface::setprogress,
bp::args( "progress" ),
"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",
bp::init< Calamares::GlobalStorage* >() )
.def( "contains", &CalamaresPython::GlobalStoragePythonWrapper::contains )
.def( "count", &CalamaresPython::GlobalStoragePythonWrapper::count )
.def( "insert", &CalamaresPython::GlobalStoragePythonWrapper::insert )
@ -95,151 +86,88 @@ BOOST_PYTHON_MODULE( libcalamares )
Q_UNUSED( utilsScope )
bp::def(
"debug",
&CalamaresPython::debug,
bp::args( "s" ),
"Writes the given string to the Calamares debug stream."
);
bp::def(
"warning",
"debug", &CalamaresPython::debug, bp::args( "s" ), "Writes the given string to the Calamares debug stream." );
bp::def( "warning",
&CalamaresPython::warning,
bp::args( "s" ),
"Writes the given string to the Calamares warning stream."
);
"Writes the given string to the Calamares warning stream." );
bp::def(
"mount",
bp::def( "mount",
&CalamaresPython::mount,
mount_overloads(
bp::args( "device_path",
"mount_point",
"filesystem_name",
"options" ),
mount_overloads( bp::args( "device_path", "mount_point", "filesystem_name", "options" ),
"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"
)
);
"-3 = bad arguments" ) );
bp::def(
"target_env_call",
static_cast< int (*)( const std::string&,
const std::string&,
int ) >( &CalamaresPython::target_env_call ),
target_env_call_str_overloads(
bp::args( "command",
"stdin",
"timeout" ),
static_cast< int ( * )( const std::string&, const std::string&, int ) >( &CalamaresPython::target_env_call ),
target_env_call_str_overloads( bp::args( "command", "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(
"target_env_call",
static_cast< int (*)( const bp::list&,
const std::string&,
int ) >( &CalamaresPython::target_env_call ),
target_env_call_list_overloads(
bp::args( "args",
"stdin",
"timeout" ),
"-4 = QProcess timeout" ) );
bp::def( "target_env_call",
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"
)
);
"-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" ),
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."
)
);
"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" ),
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."
)
);
"successfully, or raises a subprocess.CalledProcessError." ) );
bp::def(
"check_target_env_output",
static_cast< std::string (*)( const std::string&,
const std::string&,
int ) >( &CalamaresPython::check_target_env_output ),
check_target_env_output_str_overloads(
bp::args( "command",
"stdin",
"timeout" ),
bp::def( "check_target_env_output",
static_cast< std::string ( * )( const std::string&, const std::string&, int ) >(
&CalamaresPython::check_target_env_output ),
check_target_env_output_str_overloads( bp::args( "command", "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(
"check_target_env_output",
static_cast< std::string (*)( const bp::list&,
const std::string&,
int ) >( &CalamaresPython::check_target_env_output ),
check_target_env_output_list_overloads(
bp::args( "args",
"stdin",
"timeout" ),
"subprocess.CalledProcessError if something went wrong." ) );
bp::def( "check_target_env_output",
static_cast< std::string ( * )( const bp::list&, const std::string&, int ) >(
&CalamaresPython::check_target_env_output ),
check_target_env_output_list_overloads( bp::args( "args", "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",
"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."
);
"in the original string." );
bp::def(
"gettext_languages",
bp::def( "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(
"gettext_path",
&CalamaresPython::gettext_path,
"Returns path for gettext search."
);
bp::def( "gettext_path", &CalamaresPython::gettext_path, "Returns path for gettext search." );
}
namespace Calamares {
namespace Calamares
{
PythonJob::PythonJob( const QString& scriptFile,
@ -255,8 +183,7 @@ PythonJob::PythonJob( const QString& scriptFile,
}
PythonJob::~PythonJob()
{}
PythonJob::~PythonJob() {}
QString
@ -270,11 +197,12 @@ QString
PythonJob::prettyStatusMessage() const
{
if ( m_description.isEmpty() )
return tr( "Running %1 operation." )
.arg( QDir( m_workingPath ).dirName() );
return tr( "Running %1 operation." ).arg( QDir( m_workingPath ).dirName() );
else
{
return m_description;
}
}
JobResult
@ -282,19 +210,15 @@ PythonJob::exec()
{
// We assume m_scriptFile to be relative to m_workingPath.
QDir workingDir( m_workingPath );
if ( !workingDir.exists() ||
!workingDir.isReadable() )
if ( !workingDir.exists() || !workingDir.isReadable() )
{
return JobResult::error( tr( "Bad working directory path" ),
tr( "Working directory %1 for python job %2 is not readable." )
.arg( m_workingPath )
.arg( prettyName() ) );
return JobResult::error(
tr( "Bad working directory path" ),
tr( "Working directory %1 for python job %2 is not readable." ).arg( m_workingPath ).arg( prettyName() ) );
}
QFileInfo scriptFI( workingDir.absoluteFilePath( m_scriptFile ) );
if ( !scriptFI.exists() ||
!scriptFI.isFile() ||
!scriptFI.isReadable() )
if ( !scriptFI.exists() || !scriptFI.isFile() || !scriptFI.isReadable() )
{
return JobResult::error( tr( "Bad main script file" ),
tr( "Main script file %1 for python job %2 is not readable." )
@ -310,12 +234,11 @@ PythonJob::exec()
bp::dict calamaresNamespace = bp::extract< bp::dict >( calamaresModule.attr( "__dict__" ) );
calamaresNamespace[ "job" ] = CalamaresPython::PythonJobInterface( this );
calamaresNamespace[ "globalstorage" ] = CalamaresPython::GlobalStoragePythonWrapper(
JobQueue::instance()->globalStorage() );
calamaresNamespace[ "globalstorage" ]
= CalamaresPython::GlobalStoragePythonWrapper( JobQueue::instance()->globalStorage() );
bp::object execResult = bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(),
scriptNamespace,
scriptNamespace );
bp::object execResult
= bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(), scriptNamespace, scriptNamespace );
bp::object entryPoint = scriptNamespace[ "run" ];
bp::object prettyNameFunc = scriptNamespace.get( "pretty_name", bp::object() );
@ -344,7 +267,9 @@ PythonJob::exec()
m_description = QString::fromStdString( entryPoint_doc_attr() ).trimmed();
auto i_newline = m_description.indexOf( '\n' );
if ( i_newline > 0 )
{
m_description.truncate( i_newline );
}
cDebug() << "Job description from __doc__" << prettyName() << "=" << m_description;
emit progress( 0 );
}
@ -374,9 +299,7 @@ PythonJob::exec()
bp::handle_exception();
PyErr_Clear();
return JobResult::internalError(
tr( "Boost.Python error in job \"%1\"." ).arg( prettyName() ),
msg,
JobResult::PythonUncaughtException );
tr( "Boost.Python error in job \"%1\"." ).arg( prettyName() ), msg, JobResult::PythonUncaughtException );
}
}
@ -393,7 +316,9 @@ PythonJob::helper()
{
auto ptr = CalamaresPython::Helper::s_instance;
if ( !ptr )
{
ptr = new CalamaresPython::Helper;
}
return ptr;
}

View File

@ -27,9 +27,10 @@ namespace CalamaresPython
{
class PythonJobInterface;
class Helper;
}
} // namespace CalamaresPython
namespace Calamares {
namespace Calamares
{
class PythonJob : public Job
{

View File

@ -20,8 +20,8 @@
#include "PythonJobApi.h"
#include "PythonHelper.h"
#include "utils/Logger.h"
#include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h"
#include "utils/String.h"
#include "GlobalStorage.h"
@ -40,14 +40,18 @@ static int
_handle_check_target_env_call_error( const CalamaresUtils::ProcessResult& ec, const QString& cmd )
{
if ( !ec.first )
{
return ec.first;
}
QString raise = QString( "import subprocess\n"
"e = subprocess.CalledProcessError(%1,\"%2\")\n" )
.arg( ec.first )
.arg( cmd );
if ( !ec.second.isEmpty() )
{
raise.append( QStringLiteral( "e.output = \"\"\"%1\"\"\"\n" ).arg( ec.second ) );
}
raise.append( "raise e" );
bp::exec( raise.toStdString().c_str() );
bp::throw_error_already_set();
@ -63,8 +67,7 @@ mount( const std::string& device_path,
const std::string& filesystem_name,
const std::string& options )
{
return CalamaresUtils::System::instance()->
mount( QString::fromStdString( device_path ),
return CalamaresUtils::System::instance()->mount( QString::fromStdString( device_path ),
QString::fromStdString( mount_point ),
QString::fromStdString( filesystem_name ),
QString::fromStdString( options ) );
@ -77,66 +80,50 @@ _bp_list_to_qstringlist( const bp::list& args )
QStringList list;
for ( int i = 0; i < bp::len( args ); ++i )
{
list.append( QString::fromStdString(
bp::extract< std::string >( args[ i ] ) ) );
list.append( QString::fromStdString( bp::extract< std::string >( args[ i ] ) ) );
}
return list;
}
static inline CalamaresUtils::ProcessResult
_target_env_command(
const QStringList& args,
const std::string& stdin,
int timeout )
_target_env_command( const QStringList& args, const std::string& stdin, int timeout )
{
// Since Python doesn't give us the type system for distinguishing
// seconds from other integral types, massage to seconds here.
return CalamaresUtils::System::instance()->
targetEnvCommand( args,
QString(),
QString::fromStdString( stdin ),
std::chrono::seconds( timeout ) );
return CalamaresUtils::System::instance()->targetEnvCommand(
args, QString(), QString::fromStdString( stdin ), std::chrono::seconds( timeout ) );
}
int
target_env_call( const std::string& command,
const std::string& stdin,
int timeout )
target_env_call( const std::string& command, const std::string& stdin, int timeout )
{
return _target_env_command(
QStringList{ QString::fromStdString( command ) }, stdin, timeout ).first;
return _target_env_command( QStringList { QString::fromStdString( command ) }, stdin, timeout ).first;
}
int
target_env_call( const bp::list& args,
const std::string& stdin,
int timeout )
target_env_call( const bp::list& args, const std::string& stdin, int timeout )
{
return _target_env_command(
_bp_list_to_qstringlist( args ), stdin, timeout ).first;
return _target_env_command( _bp_list_to_qstringlist( args ), stdin, timeout ).first;
}
int
check_target_env_call( const std::string& command,
const std::string& stdin,
int timeout )
check_target_env_call( const std::string& command, const std::string& stdin, int timeout )
{
auto ec = _target_env_command(
QStringList{ QString::fromStdString( 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 ) );
}
int
check_target_env_call( const bp::list& args,
const std::string& stdin,
int timeout )
check_target_env_call( const bp::list& args, const std::string& stdin, int timeout )
{
auto ec = _target_env_command( _bp_list_to_qstringlist( args ), stdin, timeout );
if ( !ec.first )
{
return ec.first;
}
QStringList failedCmdList = _bp_list_to_qstringlist( args );
return _handle_check_target_env_call_error( ec, failedCmdList.join( ' ' ) );
@ -144,25 +131,19 @@ check_target_env_call( const bp::list& args,
std::string
check_target_env_output( const std::string& command,
const std::string& stdin,
int timeout )
check_target_env_output( const std::string& command, const std::string& stdin, int timeout )
{
auto ec = _target_env_command(
QStringList{ QString::fromStdString( command ) }, stdin, timeout );
auto ec = _target_env_command( QStringList { QString::fromStdString( command ) }, stdin, timeout );
_handle_check_target_env_call_error( ec, QString::fromStdString( command ) );
return ec.second.toStdString();
}
std::string
check_target_env_output( const bp::list& args,
const std::string& stdin,
int timeout )
check_target_env_output( const bp::list& args, const std::string& stdin, int timeout )
{
QStringList list = _bp_list_to_qstringlist( args );
auto ec = _target_env_command(
list, stdin, timeout );
auto ec = _target_env_command( list, stdin, timeout );
_handle_check_target_env_call_error( ec, list.join( ' ' ) );
return ec.second.toStdString();
}
@ -194,8 +175,10 @@ void
PythonJobInterface::setprogress( qreal progress )
{
if ( progress >= 0 && progress <= 1 )
{
m_parent->emitProgress( progress );
}
}
std::string
@ -216,7 +199,8 @@ _gettext_languages()
// own GlobalStoragePythonWrapper, which then holds a
// GlobalStorage object for all of Python.
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" );
if ( localeConf_.canConvert< QVariantMap >() )
@ -246,7 +230,9 @@ gettext_languages()
{
bp::list pyList;
for ( auto lang : _gettext_languages() )
{
pyList.append( lang.toStdString() );
}
return pyList;
}
@ -257,24 +243,29 @@ _add_localedirs( QStringList& pathList, const QString& candidate )
{
pathList.prepend( candidate );
if ( QDir( candidate ).cd( "lang" ) )
{
pathList.prepend( candidate + "/lang" );
}
}
}
bp::object
gettext_path()
{
// TODO: distinguish between -d runs and normal runs
// 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();
_add_localedirs( candidatePaths, extra ); // Often /usr/local/bin
if ( !extra.isEmpty() )
{
QDir d( extra );
if ( d.cd( "../share/locale" ) ) // Often /usr/local/bin/../share/locale -> /usr/local/share/locale
{
_add_localedirs( candidatePaths, d.canonicalPath() );
}
}
_add_localedirs( candidatePaths, QDir().canonicalPath() ); // .
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& options = std::string() );
int target_env_call( const std::string& command,
const std::string& stdin = std::string(),
int timeout = 0 );
int target_env_call( const std::string& command, const std::string& stdin = std::string(), int timeout = 0 );
int target_env_call( const boost::python::list& args,
const std::string& stdin = std::string(),
int timeout = 0 );
int target_env_call( const boost::python::list& args, const std::string& stdin = std::string(), int timeout = 0 );
int check_target_env_call( const std::string& command,
const std::string& stdin = std::string(),
int timeout = 0 );
int check_target_env_call( const std::string& command, const std::string& stdin = std::string(), int timeout = 0 );
int check_target_env_call( const boost::python::list& args,
const std::string& stdin = std::string(),
int timeout = 0 );
int check_target_env_call( const boost::python::list& args, const std::string& stdin = std::string(), int timeout = 0 );
std::string check_target_env_output( const std::string& command,
const std::string& stdin = std::string(),
int timeout = 0 );
std::string
check_target_env_output( const std::string& command, const std::string& stdin = std::string(), int timeout = 0 );
std::string check_target_env_output( const boost::python::list& args,
const std::string& stdin = std::string(),
int timeout = 0 );
std::string
check_target_env_output( const boost::python::list& args, const std::string& stdin = std::string(), int timeout = 0 );
std::string obscure( const std::string& string );
@ -85,6 +75,6 @@ private:
Calamares::PythonJob* m_parent;
};
}
} // namespace CalamaresPython
#endif // PYTHONJOBAPI_H

View File

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