Skip to content

Commit

Permalink
fix: [python] Package not updated
Browse files Browse the repository at this point in the history
The pip is not installed

Log: fix bug
Bug: https://pms.uniontech.com/bug-view-298501.html
  • Loading branch information
Kakueeen authored and deepin-ci-robot committed Jan 9, 2025
1 parent 4baf144 commit 6355a0e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 28 deletions.
60 changes: 42 additions & 18 deletions src/plugins/python/python/option/interpreterwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QDir>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStackedWidget>

DWIDGET_USE_NAMESPACE
class InterpreterModelPrivate
Expand Down Expand Up @@ -97,7 +98,7 @@ QVariant InterpreterModel::headerData(int section, Qt::Orientation orientation,
}
}

void InterpreterModel::setCustomData(QVector<QPair<QString, QString>> &data)
void InterpreterModel::setCustomData(const QVector<QPair<QString, QString>> &data)
{
beginResetModel();
d->packageVector = data;
Expand All @@ -110,6 +111,8 @@ class InterpreterWidgetPrivate

DComboBox *interpreterComboBox = nullptr;
DLineEdit *pipSourceEdit = nullptr;
QStackedWidget *packagesWidget = nullptr;
DLabel *msgLabel = nullptr;

//todo: modified it later: creating a generic component to manage the toolchain
DPushButton *selectCustomInterpreter = nullptr;
Expand Down Expand Up @@ -164,27 +167,35 @@ void InterpreterWidget::setupUi()
mainLayout->addWidget(d->selectCustomInterpreter, 0, 2);
mainLayout->addWidget(d->removeCustomInterpreter, 0, 3);

auto tableframe = new DFrame(this);
auto tablelayout = new QVBoxLayout(tableframe);
tableframe->setLayout(tablelayout);
auto pkgInfoFrame = new DFrame(this);
auto pkgLayout = new QVBoxLayout(pkgInfoFrame);
pkgInfoFrame->setLayout(pkgLayout);

d->packagesWidget = new QStackedWidget(this);
d->packagesWidget->setFixedHeight(180);
d->msgLabel = new DLabel(this);
d->msgLabel->setAlignment(Qt::AlignCenter);
d->msgLabel->setWordWrap(true);
d->msgLabel->setForegroundRole(DPalette::TextWarning);

DTableView *tableView = new DTableView();
tableView->setFrameShape(QFrame::NoFrame);
tableView->setAlternatingRowColors(true);
tableView->setShowGrid(false);
tableView->horizontalHeader()->setSectionResizeMode(DHeaderView::Stretch);
tableView->verticalHeader()->hide();
tablelayout->addWidget(tableView);

DHeaderView *headerView = tableView->horizontalHeader();
headerView->setDefaultAlignment(Qt::AlignLeft);

tableView->setSelectionMode(QAbstractItemView::SingleSelection);

d->model = new InterpreterModel();
tableView->setModel(d->model);
tableView->setFixedHeight(180);
mainLayout->addWidget(tableframe, 1, 0, 1, 4);

d->packagesWidget->addWidget(tableView);
d->packagesWidget->addWidget(d->msgLabel);
pkgLayout->addWidget(d->packagesWidget);

mainLayout->addWidget(pkgInfoFrame, 1, 0, 1, 4);

d->pipSourceEdit = new DLineEdit(this);
mainLayout->addWidget(new DLabel(tr("PIP Source:"), this), 2, 0);
Expand Down Expand Up @@ -257,19 +268,18 @@ void InterpreterWidget::updatePackageData()
{
auto param = qvariant_cast<ToolChainData::ToolChainParam>(d->interpreterComboBox->currentData(Qt::UserRole + 1));
QString cmd = param.path + " -m pip list";
QtConcurrent::run(this, &InterpreterWidget::findPackages, cmd);

QtConcurrent::run(this, &InterpreterWidget::queryPackages, cmd);
}

void InterpreterWidget::findPackages(const QString &cmd)
void InterpreterWidget::queryPackages(const QString &cmd)
{
QProcess process;
connect(&process, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
&process, [&](int exitcode, QProcess::ExitStatus exitStatus) {
QueryInfo info;
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();
Expand All @@ -280,20 +290,34 @@ void InterpreterWidget::findPackages(const QString &cmd)
|| sublist.at(0).indexOf("----") > -1) {
continue;
}
dataVector.append(QPair<QString, QString>(sublist.at(0).trimmed(), sublist.at(1).trimmed()));
info.packageList.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;
info.errMsg = process.readAllStandardError();
}
metaObject()->invokeMethod(this,
"applyQueryInfo",
Qt::QueuedConnection,
Q_ARG(QueryInfo, info));
});

process.start(cmd);
process.waitForFinished();
}

void InterpreterWidget::applyQueryInfo(const QueryInfo &info)
{
if (info.errMsg.isEmpty()) {
d->packagesWidget->setCurrentIndex(0);
QMutexLocker lk(&d->mutex);
d->model->setCustomData(info.packageList);
} else {
d->packagesWidget->setCurrentIndex(1);
d->msgLabel->setText(info.errMsg);
}
}

bool InterpreterWidget::getControlValue(QMap<QString, QVariant> &map)
{
int index = d->interpreterComboBox->currentIndex();
Expand Down
25 changes: 15 additions & 10 deletions src/plugins/python/python/option/interpreterwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@

#include <QAbstractTableModel>

enum ColumnID
{
enum ColumnID {
kPackage,
kVersion,
_KCount
};

struct InterpreterConfig{
struct InterpreterConfig
{
ToolChainData::ToolChainParam version;
};

struct QueryInfo
{
QVector<QPair<QString, QString>> packageList;
QString errMsg;
};

class InterpreterModelPrivate;
class InterpreterModel : public QAbstractTableModel
{
Expand All @@ -34,7 +40,7 @@ class InterpreterModel : public QAbstractTableModel
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;

void setCustomData(QVector<QPair<QString, QString>>& data);
void setCustomData(const QVector<QPair<QString, QString>> &data);

signals:

Expand All @@ -54,10 +60,6 @@ class InterpreterWidget : public PageWidget
void setUserConfig(const QMap<QString, QVariant> &map) override;
void getUserConfig(QMap<QString, QVariant> &map) override;

signals:

public slots:

private:
void setupUi();
void updateUi();
Expand All @@ -69,9 +71,12 @@ public slots:
void setControlValue(const QMap<QString, QVariant> &map);

void updatePackageData();
void findPackages(const QString &cmd);
void queryPackages(const QString &cmd);
Q_INVOKABLE void applyQueryInfo(const QueryInfo &info);

InterpreterWidgetPrivate *const d;
};

#endif // INTERPRETERWIDGET_H
Q_DECLARE_METATYPE(QueryInfo)

#endif // INTERPRETERWIDGET_H

0 comments on commit 6355a0e

Please sign in to comment.