Debian Codegraph @

avogadro/qtplugins/configurepython/configurepython.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 "configurepython.h"
7
8 #include "configurepythondialog.h"
9
10 #include <avogadro/qtgui/avogadropython.h>
11 #include <avogadro/qtgui/filebrowsewidget.h>
12 #include <avogadro/qtgui/utilities.h>
13
14 #include <QAction>
15 #include <QtCore/QDir>
16 #include <QtCore/QFileInfo>
17 #include <QtCore/QProcess>
18 #include <QtCore/QSettings>
19 #include <QtCore/QSysInfo>
20 #include <QtCore/QUrl>
21 #include <QtGui/QDesktopServices>
22 #include <QtWidgets/QMessageBox>
23
24 namespace Avogadro::QtPlugins {
25
26 using QtGui::FileBrowseWidget;
27 using QtGui::Utilities::findExecutablePaths;
28
29 ConfigurePython::ConfigurePython(QObject* parent_)
30 : Avogadro::QtGui::ExtensionPlugin(parent_), m_action(new QAction(this)),
31 m_dialog(nullptr)
32 {
33 m_action->setEnabled(true);
34 m_action->setText(tr("Python Settings…"));
35 m_action->setProperty("menu priority", 510);
36 connect(m_action, SIGNAL(triggered()), SLOT(showDialog()));
37
38 // check for Python on first launch
39 QSettings settings;
40 if (!settings.value("interpreters/firstlaunch", false).toBool()) {
41 // First, check if pixi is available (plugins will use it for installs)
42 #ifdef Q_OS_WIN
43 QString pixiName = QStringLiteral("pixi.exe");
44 #else
45 QString pixiName = QStringLiteral("pixi");
46 #endif
47 QString pixiPath = QtGui::Utilities::findExecutablePath(pixiName);
48
49 if (pixiPath.isEmpty()) {
50 // No pixi, check for python/python3 (plugins will use pip)
51 // pythonPaths() checks PATH, Windows common install locations,
52 // environment variables, and validates each with a version check
53 QStringList paths = pythonPaths();
54
55 if (paths.isEmpty()) {
56 // No pixi or python found — suggest installing miniforge
57 auto option = QMessageBox::information(
58 qobject_cast<QWidget*>(parent()), tr("Install Python"),
59 tr("Python is used for many Avogadro "
60 "features. Do you want to download Python?"),
61 QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
62 if (option == QMessageBox::Yes) {
63 QUrl miniforge;
64 #ifdef Q_OS_WIN
65 // TODO: ARM or Intel? .. conda-forge doesn't have ARM builds yet
66 miniforge = QUrl("https://github.com/conda-forge/miniforge/releases/"
67 "latest/download/Miniforge3-Windows-x86_64.exe");
68 #elif defined(Q_OS_MACOS)
69 if (QSysInfo::currentCpuArchitecture().contains("arm"))
70 miniforge =
71 QUrl("https://github.com/conda-forge/miniforge/releases/"
72 "latest/download/Miniforge3-MacOSX-arm64.sh");
73 else
74 miniforge =
75 QUrl("https://github.com/conda-forge/miniforge/releases/"
76 "latest/download/Miniforge3-MacOSX-x86_64.sh");
77 #else
78 QString arch = QSysInfo::currentCpuArchitecture();
79 if (arch == QLatin1String("arm64"))
80 miniforge =
81 QUrl("https://github.com/conda-forge/miniforge/releases/"
82 "latest/download/Miniforge3-Linux-aarch64.sh");
83 else if (arch == QLatin1String("power64"))
84 miniforge =
85 QUrl("https://github.com/conda-forge/miniforge/releases/"
86 "latest/download/Miniforge3-Linux-ppc64le.sh");
87 else if (arch == QLatin1String("x86_64"))
88 miniforge =
89 QUrl("https://github.com/conda-forge/miniforge/releases/"
90 "latest/download/Miniforge3-Linux-x86_64.sh");
91 #endif
92 if (miniforge.isValid()) {
93 QDesktopServices::openUrl(miniforge);
94 QDesktopServices::openUrl(
95 QUrl("https://github.com/conda-forge/"
96 "miniforge?tab=readme-ov-file#install"));
97 }
98 }
99 }
100 }
101 settings.setValue("interpreters/firstlaunch", true);
102 }
103 }
104
105 ConfigurePython::~ConfigurePython()
106 {
107 delete m_action;
108 }
109
110 QList<QAction*> ConfigurePython::actions() const
111 {
112 return QList<QAction*>() << m_action;
113 }
114
115 QStringList ConfigurePython::menuPath(QAction*) const
116 {
117 return QStringList() << tr("&Extensions");
118 }
119
120 void ConfigurePython::accept()
121 {
122 if (m_dialog == nullptr)
123 return;
124
125 // Save the settings
126 QSettings settings;
127 settings.setValue("interpreters/python", m_dialog->currentOption());
128
129 // check if the dialog has a conda environment selected
130 if (!m_dialog->condaEnvironment().isEmpty()) {
131 settings.setValue("interpreters/condaEnvironment",
132 m_dialog->condaEnvironment());
133 // get the path to conda
134 QString condaPath = m_dialog->condaPath();
135 if (!condaPath.isEmpty()) {
136 settings.setValue("interpreters/condaPath", condaPath);
137 }
138 }
139
140 // TODO: reload the python interpreters
141 }
142
143 QStringList ConfigurePython::pythonPaths() const
144 {
145 // Check for python interpreter in env var
146 QString pythonInterp =
147 QString::fromLocal8Bit(qgetenv("AVO_PYTHON_INTERPRETER"));
148 if (pythonInterp.isEmpty()) {
149 // Check settings
150 QSettings settings;
151 pythonInterp = settings.value("interpreters/python", QString()).toString();
152 }
153 // Use compile-time default if still not found.
154 if (pythonInterp.isEmpty())
155 pythonInterp = QString(pythonInterpreterPath);
156
157 // get the list from the system path
158 QStringList names;
159 #ifdef Q_OS_WIN
160 names << "python3.exe"
161 << "python.exe";
162 #else
163 names << "python3"
164 << "python";
165 #endif
166
167 QStringList paths = findExecutablePaths(names);
168
169 // Add the current interpreter to the list
170 // it may be filtered out by the loop below
171 if (!paths.contains(pythonInterp)) {
172 paths.prepend(pythonInterp);
173 }
174
175 #ifdef Q_OS_WIN
176 // on Windows, check for a few possible locations
177 // if they exist, add them to the list
178 // e.g. C:\Program Files\Python*
179 // C:\Program Files (x86)\Python*
180 QStringList programDirs;
181 programDirs << "C:/Program Files"
182 << "C:/Program Files (x86)";
183 // might also be in the APPDATA folder
184 QString homePath = QDir::homePath();
185 if (!homePath.isEmpty()) {
186 programDirs << homePath + "/AppData/Local/Programs";
187 programDirs << homePath + "/AppData/Local/Programs/Python";
188 }
189
190 foreach (const QString& dir, programDirs) {
191 QDir programFiles(dir);
192 QStringList pythonDirs = programFiles.entryList(
193 QStringList() << "Python*", QDir::Dirs | QDir::NoDotAndDotDot);
194 // check if there's a python3.exe or python.exe
195 foreach (const QString& pythonDir, pythonDirs) {
196 QDir pythonDirInfo(dir + "/" + pythonDir);
197 if (pythonDirInfo.exists("python3.exe"))
198 paths << pythonDirInfo.absolutePath() + "/python3.exe";
199 else if (pythonDirInfo.exists("python.exe"))
200 paths << pythonDirInfo.absolutePath() + "/python.exe";
201 }
202 }
203 #endif
204
205 // check to make sure each of the items are valid or remove them
206 // (i.e., the python should return a version flag)
207 QStringList validPaths;
208 QStringList arguments;
209 arguments << "-V";
210 foreach (const QString& path, paths) {
211 QFileInfo info(path);
212 if (info.exists() && info.isExecutable()) {
213 // try to run it to get the version
214 QProcess process;
215 process.start(path, arguments);
216 if (process.waitForFinished()) {
217 QString output = process.readAllStandardOutput();
218 // should be like Python 3.10.14
219 if (output.startsWith("Python")) {
220 QString version = output.split(" ").at(1).simplified();
221 // make sure it's at least Python 3
222 // in the future, we can ensure particular releases
223 if (version.startsWith("3"))
224 validPaths << path;
225 }
226 }
227 // if we didn't get results, it's not valid
228 }
229 }
230
231 return validPaths;
232 }
233
234 void ConfigurePython::showDialog()
235 {
236 if (m_dialog == nullptr) {
237 m_dialog = new ConfigurePythonDialog(qobject_cast<QWidget*>(parent()));
238 connect(m_dialog, SIGNAL(accepted()), SLOT(accept()));
239 }
240
241 // Populate the dialog with the current settings
242 QStringList pythonInterps = pythonPaths();
243
244 m_dialog->setOptions(pythonInterps);
245 m_dialog->show();
246 m_dialog->raise();
247 m_dialog->activateWindow();
248 }
249
250 } // namespace Avogadro::QtPlugins