1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "pythonbuildconfiguration.h"
#include "pipsupport.h"
#include "pyside.h"
#include "pysideuicextracompiler.h"
#include "pythonconstants.h"
#include "pythoneditor.h"
#include "pythonkitaspect.h"
#include "pythonlanguageclient.h"
#include "pythonproject.h"
#include "pythonsettings.h"
#include "pythontr.h"
#include "pythonutils.h"
#include <coreplugin/editormanager/documentmodel.h>
#include <languageclient/languageclientmanager.h>
#include <projectexplorer/buildinfo.h>
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/buildsystem.h>
#include <projectexplorer/environmentaspect.h>
#include <projectexplorer/processparameters.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/target.h>
#include <utils/algorithm.h>
#include <utils/commandline.h>
#include <utils/detailswidget.h>
#include <utils/fileutils.h>
#include <utils/futuresynchronizer.h>
#include <utils/layoutbuilder.h>
#include <utils/mimeconstants.h>
#include <utils/qtcprocess.h>
using namespace ProjectExplorer;
using namespace Utils;
namespace Python::Internal {
PySideBuildStep::PySideBuildStep(BuildStepList *bsl, Id id)
: AbstractProcessStep(bsl, id)
{
m_pysideProject.setSettingsKey("Python.PySideProjectTool");
m_pysideProject.setLabelText(Tr::tr("PySide project tool:"));
m_pysideProject.setToolTip(Tr::tr("Enter location of PySide project tool."));
m_pysideProject.setExpectedKind(PathChooser::Command);
m_pysideProject.setHistoryCompleter("Python.PySideProjectTool.History");
m_pysideProject.setReadOnly(true);
m_pysideUic.setSettingsKey("Python.PySideUic");
m_pysideUic.setLabelText(Tr::tr("PySide uic tool:"));
m_pysideUic.setToolTip(Tr::tr("Enter location of PySide uic tool."));
m_pysideUic.setExpectedKind(PathChooser::Command);
m_pysideUic.setHistoryCompleter("Python.PySideUic.History");
m_pysideUic.setReadOnly(true);
setCommandLineProvider([this] { return CommandLine(m_pysideProject(), {"build"}); });
setWorkingDirectoryProvider([this] {
return m_pysideProject().withNewMappedPath(project()->projectDirectory()); // FIXME: new path needed?
});
setEnvironmentModifier([this](Environment &env) {
env.prependOrSetPath(m_pysideProject().parentDir());
});
connect(buildSystem(), &BuildSystem::updated, this, &PySideBuildStep::updateExtraCompilers);
connect(&m_pysideUic, &BaseAspect::changed, this, &PySideBuildStep::updateExtraCompilers);
}
PySideBuildStep::~PySideBuildStep()
{
qDeleteAll(m_extraCompilers);
}
void PySideBuildStep::checkForPySide(const FilePath &python)
{
PySideTools tools;
if (python.isEmpty() || !python.isExecutableFile()) {
m_pysideProject.setValue(FilePath());
m_pysideUic.setValue(FilePath());
return;
}
const FilePath dir = python.parentDir();
tools.pySideProjectPath = dir.pathAppended("pyside6-project").withExecutableSuffix();
tools.pySideUicPath = dir.pathAppended("pyside6-uic").withExecutableSuffix();
if (tools.pySideProjectPath.isExecutableFile() && tools.pySideUicPath.isExecutableFile()) {
m_pysideProject.setValue(tools.pySideProjectPath.toUserOutput());
m_pysideUic.setValue(tools.pySideUicPath.toUserOutput());
} else {
checkForPySide(python, "PySide6-Essentials");
}
}
void PySideBuildStep::checkForPySide(const FilePath &python, const QString &pySidePackageName)
{
const PipPackage package(pySidePackageName);
QObject::disconnect(m_watcherConnection);
m_watcher.reset(new QFutureWatcher<PipPackageInfo>());
m_watcherConnection = QObject::connect(m_watcher.get(), &QFutureWatcherBase::finished, this,
[this, python, pySidePackageName] {
handlePySidePackageInfo(m_watcher->result(), python, pySidePackageName);
});
const auto future = Pip::instance(python)->info(package);
m_watcher->setFuture(future);
Utils::futureSynchronizer()->addFuture(future);
}
void PySideBuildStep::handlePySidePackageInfo(const PipPackageInfo &pySideInfo,
const FilePath &python,
const QString &requestedPackageName)
{
const auto findPythonTools = [](const FilePaths &files,
const FilePath &location,
const FilePath &python) -> PySideTools {
PySideTools result;
const QString pySide6ProjectName
= OsSpecificAspects::withExecutableSuffix(python.osType(), "pyside6-project");
const QString pySide6UicName
= OsSpecificAspects::withExecutableSuffix(python.osType(), "pyside6-uic");
for (const FilePath &file : files) {
if (file.fileName() == pySide6ProjectName) {
result.pySideProjectPath = python.withNewMappedPath(location.resolvePath(file));
result.pySideProjectPath = result.pySideProjectPath.cleanPath();
if (!result.pySideUicPath.isEmpty())
return result;
} else if (file.fileName() == pySide6UicName) {
result.pySideUicPath = python.withNewMappedPath(location.resolvePath(file));
result.pySideUicPath = result.pySideUicPath.cleanPath();
if (!result.pySideProjectPath.isEmpty())
return result;
}
}
return {};
};
PySideTools tools = findPythonTools(pySideInfo.files, pySideInfo.location, python);
if (!tools.pySideProjectPath.isExecutableFile() && requestedPackageName != "PySide6") {
checkForPySide(python, "PySide6");
return;
}
m_pysideProject.setValue(tools.pySideProjectPath.toUserOutput());
m_pysideUic.setValue(tools.pySideUicPath.toUserOutput());
}
Tasking::GroupItem PySideBuildStep::runRecipe()
{
using namespace Tasking;
const auto onSetup = [this] {
if (!processParameters()->effectiveCommand().isExecutableFile())
return SetupResult::StopWithSuccess;
return SetupResult::Continue;
};
return Group { onGroupSetup(onSetup), defaultProcessTask() };
}
void PySideBuildStep::updateExtraCompilers()
{
QList<PySideUicExtraCompiler *> oldCompilers = m_extraCompilers;
m_extraCompilers.clear();
if (m_pysideUic().isExecutableFile()) {
auto uiMatcher = [](const Node *node) {
if (const FileNode *fileNode = node->asFileNode())
return fileNode->fileType() == FileType::Form;
return false;
};
const FilePaths uiFiles = project()->files(uiMatcher);
for (const FilePath &uiFile : uiFiles) {
FilePath generated = uiFile.parentDir();
generated = generated.pathAppended("/ui_" + uiFile.baseName() + ".py");
int index = Utils::indexOf(oldCompilers, [&](PySideUicExtraCompiler *oldCompiler) {
return oldCompiler->pySideUicPath() == m_pysideUic()
&& oldCompiler->project() == project() && oldCompiler->source() == uiFile
&& oldCompiler->targets() == FilePaths{generated};
});
if (index < 0) {
m_extraCompilers << new PySideUicExtraCompiler(m_pysideUic(),
project(),
uiFile,
{generated},
this);
} else {
m_extraCompilers << oldCompilers.takeAt(index);
}
}
}
for (LanguageClient::Client *client : LanguageClient::LanguageClientManager::clients()) {
if (auto pylsClient = qobject_cast<PyLSClient *>(client))
pylsClient->updateExtraCompilers(m_extraCompilers);
}
qDeleteAll(oldCompilers);
}
QList<PySideUicExtraCompiler *> PySideBuildStep::extraCompilers() const
{
return m_extraCompilers;
}
Id PySideBuildStep::id()
{
return Id("Python.PysideBuildStep");
}
class PythonBuildSettingsWidget : public QWidget
{
public:
PythonBuildSettingsWidget(PythonBuildConfiguration *bc)
{
using namespace Layouting;
m_configureDetailsWidget = new DetailsWidget;
m_configureDetailsWidget->setSummaryText(bc->python().toUserOutput());
if (const std::optional<FilePath> venv = bc->venv()) {
auto details = new QWidget();
Form{Tr::tr("Effective venv:"), venv->toUserOutput(), br}.attachTo(details);
m_configureDetailsWidget->setWidget(details);
} else {
m_configureDetailsWidget->setState(DetailsWidget::OnlySummary);
}
Column{
m_configureDetailsWidget,
noMargin
}.attachTo(this);
}
private:
DetailsWidget *m_configureDetailsWidget;
};
class PySideBuildStepFactory final : public BuildStepFactory
{
public:
PySideBuildStepFactory()
{
registerStep<PySideBuildStep>(PySideBuildStep::id());
setSupportedProjectType(PythonProjectId);
setDisplayName(Tr::tr("Run PySide6 project tool"));
setFlags(BuildStep::UniqueStep);
}
};
void setupPySideBuildStep()
{
static PySideBuildStepFactory thePySideBuildStepFactory;
}
// PythonBuildConfiguration
PythonBuildConfiguration::PythonBuildConfiguration(Target *target, const Id &id)
: BuildConfiguration(target, id)
{
setInitializer([this](const BuildInfo &info) { initialize(info); });
setConfigWidgetDisplayName(Tr::tr("Python"));
updateCacheAndEmitEnvironmentChanged();
connect(&PySideInstaller::instance(),
&PySideInstaller::pySideInstalled,
this,
&PythonBuildConfiguration::handlePythonUpdated);
auto update = [this] {
if (isActive()) {
buildSystem()->emitBuildSystemUpdated();
updateDocuments();
}
};
connect(target, &Target::activeBuildConfigurationChanged, this, update);
connect(project(), &Project::activeTargetChanged, this, update);
connect(ProjectExplorerPlugin::instance(),
&ProjectExplorerPlugin::fileListChanged,
this,
update);
connect(PythonSettings::instance(),
&PythonSettings::virtualEnvironmentCreated,
this,
&PythonBuildConfiguration::handlePythonUpdated);
}
QWidget *PythonBuildConfiguration::createConfigWidget()
{
return new PythonBuildSettingsWidget(this);
}
static QString venvTypeName()
{
static QString name = Tr::tr("New Virtual Environment");
return name;
}
void PythonBuildConfiguration::initialize(const BuildInfo &info)
{
buildSteps()->appendStep(PySideBuildStep::id());
if (info.typeName == venvTypeName()) {
m_venv = info.buildDirectory;
const FilePath venvInterpreterPath = info.buildDirectory.resolvePath(
HostOsInfo::isWindowsHost() ? FilePath::fromUserInput("Scripts/python.exe")
: FilePath::fromUserInput("bin/python"));
updatePython(venvInterpreterPath);
if (info.extraInfo.toMap().value("createVenv", false).toBool()
&& !info.buildDirectory.exists()) {
if (std::optional<Interpreter> python = PythonKitAspect::python(kit()))
PythonSettings::createVirtualEnvironment(python->command, info.buildDirectory);
}
} else {
updateInterpreter(PythonKitAspect::python(kit()));
}
updateCacheAndEmitEnvironmentChanged();
}
void PythonBuildConfiguration::updateInterpreter(const std::optional<Interpreter> &python)
{
updatePython(python ? python->command : FilePath());
}
void PythonBuildConfiguration::updatePython(const FilePath &python)
{
m_python = python;
if (auto buildStep = buildSteps()->firstOfType<PySideBuildStep>())
buildStep->checkForPySide(python);
updateDocuments();
buildSystem()->requestParse();
}
void PythonBuildConfiguration::updateDocuments()
{
if (isActive()) {
const FilePaths files = project()->files(Project::AllFiles);
for (const FilePath &file : files) {
if (auto doc = TextEditor::TextDocument::textDocumentForFilePath(file)) {
if (auto pyDoc = qobject_cast<PythonDocument *>(doc))
pyDoc->updatePython(m_python);
else if (doc->mimeType() == Utils::Constants::QML_MIMETYPE)
PySideInstaller::instance().checkPySideInstallation(m_python, doc);
}
}
}
}
void PythonBuildConfiguration::handlePythonUpdated(const FilePath &python)
{
if (!m_python.isEmpty() && python == m_python)
updatePython(python); // retrigger pyside check
}
static const char pythonKey[] = "python";
static const char venvKey[] = "venv";
void PythonBuildConfiguration::fromMap(const Store &map)
{
BuildConfiguration::fromMap(map);
if (map.contains(venvKey))
m_venv = FilePath::fromSettings(map[venvKey]);
updatePython(FilePath::fromSettings(map[pythonKey]));
}
void PythonBuildConfiguration::toMap(Store &map) const
{
BuildConfiguration::toMap(map);
map[pythonKey] = m_python.toSettings();
if (m_venv)
map[venvKey] = m_venv->toSettings();
}
FilePath PythonBuildConfiguration::python() const
{
return m_python;
}
std::optional<FilePath> PythonBuildConfiguration::venv() const
{
return m_venv;
}
class PythonBuildConfigurationFactory final : public BuildConfigurationFactory
{
public:
PythonBuildConfigurationFactory()
{
registerBuildConfiguration<PythonBuildConfiguration>("Python.PySideBuildConfiguration");
setSupportedProjectType(PythonProjectId);
setSupportedProjectMimeTypeNames(
{Constants::C_PY_PROJECT_MIME_TYPE, Constants::C_PY_PROJECT_MIME_TYPE_TOML});
setBuildGenerator([](const Kit *k, const FilePath &projectPath, bool forSetup) {
if (std::optional<Interpreter> python = PythonKitAspect::python(k)) {
BuildInfo base;
base.buildDirectory = projectPath.parentDir();
base.displayName = python->name;
base.typeName = Tr::tr("Global Python");
base.showBuildDirConfigWidget = false;
if (isVenvPython(python->command) || !venvIsUsable(python->command))
return QList<BuildInfo>{base};
base.enabledByDefault = false;
BuildInfo venv;
const FilePath venvBase = projectPath.parentDir() / ".qtcreator"
/ FileUtils::fileSystemFriendlyName(python->name + "venv");
venv.buildDirectory = venvBase;
int i = 2;
while (venv.buildDirectory.exists())
venv.buildDirectory = venvBase.stringAppended('_' + QString::number(i++));
//: %1 = name of this Python as registered in QtC
venv.displayName = Tr::tr("%1 Virtual Environment").arg(python->name);
venv.typeName = venvTypeName();
venv.extraInfo = QVariantMap{{"createVenv", forSetup}};
return QList<BuildInfo>{base, venv};
}
return QList<BuildInfo>{};
});
}
};
void setupPythonBuildConfiguration()
{
static PythonBuildConfigurationFactory thePythonBuildConfigurationFactory;
}
} // Python::Internal
|