Debian Codegraph @

avogadro/qtplugins/configurepython/configurepythondialog.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "configurepythondialog.h"
7 #include "condadialog.h"
8
9 #include "ui_configurepythondialog.h"
10
11 #include <avogadro/qtgui/utilities.h>
12
13 #include <QDebug>
14 #include <QFileInfo>
15 #include <QtCore/QProcess>
16 #include <QtCore/QSettings>
17
18 using Avogadro::QtGui::Utilities::findExecutablePaths;
19
20 namespace Avogadro::QtPlugins {
21
22 ConfigurePythonDialog::ConfigurePythonDialog(QWidget* aParent)
23 : QDialog(aParent), m_ui(new Ui::ConfigurePythonDialog), m_condaUi(nullptr)
24 {
25 m_ui->setupUi(this);
26 m_ui->browseWidget->hide();
27
28 connect(m_ui->environmentCombo, SIGNAL(currentIndexChanged(int)),
29 SLOT(optionChanged(int)));
30
31 connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(accept()));
32 connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(reject()));
33
34 // look for conda environments
35 QProcess condaProcess;
36 QSettings settings;
37 QString condaPath =
38 settings.value("interpreters/condaPath", "conda").toString();
39 // check if conda is executable
40 if (!QFileInfo(condaPath).isExecutable()) {
41 // see if we can find any related executables in the path
42 QStringList names;
43 names << "micromamba"
44 << "mamba"
45 << "conda";
46 #ifdef Q_OS_WIN
47 names << "micromamba.exe"
48 << "mamba.exe"
49 << "conda.exe";
50 #endif
51 QStringList paths = findExecutablePaths(names);
52 if (!paths.isEmpty()) {
53 condaPath = paths.first();
54 } else
55 return; // nothing more to do
56 }
57
58 // set the path to conda
59 settings.setValue("interpreters/condaPath", condaPath);
60
61 // get the list of environments
62 condaProcess.start(condaPath, QStringList() << "env"
63 << "list");
64 if (condaProcess.waitForFinished()) {
65 QString output = condaProcess.readAllStandardOutput();
66 QStringList lines = output.split("\n");
67 foreach (const QString& line, lines) {
68 if (line.startsWith("#"))
69 continue;
70
71 QStringList parts = line.split(" ");
72 if (parts.size() > 1)
73 m_condaEnvironments << parts.at(0);
74 }
75 }
76 if (m_condaEnvironments.size() < 2) {
77 // no environment or only the base found
78 setupCondaEnvironment();
79 }
80 }
81
82 ConfigurePythonDialog::~ConfigurePythonDialog()
83 {
84 delete m_ui;
85 }
86
87 void ConfigurePythonDialog::setupCondaEnvironment()
88 {
89 // suggest the user create a new environment through a dialog
90 if (m_condaUi == nullptr) {
91 m_condaUi = new CondaDialog(qobject_cast<QWidget*>(parent()));
92 }
93 int choice = m_condaUi->exec();
94 if (choice == QDialog::Rejected)
95 return;
96
97 QString newEnvironment = m_condaUi->environmentName();
98 if (newEnvironment.isEmpty())
99 return;
100
101 // create the environment
102 QProcess condaProcess;
103 QSettings settings;
104 QString condaPath =
105 settings.value("interpreters/condaPath", "conda").toString();
106 // check if conda is executable
107 if (!QFileInfo(condaPath).isExecutable()) {
108 // see if we can find any related executables in the path
109 QStringList names;
110 names << "micromamba"
111 << "mamba"
112 << "conda";
113 #ifdef Q_OS_WIN
114 names << "micromamba.exe"
115 << "mamba.exe"
116 << "conda.exe";
117 #endif
118 QStringList paths = findExecutablePaths(names);
119 if (!paths.isEmpty()) {
120 condaPath = paths.first();
121 } else
122 return; // nothing more to do
123 }
124
125 QStringList arguments;
126 arguments << "create"
127 << "-n" << newEnvironment << "--clone"
128 << "base";
129 condaProcess.start(condaPath, arguments);
130 if (condaProcess.waitForFinished()) {
131 QString output = condaProcess.readAllStandardOutput();
132 if (output.contains("done")) {
133 // environment created
134 m_condaEnvironments << newEnvironment;
135 settings.setValue("interpreters/condaEnvironment", newEnvironment);
136 }
137 }
138 }
139
140 QString ConfigurePythonDialog::condaPath() const
141 {
142 QSettings settings;
143 QString path = settings.value("interpreters/condaPath").toString();
144 return path;
145 }
146
147 QString ConfigurePythonDialog::condaEnvironment() const
148 {
149 QSettings settings;
150 QString environment =
151 settings.value("interpreters/condaEnvironment").toString();
152 return environment;
153 }
154
155 void ConfigurePythonDialog::setOptions(const QStringList& options)
156 {
157 m_ui->environmentCombo->clear();
158
159 // check the current choice from QSettings
160 QSettings settings;
161 QString currentInterpreter =
162 settings.value("interpreters/python", QString()).toString();
163 QString currentConda =
164 settings.value("interpreters/condaEnvironment", QString()).toString();
165 int index = -1;
166
167 // add all conda environments
168 foreach (const QString& environment, m_condaEnvironments) {
169 if (environment.isEmpty())
170 continue; // shouldn't happen, but just in case
171
172 m_ui->environmentCombo->addItem(QString("%1 (conda)").arg(environment));
173 if (environment == currentConda)
174 index = m_ui->environmentCombo->count() - 1;
175 }
176
177 // get the Python version from each interpreter
178 QStringList versions, arguments;
179 QProcess process;
180 arguments << "-V";
181 foreach (const QString& option, options) {
182 process.start(option, arguments);
183 if (process.waitForFinished()) {
184 QString output = process.readAllStandardOutput();
185 if (output.startsWith("Python")) {
186 versions << output.split(" ").at(1).simplified();
187 } else {
188 versions << tr("Unknown");
189 }
190 } else {
191 versions << tr("Unknown");
192 }
193 }
194
195 for (int i = 0; i < options.size(); ++i) {
196 m_ui->environmentCombo->addItem(
197 QString("%1 (%2)").arg(options.at(i)).arg(versions.at(i)));
198 // if the conda environment isn't the current, check the python interpreter
199 if (options.at(i) == currentInterpreter && index == -1)
200 index = m_ui->environmentCombo->count() - 1;
201 }
202
203 m_ui->environmentCombo->addItem(tr("Other…"));
204 // set the current choice
205 if (index >= 0)
206 m_ui->environmentCombo->setCurrentIndex(index);
207
208 m_ui->browseWidget->hide();
209 }
210
211 void ConfigurePythonDialog::optionChanged(int index)
212 {
213 // if there's only one choice, check if it's "Other…"
214 if ((index == m_ui->environmentCombo->count() - 1) ||
215 m_ui->environmentCombo->currentText() == tr("Other…")) {
216 // "Other…" selected
217 m_ui->browseWidget->setEnabled(true);
218 m_ui->browseWidget->show();
219 } else {
220 m_ui->browseWidget->setEnabled(false);
221 m_ui->browseWidget->hide();
222 }
223 }
224
225 QString ConfigurePythonDialog::currentOption() const
226 {
227 if (m_ui->environmentCombo->currentIndex() ==
228 m_ui->environmentCombo->count() - 1)
229 return m_ui->browseWidget->fileName();
230
231 QString path = m_ui->environmentCombo->currentText();
232 // check if this is a conda choice
233 int index = path.indexOf(" (conda)");
234 if (index >= 0) {
235 // get the environment name
236 QString environment = path.left(index);
237 QSettings settings;
238 settings.setValue("interpreters/condaEnvironment", environment);
239
240 // activate the environment and get the path to the python interpreter
241 QProcess condaProcess;
242 QString condaPath =
243 settings.value("interpreters/condaPath", "conda").toString();
244 // check if conda is executable
245 if (!QFileInfo(condaPath).isExecutable())
246 return QString();
247 condaProcess.start(condaPath, QStringList()
248 << "run"
249 << "-n" << environment << "which"
250 << "python");
251 if (condaProcess.waitForFinished()) {
252 QString output = condaProcess.readAllStandardOutput();
253 qDebug() << " output: " << output << "\n";
254 if (output.contains("python")) {
255 // remove the newline
256 output.remove("\n");
257 return output;
258 }
259 }
260 }
261
262 // remove the Python version to get the path
263 index = path.indexOf(" (");
264 if (index >= 0)
265 return path.left(index);
266
267 return path;
268 }
269
270 void ConfigurePythonDialog::reject()
271 {
272 QDialog::reject();
273
274 emit rejected();
275 }
276
277 void ConfigurePythonDialog::accept()
278 {
279 QDialog::accept();
280
281 emit accepted();
282 }
283
284 void ConfigurePythonDialog::setCurrentOption(const QString& option)
285 {
286 int index = m_ui->environmentCombo->findText(option);
287 if (index >= 0)
288 m_ui->environmentCombo->setCurrentIndex(index);
289 }
290
291 } // namespace Avogadro::QtPlugins