From 96580e5c40a7540935c6ece8946eb174f43023e3 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 12 Feb 2020 10:28:48 +0100 Subject: [PATCH 1/9] [libcalamares] Convenience header for Boost and its warnings --- src/libcalamares/utils/BoostPython.h | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/libcalamares/utils/BoostPython.h diff --git a/src/libcalamares/utils/BoostPython.h b/src/libcalamares/utils/BoostPython.h new file mode 100644 index 000000000..7bd8865da --- /dev/null +++ b/src/libcalamares/utils/BoostPython.h @@ -0,0 +1,73 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019-2020, Adriaan de Groot + * + * 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 . + */ + +/* + * The Python and Boost::Python headers are not C++14 warning-proof, especially + * with picky compilers like Clang 8 and 9. Since we use Clang for the + * find-all-the-warnings case, switch those warnings off for + * the we-can't-change-them system headers. + * + * This convenience header handles including all the bits we need for + * Python support, while silencing warnings. + */ +#ifndef UTILS_BOOSTPYTHON_H +#define UTILS_BOOSTPYTHON_H + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wreserved-id-macro" +#pragma clang diagnostic ignored "-Wold-style-cast" +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" +#pragma clang diagnostic ignored "-Wextra-semi-stmt" +#pragma clang diagnostic ignored "-Wall" +#pragma clang diagnostic ignored "-Wimplicit-float-conversion" +#pragma clang diagnostic ignored "-Wundef" +#pragma clang diagnostic ignored "-Wdeprecated-dynamic-exception-spec" +#pragma clang diagnostic ignored "-Wshadow-field-in-constructor" +#pragma clang diagnostic ignored "-Wshadow" +#pragma clang diagnostic ignored "-Wmissing-noreturn" +#pragma clang diagnostic ignored "-Wcast-qual" +#pragma clang diagnostic ignored "-Wcast-align" +#pragma clang diagnostic ignored "-Wsign-conversion" +#pragma clang diagnostic ignored "-Wdouble-promotion" +#pragma clang diagnostic ignored "-Wredundant-parens" +#pragma clang diagnostic ignored "-Wweak-vtables" +#pragma clang diagnostic ignored "-Wdeprecated" +#pragma clang diagnostic ignored "-Wmissing-field-initializers" +#pragma clang diagnostic ignored "-Wdisabled-macro-expansion" +#pragma clang diagnostic ignored "-Wdocumentation" +#pragma clang diagnostic ignored "-Wcomma" +#pragma clang diagnostic ignored "-Wunused-parameter" +#pragma clang diagnostic ignored "-Wunused-template" + +// Actually for Python headers +#pragma clang diagnostic ignored "-Wreserved-id-macro" +#endif + +#undef slots +#include +#include +#include +#include +#include + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#endif From f3e7fe5eb416e38b228ba6f27b9eca1ee8d4af75 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 12 Feb 2020 10:32:10 +0100 Subject: [PATCH 2/9] [libcalamares] Use more specific include --- src/libcalamares/PythonJob.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcalamares/PythonJob.h b/src/libcalamares/PythonJob.h index c63daacdc..103015932 100644 --- a/src/libcalamares/PythonJob.h +++ b/src/libcalamares/PythonJob.h @@ -23,7 +23,7 @@ #include "modulesystem/InstanceKey.h" -#include +#include namespace CalamaresPython { From d42e757576543f31b0cc9ac4cab74ca7f9da1370 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 12 Feb 2020 10:48:19 +0100 Subject: [PATCH 3/9] [libcalamares] Simplify includes - CalamaresVersion used by the job, not the API presented to Python. - Untangle Qt includes from there. --- src/libcalamares/PythonJob.cpp | 1 + src/libcalamares/PythonJobApi.h | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index bff7fcc74..f5c033826 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -19,6 +19,7 @@ #include "PythonJob.h" +#include "CalamaresVersion.h" #include "GlobalStorage.h" #include "JobQueue.h" #include "PythonHelper.h" diff --git a/src/libcalamares/PythonJobApi.h b/src/libcalamares/PythonJobApi.h index 981527951..80a32b930 100644 --- a/src/libcalamares/PythonJobApi.h +++ b/src/libcalamares/PythonJobApi.h @@ -20,9 +20,7 @@ #ifndef PYTHONJOBAPI_H #define PYTHONJOBAPI_H -#include "CalamaresVersion.h" - -#include "PythonJob.h" +#include "qglobal.h" // For qreal #undef slots #include "utils/boost-warnings.h" @@ -32,6 +30,11 @@ #pragma clang diagnostic pop #endif +namespace Calamares +{ +class PythonJob; +} + namespace CalamaresPython { From f8998834cf9ce175b4860980b5da65572e63baa6 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 12 Feb 2020 10:55:36 +0100 Subject: [PATCH 4/9] [libcalamares] Simplify includes (no Python used in JobQueue) --- src/libcalamares/JobQueue.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/libcalamares/JobQueue.cpp b/src/libcalamares/JobQueue.cpp index 6772671b7..2690769db 100644 --- a/src/libcalamares/JobQueue.cpp +++ b/src/libcalamares/JobQueue.cpp @@ -19,15 +19,11 @@ #include "JobQueue.h" +#include "CalamaresConfig.h" #include "GlobalStorage.h" #include "Job.h" #include "utils/Logger.h" -#include "CalamaresConfig.h" -#ifdef WITH_PYTHON -#include "PythonHelper.h" -#endif - #include namespace Calamares From 95722541d0482cb8c2b11f8c5313cce08b0244ab Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 12 Feb 2020 11:02:38 +0100 Subject: [PATCH 5/9] [libcalamares] Untangle Python includes - Use BoostPython.h to manage overall includes - Remove local home-grown variations --- src/libcalamares/PythonHelper.cpp | 8 -------- src/libcalamares/PythonHelper.h | 12 +----------- src/libcalamares/PythonJob.cpp | 11 ++--------- src/libcalamares/PythonJobApi.cpp | 13 ++----------- src/libcalamares/PythonJobApi.h | 8 +------- 5 files changed, 6 insertions(+), 46 deletions(-) diff --git a/src/libcalamares/PythonHelper.cpp b/src/libcalamares/PythonHelper.cpp index 26a57ec14..d08fd66f1 100644 --- a/src/libcalamares/PythonHelper.cpp +++ b/src/libcalamares/PythonHelper.cpp @@ -25,14 +25,6 @@ #include #include -#undef slots -#include "utils/boost-warnings.h" -#include - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - namespace bp = boost::python; namespace CalamaresPython diff --git a/src/libcalamares/PythonHelper.h b/src/libcalamares/PythonHelper.h index 9035ba6d4..ed1d56151 100644 --- a/src/libcalamares/PythonHelper.h +++ b/src/libcalamares/PythonHelper.h @@ -21,20 +21,10 @@ #define CALAMARES_PYTHONJOBHELPER_H #include "PythonJob.h" +#include "utils/BoostPython.h" #include -#undef slots -#include "utils/boost-warnings.h" - -#include -#include -#include - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - namespace CalamaresPython { diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index f5c033826..db14c10f8 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -23,19 +23,12 @@ #include "GlobalStorage.h" #include "JobQueue.h" #include "PythonHelper.h" +#include "PythonJobApi.h" +#include "utils/BoostPython.h" #include "utils/Logger.h" #include -#undef slots -#include "utils/boost-warnings.h" - -#include -#include - -#include "PythonJobApi.h" - - namespace bp = boost::python; BOOST_PYTHON_FUNCTION_OVERLOADS( mount_overloads, CalamaresPython::mount, 2, 4 ); diff --git a/src/libcalamares/PythonJobApi.cpp b/src/libcalamares/PythonJobApi.cpp index 7b65e979c..e0bb686cd 100644 --- a/src/libcalamares/PythonJobApi.cpp +++ b/src/libcalamares/PythonJobApi.cpp @@ -19,26 +19,17 @@ #include "PythonJobApi.h" +#include "GlobalStorage.h" +#include "JobQueue.h" #include "PythonHelper.h" #include "utils/CalamaresUtilsSystem.h" #include "utils/Logger.h" #include "utils/String.h" -#include "GlobalStorage.h" -#include "JobQueue.h" - #include #include #include -#undef slots -#include "utils/boost-warnings.h" -#include - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - namespace bp = boost::python; static int diff --git a/src/libcalamares/PythonJobApi.h b/src/libcalamares/PythonJobApi.h index 80a32b930..941527d66 100644 --- a/src/libcalamares/PythonJobApi.h +++ b/src/libcalamares/PythonJobApi.h @@ -22,13 +22,7 @@ #include "qglobal.h" // For qreal -#undef slots -#include "utils/boost-warnings.h" -#include - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif +#include "utils/BoostPython.h" namespace Calamares { From 3b35ca7bb99201232288fbbdc6cdf288c8065f4a Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 12 Feb 2020 11:04:15 +0100 Subject: [PATCH 6/9] [libcalamares] Simplify includes - PythonHelper.h already pulls in all the Python machinery --- src/libcalamares/GlobalStorage.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/libcalamares/GlobalStorage.cpp b/src/libcalamares/GlobalStorage.cpp index 5094ad2fd..2241a3c7a 100644 --- a/src/libcalamares/GlobalStorage.cpp +++ b/src/libcalamares/GlobalStorage.cpp @@ -29,13 +29,6 @@ #ifdef WITH_PYTHON #include "PythonHelper.h" - - -#undef slots -#include -#include - -namespace bp = boost::python; #endif using CalamaresUtils::operator""_MiB; From 8181808bec342bd2dbeb3f1998098c505af00e6a Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 12 Feb 2020 11:25:10 +0100 Subject: [PATCH 7/9] [libcalamares] Fix build - drop now-obsolete boost-warnings.h - add missing namespace alias to GlobalStorage.h (removed accidentally in previous commit) --- src/libcalamares/GlobalStorage.cpp | 1 + src/libcalamares/utils/boost-warnings.h | 30 ------------------------- 2 files changed, 1 insertion(+), 30 deletions(-) delete mode 100644 src/libcalamares/utils/boost-warnings.h diff --git a/src/libcalamares/GlobalStorage.cpp b/src/libcalamares/GlobalStorage.cpp index 2241a3c7a..2ccaf79b4 100644 --- a/src/libcalamares/GlobalStorage.cpp +++ b/src/libcalamares/GlobalStorage.cpp @@ -29,6 +29,7 @@ #ifdef WITH_PYTHON #include "PythonHelper.h" +namespace bp = boost::python; #endif using CalamaresUtils::operator""_MiB; diff --git a/src/libcalamares/utils/boost-warnings.h b/src/libcalamares/utils/boost-warnings.h deleted file mode 100644 index 69fb9ea30..000000000 --- a/src/libcalamares/utils/boost-warnings.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wreserved-id-macro" -#pragma clang diagnostic ignored "-Wold-style-cast" -#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" -#pragma clang diagnostic ignored "-Wextra-semi-stmt" -#pragma clang diagnostic ignored "-Wall" -#pragma clang diagnostic ignored "-Wimplicit-float-conversion" -#pragma clang diagnostic ignored "-Wundef" -#pragma clang diagnostic ignored "-Wdeprecated-dynamic-exception-spec" -#pragma clang diagnostic ignored "-Wshadow-field-in-constructor" -#pragma clang diagnostic ignored "-Wshadow" -#pragma clang diagnostic ignored "-Wmissing-noreturn" -#pragma clang diagnostic ignored "-Wcast-qual" -#pragma clang diagnostic ignored "-Wcast-align" -#pragma clang diagnostic ignored "-Wsign-conversion" -#pragma clang diagnostic ignored "-Wdouble-promotion" -#pragma clang diagnostic ignored "-Wredundant-parens" -#pragma clang diagnostic ignored "-Wweak-vtables" -#pragma clang diagnostic ignored "-Wdeprecated" -#pragma clang diagnostic ignored "-Wmissing-field-initializers" -#pragma clang diagnostic ignored "-Wdisabled-macro-expansion" -#pragma clang diagnostic ignored "-Wdocumentation" -#pragma clang diagnostic ignored "-Wcomma" -#pragma clang diagnostic ignored "-Wunused-parameter" -#pragma clang diagnostic ignored "-Wunused-template" - -// Actually for Python headers -#pragma clang diagnostic ignored "-Wreserved-id-macro" -#endif From c1151cbcfa2c0d9b129ae5fc8b966aa8c3896c16 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 12 Feb 2020 12:08:46 +0100 Subject: [PATCH 8/9] [libcalamares] Update copyright info --- src/libcalamares/PythonHelper.cpp | 2 +- src/libcalamares/PythonHelper.h | 2 +- src/libcalamares/PythonJob.cpp | 4 ++-- src/libcalamares/PythonJob.h | 1 + src/libcalamares/PythonJobApi.cpp | 2 +- src/libcalamares/PythonJobApi.h | 2 +- 6 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/libcalamares/PythonHelper.cpp b/src/libcalamares/PythonHelper.cpp index d08fd66f1..88008692e 100644 --- a/src/libcalamares/PythonHelper.cpp +++ b/src/libcalamares/PythonHelper.cpp @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014, Teo Mrnjavac - * Copyright 2017-2018, Adriaan de Groot + * Copyright 2017-2018, 2020, Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/libcalamares/PythonHelper.h b/src/libcalamares/PythonHelper.h index ed1d56151..bb37eb868 100644 --- a/src/libcalamares/PythonHelper.h +++ b/src/libcalamares/PythonHelper.h @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014, Teo Mrnjavac - * Copyright 2018, Adriaan de Groot + * Copyright 2018, 2020, Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index db14c10f8..d94a20981 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2016, Teo Mrnjavac - * Copyright 2018, Adriaan de Groot + * Copyright 2018, 2020, Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -176,7 +176,7 @@ PythonJob::PythonJob( const ModuleSystem::InstanceKey& instance, , m_workingPath( workingPath ) , m_description() , m_configurationMap( moduleConfiguration ) - , m_weight( (instance.module() == QStringLiteral( "unpackfs" )) ? 12.0 : 1.0 ) + , m_weight( ( instance.module() == QStringLiteral( "unpackfs" ) ) ? 12.0 : 1.0 ) { } diff --git a/src/libcalamares/PythonJob.h b/src/libcalamares/PythonJob.h index 103015932..7cd1b7165 100644 --- a/src/libcalamares/PythonJob.h +++ b/src/libcalamares/PythonJob.h @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014, Teo Mrnjavac + * Copyright 2020, Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/libcalamares/PythonJobApi.cpp b/src/libcalamares/PythonJobApi.cpp index e0bb686cd..132a9dcf5 100644 --- a/src/libcalamares/PythonJobApi.cpp +++ b/src/libcalamares/PythonJobApi.cpp @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2016, Teo Mrnjavac - * Copyright 2017-2019, Adriaan de Groot + * Copyright 2017-2020, Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/libcalamares/PythonJobApi.h b/src/libcalamares/PythonJobApi.h index 941527d66..6fb27cd62 100644 --- a/src/libcalamares/PythonJobApi.h +++ b/src/libcalamares/PythonJobApi.h @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2016, Teo Mrnjavac - * Copyright 2017-2018, Adriaan de Groot + * Copyright 2017-2018, 2020, Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 940860107445dbdc499a4a7f737ba84e40948674 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 12 Feb 2020 12:37:43 +0100 Subject: [PATCH 9/9] [libcalamares] Move Python wrapper - Take the Python wrapper for GlobalStorage out of the GlobalStorage.h header and add it to PythonHelper instead, saving some work in all the cases that only GS is interesting, not the Python bits. --- src/libcalamares/GlobalStorage.cpp | 77 ------------------------------ src/libcalamares/GlobalStorage.h | 43 ----------------- src/libcalamares/PythonHelper.cpp | 63 ++++++++++++++++++++++++ src/libcalamares/PythonHelper.h | 27 +++++++++++ 4 files changed, 90 insertions(+), 120 deletions(-) diff --git a/src/libcalamares/GlobalStorage.cpp b/src/libcalamares/GlobalStorage.cpp index 2ccaf79b4..428b01103 100644 --- a/src/libcalamares/GlobalStorage.cpp +++ b/src/libcalamares/GlobalStorage.cpp @@ -27,11 +27,6 @@ #include #include -#ifdef WITH_PYTHON -#include "PythonHelper.h" -namespace bp = boost::python; -#endif - using CalamaresUtils::operator""_MiB; namespace Calamares @@ -161,75 +156,3 @@ GlobalStorage::loadYaml( const QString& filename ) } // namespace Calamares - -#ifdef WITH_PYTHON - -namespace CalamaresPython -{ - -Calamares::GlobalStorage* GlobalStoragePythonWrapper::s_gs_instance = nullptr; - -// The special handling for nullptr is only for the testing -// script for the python bindings, which passes in None; -// normal use will have a GlobalStorage from JobQueue::instance() -// passed in. Testing use will leak the allocated GlobalStorage -// object, but that's OK for testing. -GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs ) - : m_gs( gs ? gs : s_gs_instance ) -{ - if ( !m_gs ) - { - s_gs_instance = new Calamares::GlobalStorage; - m_gs = s_gs_instance; - } -} - -bool -GlobalStoragePythonWrapper::contains( const std::string& key ) const -{ - return m_gs->contains( QString::fromStdString( key ) ); -} - - -int -GlobalStoragePythonWrapper::count() const -{ - return m_gs->count(); -} - - -void -GlobalStoragePythonWrapper::insert( const std::string& key, const bp::object& value ) -{ - m_gs->insert( QString::fromStdString( key ), CalamaresPython::variantFromPyObject( value ) ); -} - -bp::list -GlobalStoragePythonWrapper::keys() const -{ - bp::list pyList; - const auto keys = m_gs->keys(); - for ( const QString& key : keys ) - { - pyList.append( key.toStdString() ); - } - return pyList; -} - - -int -GlobalStoragePythonWrapper::remove( const std::string& key ) -{ - return m_gs->remove( QString::fromStdString( key ) ); -} - - -bp::object -GlobalStoragePythonWrapper::value( const std::string& key ) const -{ - return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) ); -} - -} // namespace CalamaresPython - -#endif // WITH_PYTHON diff --git a/src/libcalamares/GlobalStorage.h b/src/libcalamares/GlobalStorage.h index b070e23f6..bef9ec1cc 100644 --- a/src/libcalamares/GlobalStorage.h +++ b/src/libcalamares/GlobalStorage.h @@ -26,20 +26,6 @@ #include #include -#ifdef WITH_PYTHON -namespace boost -{ -namespace python -{ -namespace api -{ -class object; -} -class list; -} // namespace python -} // namespace boost -#endif - namespace Calamares { @@ -106,33 +92,4 @@ private: } // namespace Calamares -#ifdef WITH_PYTHON -namespace CalamaresPython -{ - -class GlobalStoragePythonWrapper -{ -public: - explicit GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs ); - - bool contains( const std::string& key ) const; - int count() const; - void insert( const std::string& key, const boost::python::api::object& value ); - boost::python::list keys() const; - int remove( const std::string& key ); - boost::python::api::object value( const std::string& key ) const; - - // This is a helper for scripts that do not go through - // the JobQueue (i.e. the module testpython script), - // which allocate their own (singleton) GlobalStorage. - static Calamares::GlobalStorage* globalStorageInstance() { return s_gs_instance; } - -private: - Calamares::GlobalStorage* m_gs; - static Calamares::GlobalStorage* s_gs_instance; // See globalStorageInstance() -}; - -} // namespace CalamaresPython -#endif - #endif // CALAMARES_GLOBALSTORAGE_H diff --git a/src/libcalamares/PythonHelper.cpp b/src/libcalamares/PythonHelper.cpp index 88008692e..d9db8581e 100644 --- a/src/libcalamares/PythonHelper.cpp +++ b/src/libcalamares/PythonHelper.cpp @@ -19,6 +19,7 @@ #include "PythonHelper.h" +#include "GlobalStorage.h" #include "utils/Dirs.h" #include "utils/Logger.h" @@ -390,5 +391,67 @@ Helper::handleLastError() return QString( "
%1
" ).arg( msgList.join( "
" ) ); } +Calamares::GlobalStorage* GlobalStoragePythonWrapper::s_gs_instance = nullptr; + +// The special handling for nullptr is only for the testing +// script for the python bindings, which passes in None; +// normal use will have a GlobalStorage from JobQueue::instance() +// passed in. Testing use will leak the allocated GlobalStorage +// object, but that's OK for testing. +GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs ) + : m_gs( gs ? gs : s_gs_instance ) +{ + if ( !m_gs ) + { + s_gs_instance = new Calamares::GlobalStorage; + m_gs = s_gs_instance; + } +} + +bool +GlobalStoragePythonWrapper::contains( const std::string& key ) const +{ + return m_gs->contains( QString::fromStdString( key ) ); +} + + +int +GlobalStoragePythonWrapper::count() const +{ + return m_gs->count(); +} + + +void +GlobalStoragePythonWrapper::insert( const std::string& key, const bp::object& value ) +{ + m_gs->insert( QString::fromStdString( key ), CalamaresPython::variantFromPyObject( value ) ); +} + +bp::list +GlobalStoragePythonWrapper::keys() const +{ + bp::list pyList; + const auto keys = m_gs->keys(); + for ( const QString& key : keys ) + { + pyList.append( key.toStdString() ); + } + return pyList; +} + + +int +GlobalStoragePythonWrapper::remove( const std::string& key ) +{ + return m_gs->remove( QString::fromStdString( key ) ); +} + + +bp::object +GlobalStoragePythonWrapper::value( const std::string& key ) const +{ + return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) ); +} } // namespace CalamaresPython diff --git a/src/libcalamares/PythonHelper.h b/src/libcalamares/PythonHelper.h index bb37eb868..418c75e5f 100644 --- a/src/libcalamares/PythonHelper.h +++ b/src/libcalamares/PythonHelper.h @@ -25,6 +25,11 @@ #include +namespace Calamares +{ +class GlobalStorage; +} + namespace CalamaresPython { @@ -62,6 +67,28 @@ private: QStringList m_pythonPaths; }; +class GlobalStoragePythonWrapper +{ +public: + explicit GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs ); + + bool contains( const std::string& key ) const; + int count() const; + void insert( const std::string& key, const boost::python::api::object& value ); + boost::python::list keys() const; + int remove( const std::string& key ); + boost::python::api::object value( const std::string& key ) const; + + // This is a helper for scripts that do not go through + // the JobQueue (i.e. the module testpython script), + // which allocate their own (singleton) GlobalStorage. + static Calamares::GlobalStorage* globalStorageInstance() { return s_gs_instance; } + +private: + Calamares::GlobalStorage* m_gs; + static Calamares::GlobalStorage* s_gs_instance; // See globalStorageInstance() +}; + } // namespace CalamaresPython #endif // CALAMARES_PYTHONJOBHELPER_H