mirror of https://github.com/cutefishos/calamares
Add optional Python jobs support to libcalamares.
parent
754ef7dbbc
commit
b924aeef2b
@ -0,0 +1,113 @@
|
||||
/* === 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"
|
||||
|
||||
#include "PythonJobHelper.h"
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QDir>
|
||||
|
||||
#undef slots
|
||||
#include <boost/python.hpp>
|
||||
|
||||
|
||||
namespace bp = boost::python;
|
||||
namespace Calamares {
|
||||
|
||||
|
||||
PythonJob::PythonJob( const QString& scriptFile,
|
||||
const QString& workingPath,
|
||||
QObject* parent )
|
||||
: Job( parent )
|
||||
, m_scriptFile( scriptFile )
|
||||
, m_workingPath( workingPath )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
|
||||
bp::object result = bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(),
|
||||
scriptNamespace,
|
||||
scriptNamespace );
|
||||
|
||||
bp::object entryPoint = scriptNamespace[ "calamares_main" ];
|
||||
|
||||
QString message = QString::fromStdString( bp::extract< std::string >( entryPoint() ) );
|
||||
|
||||
cDebug() << "Python job" << prettyName() << "finished with message" << message;
|
||||
}
|
||||
catch ( bp::error_already_set e )
|
||||
{
|
||||
return JobResult::error( tr( "Boost.Python error" ) );
|
||||
}
|
||||
|
||||
return JobResult::ok();
|
||||
}
|
||||
|
||||
|
||||
PythonJobHelper*
|
||||
PythonJob::helper()
|
||||
{
|
||||
return PythonJobHelper::s_instance;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Calamares
|
@ -0,0 +1,49 @@
|
||||
/* === 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/>.
|
||||
*/
|
||||
|
||||
#ifndef CALAMARES_PYTHONJOB_H
|
||||
#define CALAMARES_PYTHONJOB_H
|
||||
|
||||
#include "Job.h"
|
||||
|
||||
namespace Calamares {
|
||||
|
||||
class PythonJobHelper;
|
||||
|
||||
class PythonJob : public Job
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit PythonJob( const QString& scriptFile,
|
||||
const QString& workingPath,
|
||||
QObject* parent = nullptr );
|
||||
virtual ~PythonJob();
|
||||
|
||||
QString prettyName() const override;
|
||||
JobResult exec() override;
|
||||
|
||||
private:
|
||||
friend class PythonJobHelper;
|
||||
PythonJobHelper* helper();
|
||||
QString m_scriptFile;
|
||||
QString m_workingPath;
|
||||
};
|
||||
|
||||
} // namespace Calamares
|
||||
|
||||
#endif // CALAMARES_PYTHONJOB_H
|
@ -0,0 +1,67 @@
|
||||
/* === 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 "PythonJobHelper.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <boost/python.hpp>
|
||||
|
||||
namespace bp = boost::python;
|
||||
|
||||
namespace Calamares {
|
||||
|
||||
PythonJobHelper* PythonJobHelper::s_instance = nullptr;
|
||||
|
||||
PythonJobHelper::PythonJobHelper( QObject* parent )
|
||||
: QObject( parent )
|
||||
{
|
||||
// Let's make extra sure we only call Py_Initialize once
|
||||
if ( !s_instance )
|
||||
{
|
||||
Py_Initialize();
|
||||
|
||||
m_mainModule = bp::import( "__main__" );
|
||||
m_mainNamespace = m_mainModule.attr( "__dict__" );
|
||||
}
|
||||
else
|
||||
{
|
||||
cDebug() << "WARNING: creating PythonJobHelper more than once. This is very bad.";
|
||||
return;
|
||||
}
|
||||
|
||||
s_instance = this;
|
||||
}
|
||||
|
||||
PythonJobHelper::~PythonJobHelper()
|
||||
{}
|
||||
|
||||
|
||||
boost::python::object
|
||||
PythonJobHelper::createCleanNamespace()
|
||||
{
|
||||
// To make sure we run each script with a clean namespace, we only fetch the
|
||||
// builtin namespace from the interpreter as it was when freshly initialized.
|
||||
bp::dict scriptNamespace;
|
||||
scriptNamespace[ "__builtins__" ] = m_mainNamespace[ "__builtins__" ];
|
||||
|
||||
return scriptNamespace;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Calamares
|
@ -0,0 +1,48 @@
|
||||
/* === 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/>.
|
||||
*/
|
||||
|
||||
#ifndef CALAMARES_PYTHONJOBHELPER_H
|
||||
#define CALAMARES_PYTHONJOBHELPER_H
|
||||
|
||||
#include "PythonJob.h"
|
||||
|
||||
#undef slots
|
||||
#include <boost/python/object.hpp>
|
||||
|
||||
namespace Calamares {
|
||||
|
||||
class PythonJobHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit PythonJobHelper( QObject* parent = nullptr );
|
||||
virtual ~PythonJobHelper();
|
||||
|
||||
boost::python::object createCleanNamespace();
|
||||
|
||||
private:
|
||||
friend PythonJobHelper* PythonJob::helper();
|
||||
static PythonJobHelper* s_instance;
|
||||
|
||||
boost::python::object m_mainModule;
|
||||
boost::python::object m_mainNamespace;
|
||||
};
|
||||
|
||||
} // namespace Calamares
|
||||
|
||||
#endif // CALAMARES_PYTHONJOBHELPER_H
|
Loading…
Reference in New Issue