1 Star 0 Fork 0

Codeya-IDE/Codeya-IDE

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
utils.cpp 2.41 KB
一键复制 编辑 原始数据 按行查看 历史
Lu Zhen 提交于 2026-02-02 14:11 +08:00 . feat: init
// 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";
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Codeya-IDE/codeya-ide.git
git@gitee.com:Codeya-IDE/codeya-ide.git
Codeya-IDE
codeya-ide
Codeya-IDE
master

搜索帮助