itom  4.1.0
pipManager.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 *********************************************************************** */
22 
23 #ifndef PIPMANAGER_H
24 #define PIPMANAGER_H
25 
26 #include <qabstractitemmodel.h>
27 #include <qprocess.h>
28 
29 #include "../common/sharedStructures.h"
30 
31 namespace ito
32 {
33 
35 {
36  enum Status {Unknown, Uptodate, Outdated};
37  PythonPackage() : m_status(Unknown) {};
38  PythonPackage(const QString &name, const QString &version, const QString &location, const QString &requires) :
39  m_name(name), m_version(version), m_location(location), m_requires(requires), m_status(Unknown), m_newVersion("")
40  {}
41  QString m_name;
42  QString m_version;
43  QString m_location;
44  QString m_requires;
45  Status m_status;
46  QString m_newVersion;
47  QString m_summary;
48  QString m_homepage;
49  QString m_license;
50 };
51 
53 {
54  PipGeneralOptions() : isolated(false), logPath(""), proxy(""), timeout(15), retries(5), useTrustedHosts(false) {}
55  bool isolated; //if true, --isolated is added to pip calls
56  QString logPath; //if != "" --log <logPath> is added to pip calls
57  QString proxy; //if != "" --proxy <proxy> is added to pip calls
58  int timeout; //if >= 0 --timeout <sec> is added to pip calls where timeout denotes the number of seconds
59  int retries; //if > 0 --retries <retries> is added to pip calls where retries denotes the number of tries if one command failed.
60  bool useTrustedHosts; //if true, each host in the corresponding text field (semicolon separated list) will be appended via --trusted-host to any pip command
61  QStringList trustedHosts; //trusted hosts, only considered if useTrustedHosts is true
62 };
63 
64 struct PipInstall
65 {
66  enum Type
67  {
68  typeWhl = 0,
69  typeTarGz = 1,
70  typeSearchIndex = 2,
71  typeRequirements = 3
72  }; //these are the same types than in DialogPipManager
73 
74  Type type;
75  QString packageName;
76  bool upgrade;
77  bool installDeps;
78  QString findLinks;
79  bool ignoreIndex;
80  bool runAsSudo;
81 };
82 
83 class PipManager : public QAbstractItemModel
84 {
85  Q_OBJECT
86 
87  public:
88  PipManager(ito::RetVal &retval, QObject *parent = 0);
89  ~PipManager();
90 
91  enum Task {
92  taskNo,
93  taskCheckAvailable,
94  taskListPackages1,
95  taskListPackages2,
96  taskCheckUpdates,
97  taskInstall,
98  taskUninstall,
99  taskVerifyInstalledPackages
100  };
101 
102  enum PipMode {
105 
108  pipModeRunPipUtf8
109  };
110 
111  QVariant data(const QModelIndex &index, int role) const;
112  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
113  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
114  QModelIndex parent(const QModelIndex &index) const;
115  int rowCount(const QModelIndex &parent = QModelIndex()) const;
116  int columnCount(const QModelIndex &parent = QModelIndex()) const;
117 
118  void startPipProcess();
119  bool isPipStarted() const;
120  inline int getPipVersion() const { return m_pipVersion; }
121 
122  void checkPipAvailable(const PipGeneralOptions &options = PipGeneralOptions());
123  void listAvailablePackages(const PipGeneralOptions &options = PipGeneralOptions());
124  void listAvailablePackages2(const QStringList &names);
125  void checkPackageUpdates(const PipGeneralOptions &options = PipGeneralOptions());
126  void checkVerifyInstalledPackages(const PipGeneralOptions &options = PipGeneralOptions());
127  void installPackage(const PipInstall &installSettings, const PipGeneralOptions &options = PipGeneralOptions());
128  void uninstallPackage(const QString &packageName, bool runAsSudo, const PipGeneralOptions &options = PipGeneralOptions());
129  void finalizeTask(int exitCode = 0);
130 
131  void interruptPipProcess();
132 
133  bool isPackageInUseByOther(const QModelIndex &index);
134 
135 
136  private:
137  QStringList parseGeneralOptions(const PipGeneralOptions &options, bool ignoreRetries = false, bool ignoreVersionCheck = true) const;
138  void clearBuffers();
139  ito::RetVal initPythonIfStandalone();
141  void startProcess(const QStringList &arguments);
142 
143  QList<QString> m_headers;
144  QList<QVariant> m_alignment;
145  QList<PythonPackage> m_pythonPackages;
146  QProcess m_pipProcess;
147  bool m_pipAvailable;
148  QByteArray m_standardOutputBuffer;
149  QByteArray m_standardErrorBuffer;
150  Task m_currentTask;
151  PipGeneralOptions m_generalOptionsCache;
152  QString m_pythonPath;
153  int m_pipVersion; //form 0x060100 for 6.1.0
154  wchar_t *m_pUserDefinedPythonHome; //only used for standalone usage
155  PipMode m_pipCallMode;
157 
158  private slots:
159  void processError(QProcess::ProcessError error);
160  void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
161  void processReadyReadStandardError();
162  void processReadyReadStandardOutput();
163 
164  signals:
165  void pipManagerBusy();
166  void outputAvailable(const QString &text, bool success);
167  void pipVersion(const QString &version);
168  void pipRequestStarted(const PipManager::Task &task, const QString &text, bool outputSilent = false);
169  void pipRequestFinished(const PipManager::Task &task, const QString &text, bool success);
170 };
171 
172 }
173 
174 #endif //PIPMANAGER_H
ito::RetVal checkCallMode()
check if pip can be called via the itom-packages/pipProcess/runPipUtf8.py script or directly ...
Definition: pipManager.cpp:299
Class for managing status values (like errors or warning)
Definition: retVal.h:54
PipMode
Definition: pipManager.h:102
Definition: pipManager.h:64
Definition: apiFunctionsGraph.cpp:39
QVariant data(const QModelIndex &index, int role) const
Definition: pipManager.cpp:375
Definition: pipManager.h:52
QList< PythonPackage > m_pythonPackages
list with installed python packages
Definition: pipManager.h:145
void startProcess(const QStringList &arguments)
the arguments string list must not contain -m as first entry.
Definition: pipManager.cpp:465
PipManager(ito::RetVal &retval, QObject *parent=0)
Definition: pipManager.cpp:43
Definition: pipManager.h:34
QList< QString > m_headers
string list of names of column headers
Definition: pipManager.h:143
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: pipManager.cpp:434
QString m_runPipUtf8Path
only valid if m_pipCallMode == pipModeRunPipUtf8
Definition: pipManager.h:156
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: pipManager.cpp:355
~PipManager()
Definition: pipManager.cpp:150
Definition: pipManager.h:83
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: pipManager.cpp:448
directly call pip as process (might cause encoding errors under windows)
Definition: pipManager.h:104
QList< QVariant > m_alignment
list of alignments for the corresponding headers
Definition: pipManager.h:144
QModelIndex parent(const QModelIndex &index) const
Definition: pipManager.cpp:336
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: pipManager.cpp:346