| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "wordcountanalyse.h" |
| 6 | #include "common/common.h" |
| 7 | |
| 8 | namespace { |
| 9 | const QString wordcount{"wordcount" }; |
| 10 | } |
| 11 | WordCountAnalyse::WordCountAnalyse(QObject *parent) |
| 12 | : QProcess (parent) |
| 13 | { |
| 14 | auto procEnv = env::lang::get(env::lang::User, env::lang::Python, 3); |
| 15 | for (auto val : procEnv.keys()) { |
| 16 | qInfo()<< val << procEnv.value(val); |
| 17 | } |
| 18 | setProcessEnvironment(procEnv); |
| 19 | auto env = processEnvironment().systemEnvironment(); |
| 20 | for (auto val : env.keys()) { |
| 21 | qInfo() << val << env.value(val); |
| 22 | } |
| 23 | QObject::connect(this, &QProcess::errorOccurred, |
| 24 | this, &WordCountAnalyse::errorOccurred); |
| 25 | QObject::connect(this, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), |
| 26 | this, &WordCountAnalyse::finished); |
| 27 | // QObject::connect(this, &QProcess::readAllStandardOutput, |
| 28 | // this, &QDebug::redirectErr); |
| 29 | // QObject::connect(this, &QProcess::readAllStandardError, |
| 30 | // this, &WordCountAnalyse::redirectErr); |
| 31 | setWorkingDirectory(CustomPaths::global(CustomPaths::Scripts)+ QDir::separator() + "action-analysis" ); |
| 32 | } |
| 33 | |
| 34 | void WordCountAnalyse::setArgs(const ActionAnalyseArgs &args) |
| 35 | { |
| 36 | this->processArgs = args; |
| 37 | } |
| 38 | |
| 39 | ActionAnalyseArgs WordCountAnalyse::args() const |
| 40 | { |
| 41 | return this->processArgs; |
| 42 | } |
| 43 | |
| 44 | void WordCountAnalyse::setStorage(const QString &storage) |
| 45 | { |
| 46 | processArgs.storage = storage; |
| 47 | } |
| 48 | |
| 49 | QString WordCountAnalyse::getStorage() const |
| 50 | { |
| 51 | return processArgs.storage; |
| 52 | } |
| 53 | |
| 54 | void WordCountAnalyse::setWorkspace(const QString &workspace) |
| 55 | { |
| 56 | processArgs.workspace = workspace; |
| 57 | } |
| 58 | |
| 59 | QString WordCountAnalyse::getWorkspace() const |
| 60 | { |
| 61 | return processArgs.workspace; |
| 62 | } |
| 63 | |
| 64 | void WordCountAnalyse::setLanguage(const QString &language) |
| 65 | { |
| 66 | processArgs.language = language; |
| 67 | } |
| 68 | |
| 69 | QString WordCountAnalyse::getLanguage() const |
| 70 | { |
| 71 | return processArgs.language; |
| 72 | } |
| 73 | |
| 74 | void WordCountAnalyse::start() |
| 75 | { |
| 76 | qInfo() << workingDirectory(); |
| 77 | setProcessChannelMode(QProcess::ForwardedChannels); |
| 78 | setProgram(getPythonVersion()); |
| 79 | QStringList args; |
| 80 | args << "./main.py" ; |
| 81 | args << "-w" << processArgs.workspace; |
| 82 | args << "-s" << processArgs.storage; |
| 83 | args << "-l" << processArgs.language; |
| 84 | setArguments(args); |
| 85 | QProcess::start(); |
| 86 | } |
| 87 | |
| 88 | QString WordCountAnalyse::getPythonVersion() |
| 89 | { |
| 90 | if (pythonVersion.isEmpty()) { |
| 91 | QDir dir("/usr/bin" ); |
| 92 | QStringList filter {"Python*.*" }; |
| 93 | QStringList pythonList = dir.entryList(filter); |
| 94 | |
| 95 | QRegExp reg("((\\d)|(\\d.\\d))($|\\s)" ); |
| 96 | reg.setMinimal(true); |
| 97 | int position = 0; |
| 98 | QList<QString> versions; |
| 99 | while (position >= 0) { |
| 100 | position = reg.indexIn(pythonList.join(" " ), position); |
| 101 | if (position < 0) |
| 102 | break; |
| 103 | versions << reg.cap(1); |
| 104 | position += reg.matchedLength(); |
| 105 | } |
| 106 | |
| 107 | double newVersion = 0; |
| 108 | for (auto version : versions) { |
| 109 | double v = version.toDouble(); |
| 110 | if (v > newVersion) { |
| 111 | newVersion = v; |
| 112 | } |
| 113 | } |
| 114 | pythonVersion = "python" + QString::number(newVersion); |
| 115 | } |
| 116 | return pythonVersion; |
| 117 | } |
| 118 | |
| 119 | ActionAnalyseArgs::ActionAnalyseArgs() |
| 120 | { |
| 121 | |
| 122 | } |
| 123 | |
| 124 | ActionAnalyseArgs::ActionAnalyseArgs(const QString &workspace, |
| 125 | const QString &language, |
| 126 | const QString &storage) |
| 127 | : workspace(workspace) |
| 128 | , language(language) |
| 129 | , storage(storage) |
| 130 | { |
| 131 | |
| 132 | } |
| 133 | |
| 134 | ActionAnalyseArgs::ActionAnalyseArgs(const ActionAnalyseArgs &as) |
| 135 | : workspace(as.workspace) |
| 136 | , language(as.language) |
| 137 | , storage(as.storage) |
| 138 | { |
| 139 | |
| 140 | } |
| 141 | |
| 142 | ActionAnalyseArgs &ActionAnalyseArgs::operator=(const ActionAnalyseArgs &as) |
| 143 | { |
| 144 | workspace = as.workspace; |
| 145 | language = as.language; |
| 146 | storage = as.storage; |
| 147 | return *this; |
| 148 | } |
| 149 | |
| 150 | void WordCountAnalyse::errorOccurred(QProcess::ProcessError err) |
| 151 | { |
| 152 | qCritical() << exitCode() << exitStatus() << err << readAllStandardError(); |
| 153 | } |
| 154 | |
| 155 | void WordCountAnalyse::finished(int exitCode, QProcess::ExitStatus status) |
| 156 | { |
| 157 | qCritical() << exitCode << status << readAllStandardOutput(); |
| 158 | if (exitCode == 0) |
| 159 | analyseDone(true); |
| 160 | else |
| 161 | analyseDone(false); |
| 162 | } |
| 163 | |