Debian Codegraph @

src/risset.cpp

raw symbols SCIP
1 #include "risset.h"
2 #include <QString>
3 #include <QProcess>
4 #include <QDebug>
5 #include <QJsonDocument>
6
7 #include <QJsonDocument>
8 #include <QJsonArray>
9 #include <QJsonObject>
10 #include <QJsonParseError>
11 #include <QJsonValue>
12 #include <QDir>
13 #include <iostream>
14 #include "types.h"
15
16 /**
17 * @brief like unix' which, but cross-platform. Based on python shutil.which
18 * @param cmd - The command to find
19 * @return the absolute path, or an empty string if not found
20 */
21 static QString which(QString cmd, QString otherwise) {
22 if(QDir::isAbsolutePath(cmd))
23 return cmd;
24
25 QString PATH = QProcessEnvironment::systemEnvironment().value("PATH");
26 if(PATH.isEmpty())
27 return "";
28 #if defined(Q_OS_WIN)
29 QFileInfo finfo(cmd);
30 if(finfo.suffix().isEmpty()) {
31 cmd = cmd + ".exe";
32 }
33 #endif
34
35 QStringList paths = PATH.split(QDir::listSeparator());
36
37 QSet<QString> seen;
38 foreach(auto dir, paths) {
39 #ifdef Q_OS_WIN
40 dir = QDir::toNativeSeparators(dir.toLower());
41 #endif
42 if(!seen.contains(dir)) {
43 seen.insert(dir);
44 QString name = QDir(dir).filePath(cmd);
45 QDEBUG << "Inspecting " << name;
46 if(QFile::exists(name)) {
47 QDEBUG << "Found!" << name;
48 return name;
49 }
50 }
51 }
52 return otherwise;
53 }
54
55 Risset::Risset(QString pythonExe)
56 {
57 opcodeIndexDone = false;
58 #if defined(Q_OS_LINUX)
59 csoundqtDataRoot.setPath(QDir::home().filePath(".local/share/csoundqt"));
60 #elif defined(Q_OS_WIN)
61 csoundqtDataRoot.setPath(QDir(QProcessEnvironment::systemEnvironment().value("LOCALAPPDATA")).filePath("csoundqt"));
62 #elif defined(Q_OS_MACOS)
63 csoundqtDataRoot.setPath(QDir::home().filePath("Library/Application Support/csoundqt"));
64 #else
65 csoundqtDataRoot.setPath(QDir::home().filePath(".csoundqt"));
66 #endif
67 // local path to download the html docs from github.com//csound-plugins/risset-docs
68 rissetDocsRepoPath.setPath(csoundqtDataRoot.filePath("risset-docs"));
69 // Try the risset script
70 QString rissetScriptPath = which("risset", "risset");
71 QStringList args = {"info", "--full"};
72 QProcess proc;
73 proc.start(rissetScriptPath, args);
74 auto finishok = proc.waitForFinished();
75 if (!finishok) {
76 QDEBUG << "Risset script not installed" << proc.errorString();
77 m_rissetPath = "";
78 if(QFile::exists(pythonExe))
79 m_pythonExe = pythonExe;
80 else
81 m_pythonExe = which(!pythonExe.isEmpty() ? pythonExe: "python3", "python3");
82
83 QDEBUG << "Python binary: " << m_pythonExe;
84
85 proc.start(m_pythonExe, {"-m", "risset", "info", "--full"});
86 finishok = proc.waitForFinished();
87 if (!finishok) {
88 isInstalled = false;
89 QDEBUG << "Risset not installed for python " << m_pythonExe << ", error: " << proc.errorString();
90 return;
91 }
92 } else {
93 m_rissetPath = rissetScriptPath;
94 }
95
96 auto procOut = proc.readAllStandardOutput();
97 m_infoText = QString::fromLocal8Bit(procOut);
98 QJsonDocument m_jsonInfo = QJsonDocument::fromJson(procOut);
99 QJsonObject root = m_jsonInfo.object();
100 rissetVersion = root.value("version").toString();
101 if(rissetVersion.isEmpty()) {
102 isInstalled = false;
103 QDEBUG << "Risset did not execute correctly: " << proc.errorString();
104 return;
105 }
106 m_rissetPath = rissetScriptPath;
107 rissetRoot.setPath(root.value("rissetroot").toString());
108 rissetHtmlDocs.setPath(root.value("htmldocs").toString());
109 rissetOpcodesXml = root.value("opcodesxml").toString();
110 if(!QFile::exists(rissetOpcodesXml)) {
111 QDEBUG << "Did not find opcodes.xml, path = " << rissetOpcodesXml;
112 this->generateOpcodesXml();
113 }
114
115 rissetManpages.setPath(root.value("manpages").toString());
116 QDEBUG << "Risset opcodes.xml: " << rissetOpcodesXml;
117 QDEBUG << "Risset version: " << rissetVersion;
118 QDEBUG << "Risset manual: " << rissetHtmlDocs.absolutePath();
119 this->opcodeNames.clear();
120 auto plugins = root.value(QString("plugins")).toObject();
121 for(auto& pluginName: plugins.keys()) {
122 QDEBUG << "Risset - parsing plugin" << pluginName;
123 auto plugindef = plugins.value(pluginName).toObject();
124 bool installed = plugindef.value("installed").toBool();
125 auto opcodesInPlugin = plugindef.value("opcodes").toArray();
126 QStringList opcodesList;
127 for(auto opcodeName: opcodesInPlugin) {
128 QString opcodeNameStr = opcodeName.toString();
129 opcodeNames.append(opcodeNameStr);
130 opcodeToPlugin[opcodeNameStr] = pluginName;
131 opcodesList.append(opcodeNameStr);
132 }
133 pluginInstalled[pluginName] = installed;
134 this->pluginOpcodes[pluginName] = opcodesList;
135 if(installed) {
136 this->installedPlugins.append(pluginName);
137 }
138
139 }
140 // QDEBUG << "Risset opcodes: " << opcodeNames;
141 opcodeIndexDone = true;
142 }
143
144
145 void Risset::markOpcodeTree(OpEntryParser *tree) {
146 // Mark the opcodes which are not installed. This information can be used, for example
147 // for more nuanced highlighting
148 for(auto& opcode : tree->opcodeList) {
149 auto pluginName = this->opcodeToPlugin.value(opcode.opcodeName);
150 if(!pluginName.isEmpty()) {
151 opcode.isInstalled = this->pluginInstalled[pluginName];
152 }
153 }
154 }
155
156
157 QString Risset::markdownManpage(QString opcodeName) {
158 if(!rissetManpages.exists()) {
159 QDEBUG << "Risset markdown root not set";
160 return "";
161 }
162 QString path = rissetManpages.filePath(opcodeName + ".md");
163 if(QFile::exists(path))
164 return path;
165 else {
166 QDEBUG << "Did not find markdown manpage for opcode" << opcodeName;
167 QDEBUG << "... Searched: " << path;
168 return "";
169 }
170
171 }
172
173
174 QString Risset::htmlManpage(QString opcodeName) {
175 // TODO!
176 if(rissetHtmlDocs.exists()) {
177 QString path = rissetHtmlDocs.filePath("opcodes/" + opcodeName + ".html");
178 if(QFile::exists(path)) {
179 QDEBUG << "Risset: found html man page for opcode" << opcodeName << ": " << path;
180 return path;
181 }
182 else {
183 QDEBUG << "Risset: did not find html page for opcode" << opcodeName << "\n"
184 << "... Searched: " << path;
185 }
186 }
187 else
188 QDEBUG << "Risset's documentation not found. Searched:" << rissetHtmlDocs.path();
189 return "";
190 }
191
192 RissetError Risset::generateOpcodesXml() {
193 if (!isInstalled) {
194 QDEBUG << "Risset is not installed";
195 return RissetError::RissetNotInstalled;
196 }
197 QString executable;
198 QStringList args;
199 QString path = this->defaultOpcodesXmlPath();
200
201 if (!m_rissetPath.isEmpty()) {
202 executable = m_rissetPath;
203 args = QStringList({"--debug", "dev", "--outfile", path, "opcodesxml"});
204 } else if(!m_pythonExe.isEmpty()) {
205 executable = m_pythonExe;
206 args = QStringList({"-m", "risset", "--debug", "dev", "--outfile", path, "opcodesxml"});
207 } else {
208 QDEBUG << "Either the risset script should be known or the python executable "
209 "should be known...";
210 return RissetError::RissetNotInstalled;
211 }
212
213 QProcess proc;
214 QDEBUG << "Calling risset to generate opcodes.xml. exec: " << executable << ", args: " << args;
215 proc.start(executable, args);
216 proc.waitForFinished();
217 if(proc.exitCode() != 0) {
218 QDEBUG << "Error while generating opcodesxml";
219 return RissetError::Error;
220 }
221 rissetOpcodesXml = path;
222 return RissetError::Ok;
223 }
224
225
226
227 RissetError Risset::generateDocumentation(std::function<void(int)> callback) {
228 if (!isInstalled) {
229 QDEBUG << "Risset is not installed";
230 return RissetError::RissetNotInstalled;
231 }
232
233 QString executable;
234 QStringList args;
235
236 if (!m_rissetPath.isEmpty()) {
237 executable = m_rissetPath;
238 args = QStringList({"makedocs"});
239 } else {
240 executable = m_pythonExe;
241 args = QStringList({"-m", "risset", "makedocs"});
242 }
243
244 if(callback == nullptr) {
245 QProcess proc;
246 proc.start(executable, args);
247 proc.waitForFinished();
248 if(proc.exitCode() != 0) {
249 QDEBUG << "Error while making documentation. Risset args: " << args;
250 return RissetError::Error;
251 } else {
252 if(!QFile::exists(rissetHtmlDocs.filePath("index.html"))) {
253 QDEBUG << "Risset::generateDocumentation failed to genererate HTML docs, path: "
254 << rissetHtmlDocs.path();
255 return RissetError::HtmlError;
256 }
257 QDEBUG << "Risset makedocs OK!";
258 return RissetError::Ok;
259 }
260 }
261 else {
262 QProcess *proc = new QProcess();
263 proc->start(executable, args);
264 runningProcesses.append(proc);
265 QObject::connect(proc, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
266 [this, callback](int exitCode, QProcess::ExitStatus exitStatus) {
267 if(exitStatus == QProcess::CrashExit)
268 exitCode = -1;
269 callback(exitCode);
270 this->cleanupProcesses();
271 });
272 return RissetError::Ok;
273 }
274 }
275
276
277 static bool updateGitRepo(QString path) {
278 QProcess proc;
279 QStringList args = {"-C", path, "update"};
280 proc.start("git", args);
281 proc.waitForFinished();
282 return true;
283 }
284
285
286 static bool cloneGitRepo(QString url, QString path) {
287 QProcess proc;
288 proc.start("git", {"clone", "--depth", "1", url, path});
289 proc.waitForFinished();
290 return true;
291 }
292
293
294 bool Risset::downloadDocumentation() {
295 // TODO
296 if(rissetDocsRepoPath.exists()) {
297 return updateGitRepo(rissetDocsRepoPath.path());
298 }
299 else {
300 return cloneGitRepo("https://github.com/csound-plugins/risset-docs.git", rissetDocsRepoPath.path());
301 }
302 }
303
304 QString Risset::findHtmlDocumentation() {
305 if(QFile::exists(rissetHtmlDocs.filePath("index.html"))) {
306 return rissetHtmlDocs.path();
307 }
308 else if(QFile::exists(rissetDocsRepoPath.filePath("site/index.html"))) {
309 return rissetDocsRepoPath.filePath("site");
310 }
311 else
312 return "";
313 }
314
315
316 void Risset::initIndex() {
317 }
318
319 void Risset::cleanupProcesses()
320 {
321 QMutableListIterator<QProcess*> it(runningProcesses);
322 while (it.hasNext()) {
323 auto proc = it.next();
324 if(proc->processId() == 0) {
325 delete proc;
326 it.remove();
327 }
328 }
329 QDEBUG << "Running processes: " << runningProcesses.size();
330 }
331
332
333