2014-07-16 16:07:32 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
|
|
|
|
*
|
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PythonJob.h"
|
|
|
|
|
2014-07-18 14:27:59 +02:00
|
|
|
#include "PythonHelper.h"
|
2014-07-16 16:07:32 +02:00
|
|
|
#include "utils/Logger.h"
|
2014-07-21 17:08:06 +02:00
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
2014-07-16 16:07:32 +02:00
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
#undef slots
|
|
|
|
#include <boost/python.hpp>
|
2014-07-29 20:18:02 +02:00
|
|
|
#include <boost/python/args.hpp>
|
2014-07-16 16:07:32 +02:00
|
|
|
|
2014-07-17 17:52:02 +02:00
|
|
|
#include "PythonJobApi.h"
|
|
|
|
|
2014-07-16 16:07:32 +02:00
|
|
|
|
|
|
|
namespace bp = boost::python;
|
2014-07-17 17:52:02 +02:00
|
|
|
|
2014-07-29 20:18:02 +02:00
|
|
|
BOOST_PYTHON_FUNCTION_OVERLOADS( mount_overloads,
|
|
|
|
CalamaresPython::mount,
|
|
|
|
2, 4 );
|
|
|
|
BOOST_PYTHON_FUNCTION_OVERLOADS( chroot_call_str_overloads,
|
|
|
|
CalamaresPython::chroot_call,
|
|
|
|
1, 3 );
|
|
|
|
BOOST_PYTHON_FUNCTION_OVERLOADS( chroot_call_list_overloads,
|
|
|
|
CalamaresPython::chroot_call,
|
|
|
|
1, 3 );
|
2014-08-04 16:03:33 +02:00
|
|
|
BOOST_PYTHON_FUNCTION_OVERLOADS( check_chroot_call_str_overloads,
|
|
|
|
CalamaresPython::check_chroot_call,
|
|
|
|
1, 3 );
|
|
|
|
BOOST_PYTHON_FUNCTION_OVERLOADS( check_chroot_call_list_overloads,
|
|
|
|
CalamaresPython::check_chroot_call,
|
|
|
|
1, 3 );
|
2014-07-17 17:52:02 +02:00
|
|
|
BOOST_PYTHON_MODULE( libcalamares )
|
|
|
|
{
|
2014-08-04 17:04:36 +02:00
|
|
|
bp::object package = bp::scope();
|
|
|
|
package.attr( "__path__" ) = "libcalamares";
|
|
|
|
|
2014-07-29 13:16:46 +02:00
|
|
|
bp::scope().attr( "ORGANIZATION_NAME" ) = CALAMARES_ORGANIZATION_NAME;
|
|
|
|
bp::scope().attr( "ORGANIZATION_DOMAIN" ) = CALAMARES_ORGANIZATION_DOMAIN;
|
|
|
|
bp::scope().attr( "APPLICATION_NAME" ) = CALAMARES_APPLICATION_NAME;
|
|
|
|
bp::scope().attr( "VERSION" ) = CALAMARES_VERSION;
|
|
|
|
bp::scope().attr( "VERSION_SHORT" ) = CALAMARES_VERSION_SHORT;
|
2014-07-17 17:52:02 +02:00
|
|
|
|
2014-07-23 13:02:42 +02:00
|
|
|
bp::class_< CalamaresPython::PythonJobInterface >( "Job", bp::init< Calamares::PythonJob* >() )
|
2014-08-05 13:23:04 +02:00
|
|
|
.def_readonly( "module_name", &CalamaresPython::PythonJobInterface::moduleName )
|
|
|
|
.def_readonly( "pretty_name", &CalamaresPython::PythonJobInterface::prettyName )
|
|
|
|
.def_readonly( "working_path", &CalamaresPython::PythonJobInterface::workingPath )
|
2014-07-22 18:05:58 +02:00
|
|
|
.def_readonly( "configuration", &CalamaresPython::PythonJobInterface::configuration )
|
2014-08-05 13:23:04 +02:00
|
|
|
.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."
|
|
|
|
);
|
2014-07-21 17:08:06 +02:00
|
|
|
|
|
|
|
bp::class_< Calamares::GlobalStorage >( "GlobalStorage", bp::init<>() )
|
|
|
|
.def( "contains", &Calamares::GlobalStorage::python_contains )
|
|
|
|
.def( "count", &Calamares::GlobalStorage::count )
|
|
|
|
.def( "insert", &Calamares::GlobalStorage::python_insert )
|
|
|
|
.def( "keys", &Calamares::GlobalStorage::python_keys )
|
|
|
|
.def( "remove", &Calamares::GlobalStorage::python_remove )
|
|
|
|
.def( "value", &Calamares::GlobalStorage::python_value );
|
2014-07-29 20:18:02 +02:00
|
|
|
|
|
|
|
// libcalamares.utils submodule starts here
|
2014-08-04 17:06:16 +02:00
|
|
|
bp::object utilsModule( bp::handle<>( bp::borrowed( PyImport_AddModule( "libcalamares.utils" ) ) ) );
|
|
|
|
bp::scope().attr( "utils" ) = utilsModule;
|
|
|
|
bp::scope utilsScope = utilsModule;
|
2014-07-29 20:18:02 +02:00
|
|
|
Q_UNUSED( utilsScope );
|
|
|
|
|
2014-08-05 13:23:04 +02:00
|
|
|
bp::def(
|
|
|
|
"debug",
|
|
|
|
&CalamaresPython::debug,
|
|
|
|
bp::args( "s" ),
|
|
|
|
"Writes the given string to the Calamares debug stream."
|
|
|
|
);
|
2014-08-04 16:03:33 +02:00
|
|
|
bp::def(
|
|
|
|
"mount",
|
|
|
|
&CalamaresPython::mount,
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
bp::def(
|
|
|
|
"chroot_call",
|
|
|
|
static_cast< int (*)( const std::string&,
|
|
|
|
const std::string&,
|
|
|
|
int ) >( &CalamaresPython::chroot_call ),
|
|
|
|
chroot_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(
|
|
|
|
"chroot_call",
|
|
|
|
static_cast< int (*)( const bp::list&,
|
|
|
|
const std::string&,
|
|
|
|
int ) >( &CalamaresPython::chroot_call ),
|
|
|
|
chroot_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_chroot_call",
|
|
|
|
static_cast< int (*)( const std::string&,
|
|
|
|
const std::string&,
|
|
|
|
int ) >( &CalamaresPython::check_chroot_call ),
|
|
|
|
check_chroot_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(
|
|
|
|
"check_chroot_call",
|
|
|
|
static_cast< int (*)( const bp::list&,
|
|
|
|
const std::string&,
|
|
|
|
int ) >( &CalamaresPython::check_chroot_call ),
|
|
|
|
check_chroot_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."
|
|
|
|
)
|
|
|
|
);
|
2014-07-17 17:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-16 16:07:32 +02:00
|
|
|
namespace Calamares {
|
|
|
|
|
|
|
|
|
|
|
|
PythonJob::PythonJob( const QString& scriptFile,
|
|
|
|
const QString& workingPath,
|
2014-07-18 14:27:59 +02:00
|
|
|
const QVariantMap& moduleConfiguration,
|
2014-07-16 16:07:32 +02:00
|
|
|
QObject* parent )
|
|
|
|
: Job( parent )
|
|
|
|
, m_scriptFile( scriptFile )
|
|
|
|
, m_workingPath( workingPath )
|
2014-07-18 14:27:59 +02:00
|
|
|
, m_configurationMap( moduleConfiguration )
|
2014-07-16 16:07:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PythonJob::~PythonJob()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
PythonJob::prettyName() const
|
|
|
|
{
|
|
|
|
return tr( "Run script %1" )
|
|
|
|
.arg( QDir( m_workingPath ).dirName() +
|
|
|
|
QDir::separator() +
|
|
|
|
m_scriptFile );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JobResult
|
|
|
|
PythonJob::exec()
|
|
|
|
{
|
|
|
|
// We assume m_scriptFile to be relative to m_workingPath.
|
|
|
|
QDir workingDir( m_workingPath );
|
|
|
|
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() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
QFileInfo scriptFI( workingDir.absoluteFilePath( m_scriptFile ) );
|
|
|
|
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." )
|
|
|
|
.arg( scriptFI.absoluteFilePath() )
|
|
|
|
.arg( prettyName() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bp::object scriptNamespace = helper()->createCleanNamespace();
|
|
|
|
|
2014-07-17 17:52:02 +02:00
|
|
|
bp::object calamaresModule = bp::import( "libcalamares" );
|
|
|
|
bp::dict calamaresNamespace = bp::extract< bp::dict >( calamaresModule.attr( "__dict__" ) );
|
|
|
|
|
2014-07-18 11:59:12 +02:00
|
|
|
calamaresNamespace[ "job" ] = CalamaresPython::PythonJobInterface( this );
|
2014-07-29 13:16:46 +02:00
|
|
|
calamaresNamespace[ "globalstorage" ] = bp::ptr( JobQueue::instance()->globalStorage() );
|
2014-07-17 17:52:02 +02:00
|
|
|
|
2014-07-28 15:17:28 +02:00
|
|
|
bp::object execResult = bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(),
|
2014-07-16 16:07:32 +02:00
|
|
|
scriptNamespace,
|
|
|
|
scriptNamespace );
|
|
|
|
|
2014-07-25 16:41:21 +02:00
|
|
|
bp::object entryPoint = scriptNamespace[ "run" ];
|
2014-07-16 16:07:32 +02:00
|
|
|
|
2014-07-28 15:17:28 +02:00
|
|
|
bp::object runResult = entryPoint();
|
2014-07-16 16:07:32 +02:00
|
|
|
|
2014-07-28 15:17:28 +02:00
|
|
|
if ( runResult.is_none() )
|
|
|
|
{
|
|
|
|
return JobResult::ok();
|
|
|
|
}
|
|
|
|
else // Something happened in the Python job
|
|
|
|
{
|
|
|
|
bp::tuple resultTuple = bp::extract< bp::tuple >( runResult );
|
|
|
|
QString message = QString::fromStdString( bp::extract< std::string >( resultTuple[ 0 ] ) );
|
|
|
|
QString description = QString::fromStdString( bp::extract< std::string >( resultTuple[ 1 ] ) );
|
2014-08-01 09:56:40 +02:00
|
|
|
return JobResult::error( message, description );
|
2014-07-28 15:17:28 +02:00
|
|
|
}
|
2014-07-16 16:07:32 +02:00
|
|
|
}
|
2014-07-17 17:52:02 +02:00
|
|
|
catch ( bp::error_already_set )
|
2014-07-16 16:07:32 +02:00
|
|
|
{
|
2014-07-17 17:52:02 +02:00
|
|
|
QString msg;
|
|
|
|
if ( PyErr_Occurred() )
|
|
|
|
{
|
|
|
|
msg = helper()->handleLastError();
|
|
|
|
}
|
|
|
|
bp::handle_exception();
|
|
|
|
PyErr_Clear();
|
2014-08-01 09:56:40 +02:00
|
|
|
return JobResult::error( tr( "Boost.Python error in job \"%1\"." ).arg( prettyName() ),
|
2014-07-17 17:52:02 +02:00
|
|
|
msg );
|
2014-07-16 16:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return JobResult::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-22 18:05:58 +02:00
|
|
|
void
|
2014-07-23 12:54:53 +02:00
|
|
|
PythonJob::emitProgress( qreal progressValue )
|
2014-07-22 18:05:58 +02:00
|
|
|
{
|
2014-07-23 12:54:53 +02:00
|
|
|
emit progress( progressValue );
|
2014-07-22 18:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-18 11:59:12 +02:00
|
|
|
CalamaresPython::Helper*
|
2014-07-16 16:07:32 +02:00
|
|
|
PythonJob::helper()
|
|
|
|
{
|
2014-07-18 11:59:12 +02:00
|
|
|
return CalamaresPython::Helper::s_instance;
|
2014-07-17 17:52:02 +02:00
|
|
|
|
2014-07-16 16:07:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Calamares
|