Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [python] Added pip source configuration #1032

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/plugins/python/installer/pipinstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "pipinstaller.h"
#include "utils/utils.h"

Check warning on line 6 in src/plugins/python/installer/pipinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "utils/utils.h" not found.

Check warning on line 6 in src/plugins/python/installer/pipinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "utils/utils.h" not found.

#include "common/util/eventdefinitions.h"

Check warning on line 8 in src/plugins/python/installer/pipinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "common/util/eventdefinitions.h" not found.

Check warning on line 8 in src/plugins/python/installer/pipinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "common/util/eventdefinitions.h" not found.
#include "services/option/optionmanager.h"

Check warning on line 9 in src/plugins/python/installer/pipinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "services/option/optionmanager.h" not found.

Check warning on line 9 in src/plugins/python/installer/pipinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "services/option/optionmanager.h" not found.
#include "services/terminal/terminalservice.h"

Check warning on line 10 in src/plugins/python/installer/pipinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "services/terminal/terminalservice.h" not found.

Check warning on line 10 in src/plugins/python/installer/pipinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "services/terminal/terminalservice.h" not found.
#include "services/window/windowelement.h"

Check warning on line 11 in src/plugins/python/installer/pipinstaller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "services/window/windowelement.h" not found.

Check warning on line 11 in src/plugins/python/installer/pipinstaller.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "services/window/windowelement.h" not found.

#include <QProcess>
#include <QStandardPaths>
Expand Down Expand Up @@ -65,6 +66,11 @@
<< "--target"
<< Utils::packageInstallPath(python);

const auto &map = OptionManager::getInstance()->getValue(option::CATEGORY_PYTHON, "Interpreter").toMap();
const auto &pipSrc = map.value("pipSource").toString();
if (!pipSrc.isEmpty())
args << "-i" << pipSrc;

uiController.switchContext(TERMINAL_TAB_TEXT);
termSrv->executeCommand(info.plugin.isEmpty() ? "PIPInstaller" : info.plugin, python, args, "", QStringList());
}
116 changes: 62 additions & 54 deletions src/plugins/python/python/option/interpreterwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "interpreterwidget.h"
#include "utils/utils.h"

Check warning on line 6 in src/plugins/python/python/option/interpreterwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "utils/utils.h" not found.

Check warning on line 6 in src/plugins/python/python/option/interpreterwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "utils/utils.h" not found.
#include "services/option/toolchaindata.h"

Check warning on line 7 in src/plugins/python/python/option/interpreterwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "services/option/toolchaindata.h" not found.

Check warning on line 7 in src/plugins/python/python/option/interpreterwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "services/option/toolchaindata.h" not found.
#include "common/util/custompaths.h"

Check warning on line 8 in src/plugins/python/python/option/interpreterwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "common/util/custompaths.h" not found.

Check warning on line 8 in src/plugins/python/python/option/interpreterwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "common/util/custompaths.h" not found.
#include "common/toolchain/toolchain.h"

Check warning on line 9 in src/plugins/python/python/option/interpreterwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "common/toolchain/toolchain.h" not found.

Check warning on line 9 in src/plugins/python/python/option/interpreterwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "common/toolchain/toolchain.h" not found.

#include <DComboBox>
#include <DTableView>
Expand All @@ -13,9 +14,10 @@
#include <DLabel>
#include <DWidget>
#include <DFrame>
#include <DPushButton>

Check warning on line 17 in src/plugins/python/python/option/interpreterwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DPushButton> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 17 in src/plugins/python/python/option/interpreterwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <DPushButton> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DFileDialog>
#include <DDialog>
#include <DLineEdit>

#include <QtConcurrent>
#include <QDir>
Expand All @@ -31,15 +33,12 @@
};

InterpreterModel::InterpreterModel(QObject *parent)
: QAbstractTableModel(parent)
, d(new InterpreterModelPrivate())
: QAbstractTableModel(parent), d(new InterpreterModelPrivate())
{

}

InterpreterModel::~InterpreterModel()
{

}

int InterpreterModel::rowCount(const QModelIndex &parent) const
Expand Down Expand Up @@ -71,12 +70,12 @@
QString version = d->packageVector.at(index.row()).second;

switch (index.column()) {
case ColumnID::kPackage:
return package;
case ColumnID::kVersion:
return version;
default:
return QVariant();
case ColumnID::kPackage:
return package;
case ColumnID::kVersion:
return version;
default:
return QVariant();
}
}

Expand All @@ -89,16 +88,16 @@
return QVariant();

switch (section) {
case ColumnID::kPackage:
return tr("Package");
case ColumnID::kVersion:
return tr("Version");
default:
return QVariant();
case ColumnID::kPackage:
return tr("Package");
case ColumnID::kVersion:
return tr("Version");
default:
return QVariant();
}
}

void InterpreterModel::setCustomData(QVector<QPair<QString, QString>>& data)
void InterpreterModel::setCustomData(QVector<QPair<QString, QString>> &data)
{
beginResetModel();
d->packageVector = data;
Expand All @@ -110,6 +109,7 @@
friend class InterpreterWidget;

DComboBox *interpreterComboBox = nullptr;
DLineEdit *pipSourceEdit = nullptr;

//todo: modified it later: creating a generic component to manage the toolchain
DPushButton *selectCustomInterpreter = nullptr;
Expand All @@ -124,8 +124,7 @@
};

InterpreterWidget::InterpreterWidget(QWidget *parent)
: PageWidget(parent)
, d(new InterpreterWidgetPrivate())
: PageWidget(parent), d(new InterpreterWidgetPrivate())
{
d->toolChainData.reset(new ToolChainData());
QString retMsg;
Expand All @@ -147,10 +146,9 @@

void InterpreterWidget::setupUi()
{
QVBoxLayout *vLayout = new QVBoxLayout();
setLayout(vLayout);
QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->setColumnStretch(1, 1);

QHBoxLayout *hLayout = new QHBoxLayout();
DLabel *label = new DLabel(tr("Python Interpreter:"));
label->setFixedWidth(180);
d->interpreterComboBox = new DComboBox();
Expand All @@ -161,10 +159,10 @@
d->selectCustomInterpreter = new DPushButton(tr("Browse"), this);
d->removeCustomInterpreter = new DPushButton(tr("Remove"), this);

hLayout->addWidget(label);
hLayout->addWidget(d->interpreterComboBox);
hLayout->addWidget(d->selectCustomInterpreter);
hLayout->addWidget(d->removeCustomInterpreter);
mainLayout->addWidget(label, 0, 0);
mainLayout->addWidget(d->interpreterComboBox, 0, 1);
mainLayout->addWidget(d->selectCustomInterpreter, 0, 2);
mainLayout->addWidget(d->removeCustomInterpreter, 0, 3);

auto tableframe = new DFrame(this);
auto tablelayout = new QVBoxLayout(tableframe);
Expand All @@ -186,15 +184,18 @@
d->model = new InterpreterModel();
tableView->setModel(d->model);
tableView->setFixedHeight(180);
vLayout->addLayout(hLayout);
vLayout->addWidget(tableframe);
mainLayout->addWidget(tableframe, 1, 0, 1, 4);

d->pipSourceEdit = new DLineEdit(this);
mainLayout->addWidget(new DLabel(tr("PIP Source:"), this), 2, 0);
mainLayout->addWidget(d->pipSourceEdit, 2, 1, 1, 3);

connect(d->selectCustomInterpreter, &QPushButton::clicked, this, [=](){
connect(d->selectCustomInterpreter, &QPushButton::clicked, this, [=]() {
auto path = DFileDialog::getOpenFileName(this, tr("Select Local Interpreter"), QDir::homePath(), "Python interpreters (python* python3*)");
if (path.isEmpty())
return;
auto name = QFileInfo(path).fileName();
ToolChainData::ToolChainParam param {name, path};
ToolChainData::ToolChainParam param { name, path };
int index = 0;
while (index < d->interpreterComboBox->count()) {
auto temp = qvariant_cast<ToolChainData::ToolChainParam>(d->interpreterComboBox->itemData(index, Qt::UserRole + 1));
Expand All @@ -214,7 +215,7 @@
d->interpreterComboBox->setCurrentIndex(index);
d->customInterpreters.append(param);
});
connect(d->removeCustomInterpreter, &QPushButton::clicked, this, [=](){
connect(d->removeCustomInterpreter, &QPushButton::clicked, this, [=]() {
DDialog dialog;
dialog.setMessage(tr("Confirm the removal of this interpreter?"));
dialog.setWindowTitle(tr("Remove"));
Expand Down Expand Up @@ -264,31 +265,31 @@
void InterpreterWidget::findPackages(const QString &cmd)
{
QProcess process;
connect(&process, static_cast<void (QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished),
connect(&process, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
&process, [&](int exitcode, QProcess::ExitStatus exitStatus) {
if (0 == exitcode && exitStatus == QProcess::ExitStatus::NormalExit) {
QString output = QString(process.readAllStandardOutput());

QVector<QPair<QString, QString>> dataVector;
QStringList list = output.split("\n");
foreach (QString value, list) {
value = value.trimmed();
value = value.replace(QRegExp("[\\s]+"), ",");
QStringList sublist = value.split(",");
if (sublist.count() > 1) {
if (sublist.at(0).indexOf("Package") > -1
|| sublist.at(0).indexOf("----") > -1) {
continue;
if (0 == exitcode && exitStatus == QProcess::ExitStatus::NormalExit) {
QString output = QString(process.readAllStandardOutput());

QVector<QPair<QString, QString>> dataVector;
QStringList list = output.split("\n");
foreach (QString value, list) {
value = value.trimmed();
value = value.replace(QRegExp("[\\s]+"), ",");
QStringList sublist = value.split(",");
if (sublist.count() > 1) {
if (sublist.at(0).indexOf("Package") > -1
|| sublist.at(0).indexOf("----") > -1) {
continue;
}
dataVector.append(QPair<QString, QString>(sublist.at(0).trimmed(), sublist.at(1).trimmed()));
}
}
dataVector.append(QPair<QString, QString>(sublist.at(0).trimmed(), sublist.at(1).trimmed()));
QMutexLocker lk(&d->mutex);
d->model->setCustomData(dataVector);
} else {
qInfo() << "Error" << exitcode << exitStatus;
}
}
QMutexLocker lk(&d->mutex);
d->model->setCustomData(dataVector);
} else {
qInfo() << "Error" << exitcode << exitStatus;
}
});
});

process.start(cmd);
process.waitForFinished();
Expand Down Expand Up @@ -348,6 +349,7 @@

map.insert("version", currentInterpreter);
map.insert("customInterpreters", list);
map.insert("pipSource", d->pipSourceEdit->text());
return true;
}

Expand All @@ -364,6 +366,13 @@
d->customInterpreters.append(interpreter);
}

if (map.contains("pipSource")) {
const auto &pipSrc = map.value("pipSource").toString();
d->pipSourceEdit->setText(pipSrc);
} else {
d->pipSourceEdit->setText(Utils::defaultPIPSource());
}

return true;
}

Expand All @@ -376,4 +385,3 @@
{
getControlValue(map);
}

5 changes: 5 additions & 0 deletions src/plugins/python/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ QString Utils::packageInstallPath(const QString &python)
return QStandardPaths::writableLocation(QStandardPaths::HomeLocation)
+ "/.unioncode/packages/Python" + version;
}

QString Utils::defaultPIPSource()
{
return "https://pypi.tuna.tsinghua.edu.cn/simple";
}
1 change: 1 addition & 0 deletions src/plugins/python/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Utils
static bool createVenv(const QString &python, const QString &venvPath);
static QString pythonVersion(const QString &python);
static QString packageInstallPath(const QString &python);
static QString defaultPIPSource();
};

#endif // UTILS_H
Loading