mirror of https://github.com/cutefishos/calamares
				
				
				
			
							parent
							
								
									1da7ba446d
								
							
						
					
					
						commit
						13fcf387c7
					
				@ -0,0 +1,120 @@
 | 
			
		||||
/* === 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 "Module.h"
 | 
			
		||||
 | 
			
		||||
#include "YamlUtils.h"
 | 
			
		||||
 | 
			
		||||
#include "utils/Logger.h"
 | 
			
		||||
 | 
			
		||||
#include <yaml-cpp/yaml.h>
 | 
			
		||||
 | 
			
		||||
#include <QFile>
 | 
			
		||||
#include <QString>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// Example module.conf
 | 
			
		||||
/*
 | 
			
		||||
---
 | 
			
		||||
type:      "core"     #core or view
 | 
			
		||||
name:      "foo"      #the module name. must be unique and same as the parent directory
 | 
			
		||||
interface: "qtplugin" #can be: qtplugin, python, process, ...
 | 
			
		||||
requires:  []         #list of module names that must also be loaded. only applies to
 | 
			
		||||
                      #binary plugins! these are actual link-time dependencies, not
 | 
			
		||||
                      #conceptual dependencies for the setup procedure
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
operator>>( const YAML::Node& node, Calamares::Module& m )
 | 
			
		||||
{
 | 
			
		||||
    m.m_name = QString::fromStdString( node[ "name" ].as< std::string >() );
 | 
			
		||||
 | 
			
		||||
    QString typeString = QString::fromStdString( node[ "type" ].as< std::string >() );
 | 
			
		||||
    if ( typeString == "core" )
 | 
			
		||||
    {
 | 
			
		||||
        m.m_type = Calamares::Module::Core;
 | 
			
		||||
    }
 | 
			
		||||
    else if ( typeString == "view" )
 | 
			
		||||
    {
 | 
			
		||||
        m.m_type = Calamares::Module::View;
 | 
			
		||||
        m.m_interface = Calamares::Module::QtPlugin;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ( m.m_type != Calamares::Module::View )
 | 
			
		||||
    {
 | 
			
		||||
        QString interfaceString = QString::fromStdString( node[ "interface" ].as< std::string >() );
 | 
			
		||||
        if ( interfaceString == "qtplugin" )
 | 
			
		||||
        {
 | 
			
		||||
            m.m_interface = Calamares::Module::QtPlugin;
 | 
			
		||||
        }
 | 
			
		||||
        else if ( interfaceString == "python" )
 | 
			
		||||
        {
 | 
			
		||||
            m.m_interface = Calamares::Module::Python;
 | 
			
		||||
        }
 | 
			
		||||
        else if ( interfaceString == "process" )
 | 
			
		||||
        {
 | 
			
		||||
            m.m_interface = Calamares::Module::Process;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ( node[ "requires" ] && node[ "requires" ].IsSequence() )
 | 
			
		||||
    {
 | 
			
		||||
        node[ "requires" ] >> m.m_requiredModules;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Calamares::Module*
 | 
			
		||||
Calamares::Module::loadFromFile( const QString& path )
 | 
			
		||||
{
 | 
			
		||||
    QFile metadataFile( path );
 | 
			
		||||
    if ( metadataFile.exists() && metadataFile.open( QFile::ReadOnly | QFile::Text ) )
 | 
			
		||||
    {
 | 
			
		||||
        QByteArray ba = metadataFile.readAll();
 | 
			
		||||
        cDebug() << Q_FUNC_INFO << "module metadata file: " << ba;
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            YAML::Node moduleDocument = YAML::Load( ba.constData() );
 | 
			
		||||
            if ( !moduleDocument.IsMap() )
 | 
			
		||||
            {
 | 
			
		||||
                cDebug() << Q_FUNC_INFO << "bad module metadata format"
 | 
			
		||||
                         << path;
 | 
			
		||||
                return nullptr;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            Module* m = new Module();
 | 
			
		||||
            moduleDocument >> *m;
 | 
			
		||||
            return m;
 | 
			
		||||
        }
 | 
			
		||||
        catch ( YAML::Exception& e )
 | 
			
		||||
        {
 | 
			
		||||
            cDebug() << "WARNING: YAML parser error " << e.what();
 | 
			
		||||
            return nullptr;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString
 | 
			
		||||
Calamares::Module::name()
 | 
			
		||||
{
 | 
			
		||||
    return m_name;
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,72 @@
 | 
			
		||||
/* === 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 CALAMARESMODULE_H
 | 
			
		||||
#define CALAMARESMODULE_H
 | 
			
		||||
 | 
			
		||||
#include "DllMacro.h"
 | 
			
		||||
 | 
			
		||||
#include <QStringList>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
namespace YAML
 | 
			
		||||
{
 | 
			
		||||
class Node;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
namespace Calamares
 | 
			
		||||
{
 | 
			
		||||
class Module;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void operator>>( const YAML::Node& node, Calamares::Module& m );
 | 
			
		||||
 | 
			
		||||
namespace Calamares
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
class UIDLLEXPORT Module
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    static Module* loadFromFile( const QString& path );
 | 
			
		||||
 | 
			
		||||
    QString name();
 | 
			
		||||
 | 
			
		||||
    enum Type
 | 
			
		||||
    {
 | 
			
		||||
        Core,
 | 
			
		||||
        View
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    enum Interface
 | 
			
		||||
    {
 | 
			
		||||
        QtPlugin,
 | 
			
		||||
        Python,
 | 
			
		||||
        Process
 | 
			
		||||
    };
 | 
			
		||||
private:
 | 
			
		||||
    QString m_name;
 | 
			
		||||
    Type m_type;
 | 
			
		||||
    Interface m_interface;
 | 
			
		||||
    QStringList m_requiredModules;
 | 
			
		||||
 | 
			
		||||
    friend void ::operator>>( const YAML::Node& node, Calamares::Module& m );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // CALAMARESMODULE_H
 | 
			
		||||
@ -0,0 +1,103 @@
 | 
			
		||||
/* === 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 "ModuleLoader.h"
 | 
			
		||||
 | 
			
		||||
#include "utils/Logger.h"
 | 
			
		||||
 | 
			
		||||
#include <yaml-cpp/yaml.h>
 | 
			
		||||
 | 
			
		||||
#include <QDir>
 | 
			
		||||
 | 
			
		||||
#define MODULE_CONFIG_FILENAME "module.conf"
 | 
			
		||||
 | 
			
		||||
namespace Calamares
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
ModuleLoader::ModuleLoader( const QStringList& paths, QObject* parent )
 | 
			
		||||
    : QObject( parent )
 | 
			
		||||
    , m_paths( paths )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ModuleLoader::~ModuleLoader()
 | 
			
		||||
{
 | 
			
		||||
    foreach ( Module* m, m_availableModules )
 | 
			
		||||
    {
 | 
			
		||||
        delete m;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
ModuleLoader::exec()
 | 
			
		||||
{
 | 
			
		||||
    // We start from a list of paths in m_paths. Each of those is a directory that
 | 
			
		||||
    // might (should) contain Calamares modules of any type/interface.
 | 
			
		||||
    // For each modules search path (directory), it is expected that each module
 | 
			
		||||
    // lives in its own subdirectory. This subdirectory must have the same name as
 | 
			
		||||
    // the module name, and must contain a settings file named module.conf.
 | 
			
		||||
    // If at any time the module loading procedure finds something unexpected, it
 | 
			
		||||
    // silently skips to the next module or search path. --Teo 6/2014
 | 
			
		||||
    foreach ( const QString& path, m_paths )
 | 
			
		||||
    {
 | 
			
		||||
        QDir currentDir( path );
 | 
			
		||||
        if ( currentDir.exists() && currentDir.isReadable() )
 | 
			
		||||
        {
 | 
			
		||||
            QStringList subdirs = currentDir.entryList( QDir::AllDirs | QDir::NoDotAndDotDot );
 | 
			
		||||
            foreach ( const QString& subdir, subdirs )
 | 
			
		||||
            {
 | 
			
		||||
                bool success = currentDir.cd( subdir );
 | 
			
		||||
                if ( success && currentDir.isReadable() )
 | 
			
		||||
                {
 | 
			
		||||
                    QFileInfo metadataFileInfo( currentDir.absoluteFilePath( MODULE_CONFIG_FILENAME ) );
 | 
			
		||||
                    if ( ! ( metadataFileInfo.exists() && metadataFileInfo.isReadable() ) )
 | 
			
		||||
                    {
 | 
			
		||||
                        cDebug() << Q_FUNC_INFO << "unreadable file: "
 | 
			
		||||
                                 << metadataFileInfo.absoluteFilePath();
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    Module* moduleInfo = Module::loadFromFile( metadataFileInfo.absoluteFilePath() );
 | 
			
		||||
 | 
			
		||||
                    if ( moduleInfo &&
 | 
			
		||||
                         ( moduleInfo->name() == currentDir.dirName() ) &&
 | 
			
		||||
                         ( !m_availableModules.contains( moduleInfo->name() ) ) )
 | 
			
		||||
                    {
 | 
			
		||||
                        m_availableModules.insert( moduleInfo->name(), moduleInfo );
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    currentDir.cdUp();
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    cDebug() << Q_FUNC_INFO << "cannot cd into module directory "
 | 
			
		||||
                             << path << "/" << subdir;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            cDebug() << Q_FUNC_INFO << "bad search path " << path;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // At this point m_availableModules is filled with whatever was found in the
 | 
			
		||||
    // search paths.
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,54 @@
 | 
			
		||||
/* === 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 MODULELOADER_H
 | 
			
		||||
#define MODULELOADER_H
 | 
			
		||||
 | 
			
		||||
#include "Module.h"
 | 
			
		||||
 | 
			
		||||
#include <QMap>
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#include <QStringList>
 | 
			
		||||
 | 
			
		||||
namespace Calamares
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
class Module;
 | 
			
		||||
 | 
			
		||||
class ModuleLoader : public QObject
 | 
			
		||||
{
 | 
			
		||||
    Q_OBJECT
 | 
			
		||||
public:
 | 
			
		||||
    explicit ModuleLoader( const QStringList& paths, QObject* parent = 0 );
 | 
			
		||||
    virtual ~ModuleLoader();
 | 
			
		||||
 | 
			
		||||
    void exec();
 | 
			
		||||
 | 
			
		||||
signals:
 | 
			
		||||
    void done();
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    QMap< QString, Module* > m_availableModules;
 | 
			
		||||
 | 
			
		||||
    QStringList m_paths;
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // MODULELOADER_H
 | 
			
		||||
@ -0,0 +1,30 @@
 | 
			
		||||
/* === 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 "YamlUtils.h"
 | 
			
		||||
 | 
			
		||||
#include <yaml-cpp/yaml.h>
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
operator>>( const YAML::Node& node, QStringList& v )
 | 
			
		||||
{
 | 
			
		||||
    for ( int i = 0; i < node.size(); ++i )
 | 
			
		||||
    {
 | 
			
		||||
        v.append( QString::fromStdString( node[ i ].as< std::string >() ) );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,31 @@
 | 
			
		||||
/* === 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 YAMLUTILS_H
 | 
			
		||||
#define YAMLUTILS_H
 | 
			
		||||
 | 
			
		||||
#include <QStringList>
 | 
			
		||||
 | 
			
		||||
namespace YAML
 | 
			
		||||
{
 | 
			
		||||
class Node;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void operator>>( const YAML::Node& node, QStringList& v );
 | 
			
		||||
 | 
			
		||||
#endif // YAMLUTILS_H
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue