代码拉取完成,页面将自动刷新
// SPDX-FileCopyrightText: 2024 - 2025 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "utils.h"
#include <QDir>
#include <QFileInfo>
#include <QProcess>
#include <QRegularExpression>
#include <QStandardPaths>
bool Utils::checkVenvValid(const QString &venvPath)
{
QFileInfo info(venvPath);
if (info.exists() && !info.isDir())
return false;
QDir binDir(venvPath + "/bin");
if (!binDir.exists())
return false;
const auto &fileList = binDir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries);
auto iter = std::find_if(fileList.cbegin(), fileList.cend(),
[](const QString &file) {
return file.startsWith("python");
});
return iter != fileList.cend();
}
bool Utils::createVenv(const QString &python, const QString &venvPath)
{
if (checkVenvValid(venvPath))
return true;
QProcess process;
QStringList args { "-m", "venv", venvPath };
process.setProgram(python);
process.setArguments(args);
process.start();
process.waitForFinished();
return process.exitCode() == 0;
}
QString Utils::pythonVersion(const QString &python)
{
auto getVersion = [](const QString &output) -> QString {
static QRegularExpression regex(R"((\d{1,3}(?:\.\d{1,3}){0,2}))");
if (output.isEmpty())
return "";
QRegularExpressionMatch match = regex.match(output);
if (match.hasMatch())
return match.captured(1);
return "";
};
QProcess process;
process.start(python, { "--version" });
process.waitForFinished();
QString output = process.readAllStandardOutput();
QString version = getVersion(output);
if (version.isEmpty()) {
output = process.readAllStandardError();
version = getVersion(output);
if (version.isEmpty()) {
int index = python.lastIndexOf('/') + 1;
output = python.mid(index, python.length() - index);
version = getVersion(output);
}
}
return version;
}
QString Utils::packageInstallPath(const QString &python)
{
const auto &version = pythonVersion(python);
return QStandardPaths::writableLocation(QStandardPaths::HomeLocation)
+ "/.unioncode/packages/Python" + version;
}
QString Utils::defaultPIPSource()
{
return "https://pypi.tuna.tsinghua.edu.cn/simple";
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。