itom 2.0.0
D:/git-itom/sources/itom/Qitom/models/pipManager.h
00001 /* ********************************************************************
00002     itom software
00003     URL: http://www.uni-stuttgart.de/ito
00004     Copyright (C) 2015, Institut für Technische Optik (ITO),
00005     Universität Stuttgart, Germany
00006 
00007     This file is part of itom.
00008   
00009     itom is free software; you can redistribute it and/or modify it
00010     under the terms of the GNU Library General Public Licence as published by
00011     the Free Software Foundation; either version 2 of the Licence, or (at
00012     your option) any later version.
00013 
00014     itom is distributed in the hope that it will be useful, but
00015     WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
00017     General Public Licence for more details.
00018 
00019     You should have received a copy of the GNU Library General Public License
00020     along with itom. If not, see <http://www.gnu.org/licenses/>.
00021 *********************************************************************** */
00022 
00023 #ifndef PIPMANAGER_H
00024 #define PIPMANAGER_H
00025 
00026 #include <qabstractitemmodel.h>
00027 #include <qprocess.h>
00028 
00029 namespace ito 
00030 {
00031 
00032 struct PythonPackage
00033 {
00034     enum Status {Unknown, Uptodate, Outdated};
00035     PythonPackage() : m_status(Unknown) {};
00036     PythonPackage(const QString &name, const QString &version, const QString &location, const QString &requires) : 
00037         m_name(name), m_version(version), m_location(location), m_requires(requires), m_status(Unknown), m_newVersion("") 
00038     {}
00039     QString m_name;
00040     QString m_version;
00041     QString m_location;
00042     QString m_requires;
00043     Status  m_status;
00044     QString m_newVersion;
00045     QString m_summary;
00046     QString m_homepage;
00047     QString m_license;
00048 };
00049 
00050 struct PipGeneralOptions
00051 {
00052     PipGeneralOptions() : isolated(false), logPath(""), proxy(""), timeout(15), retries(5) {}
00053     bool isolated;          //if true, --isolated is added to pip calls
00054     QString logPath;        //if != "" --log <logPath> is added to pip calls
00055     QString proxy;          //if != "" --proxy <proxy> is added to pip calls
00056     int timeout;            //if >= 0 --timeout <sec> is added to pip calls where timeout denotes the number of seconds
00057     int retries;            //if > 0 --retries <retries> is added to pip calls where retries denotes the number of tries if one command failed.
00058 };
00059 
00060 struct PipInstall
00061 {
00062     enum Type { typeWhl = 0, typeTarGz = 1, typeSearchIndex = 2}; //these are the same types than in DialogPipManager
00063     Type type;
00064     QString packageName;
00065     bool upgrade;
00066     bool installDeps;
00067     QString findLinks;
00068     bool ignoreIndex;
00069     bool runAsSudo;
00070 };
00071 
00072 class PipManager : public QAbstractItemModel
00073 {
00074     Q_OBJECT
00075 
00076     public:
00077         PipManager(QObject *parent = 0);
00078         ~PipManager();
00079 
00080         enum Task {taskNo, taskCheckAvailable, taskListPackages1, taskListPackages2, taskCheckUpdates, taskInstall, taskUninstall};
00081 
00082         QVariant data(const QModelIndex &index, int role) const;
00083         QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
00084         QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
00085         QModelIndex parent(const QModelIndex &index) const;
00086         int rowCount(const QModelIndex &parent = QModelIndex()) const;
00087         int columnCount(const QModelIndex &parent = QModelIndex()) const;
00088 
00089         void startPipProcess();
00090         bool isPipStarted() const;
00091         inline int getPipVersion() const { return m_pipVersion; }
00092 
00093         void checkPipAvailable(const PipGeneralOptions &options = PipGeneralOptions());
00094         void listAvailablePackages(const PipGeneralOptions &options = PipGeneralOptions());
00095         void listAvailablePackages2(const QStringList &names);
00096         void checkPackageUpdates(const PipGeneralOptions &options = PipGeneralOptions());
00097         void installPackage(const PipInstall &installSettings, const PipGeneralOptions &options = PipGeneralOptions());
00098         void uninstallPackage(const QString &packageName, bool runAsSudo, const PipGeneralOptions &options = PipGeneralOptions());
00099         void finalizeTask();
00100 
00101         void interruptPipProcess();
00102 
00103         bool isPackageInUseByOther(const QModelIndex &index);
00104 
00105 
00106     private:
00107         QStringList parseGeneralOptions(const PipGeneralOptions &options, bool ignoreRetries = false, bool ignoreVersionCheck = true) const;
00108         void clearBuffers();
00109 
00110         QList<QString> m_headers;               
00111         QList<QVariant> m_alignment;            
00112         QList<PythonPackage> m_pythonPackages;  
00113         QProcess m_pipProcess;
00114         bool m_pipAvailable;
00115         QByteArray m_standardOutputBuffer;
00116         QByteArray m_standardErrorBuffer;
00117         Task m_currentTask;
00118         PipGeneralOptions m_generalOptionsCache;
00119         QString m_pythonPath;
00120         int m_pipVersion; //form 0x060100 for 6.1.0
00121     
00122     private slots:
00123         void processError(QProcess::ProcessError error);
00124         void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
00125         void processReadyReadStandardError();
00126         void processReadyReadStandardOutput();
00127 
00128     signals:
00129         void pipManagerBusy();
00130         void outputAvailable(const QString &text, bool success);
00131         void pipVersion(const QString &version);
00132         void pipRequestStarted(const PipManager::Task &task, const QString &text, bool outputSilent = false);
00133         void pipRequestFinished(const PipManager::Task &task, const QString &text, bool success);
00134 };
00135 
00136 }
00137 
00138 #endif //PIPMANAGER_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends