|
11 | 11 | * GNU General Public License for more details. |
12 | 12 | */ |
13 | 13 |
|
14 | | - |
15 | 14 | #include "../src/PyRunner.hpp" |
16 | | -#include <QFileInfo> |
17 | | - |
18 | | -PyRunner::PyRunner(const QString& cwd) |
19 | | - : m_env(QProcessEnvironment::systemEnvironment()) { |
20 | | - m_status = 0; |
21 | | - if (QFileInfo("/usr/bin/python3").exists()) { |
22 | | - m_command = "/usr/bin/python3"; |
23 | | - } else if (QFileInfo("/usr/bin/python").exists()) { |
24 | | - m_command = "/usr/bin/python"; |
25 | | - } |
26 | | - m_process.setWorkingDirectory(cwd); |
27 | | - m_process.setProcessEnvironment(m_env); |
| 15 | +#include <Python.h> |
| 16 | +#include <vector> |
| 17 | +#include <string> |
| 18 | +#include <iostream> |
| 19 | +#include <filesystem> |
| 20 | +#include <cstdio> |
| 21 | + |
| 22 | +namespace fs = std::filesystem; |
| 23 | + |
| 24 | +PyRunner::PyRunner() : m_status(0) { |
| 25 | + Py_Initialize(); |
| 26 | +} |
| 27 | + |
| 28 | +PyRunner::PyRunner(const std::string& cwd) : m_cwd(cwd), m_status(0) { |
| 29 | + Py_Initialize(); |
28 | 30 | } |
29 | 31 |
|
30 | | -void PyRunner::run(const QStringList& arguments) { |
31 | | - m_process.setProgram(m_command); |
32 | | - m_process.setArguments(arguments); |
33 | | - qDebug() << m_command << arguments; |
34 | | - m_process.start(); |
35 | | - |
36 | | - if (m_process.waitForStarted() == true) { |
37 | | - m_process.waitForFinished(); |
38 | | - qDebug().noquote() << "\n" |
39 | | - << m_process.readAllStandardOutput(); |
40 | | - qDebug().noquote() |
41 | | - << m_process.readAllStandardError(); |
42 | | - qDebug().noquote() << "\nExit status :" << m_process.exitStatus(); |
43 | | - m_status = m_process.exitCode(); |
| 32 | +PyRunner::~PyRunner() { |
| 33 | + Py_Finalize(); |
| 34 | +} |
| 35 | + |
| 36 | +bool PyRunner::setUpCamp(const std::string& level) { |
| 37 | + if (!fs::exists(level)) return false; |
| 38 | + m_cwd = level; |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +void PyRunner::run(const std::string& script, |
| 43 | + const std::vector<std::string>& args) { |
| 44 | + if (!Py_IsInitialized()) { |
| 45 | + std::cerr << "Python not initialized!" << std::endl; |
| 46 | + m_status = 1; |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + std::cerr << "[PyRunner] Executing script file: " << script << std::endl; |
| 51 | + |
| 52 | + // Add working dir to sys.path |
| 53 | + if (!m_cwd.empty()) { |
| 54 | + PyObject *sysPath = PySys_GetObject("path"); |
| 55 | + PyObject *cwdPath = PyUnicode_FromString(m_cwd.c_str()); |
| 56 | + PyList_Insert(sysPath, 0, cwdPath); |
| 57 | + Py_DECREF(cwdPath); |
| 58 | + } |
| 59 | + |
| 60 | + // Set sys.argv |
| 61 | + PyObject *argvList = PyList_New(args.size()); |
| 62 | + for (size_t i = 0; i < args.size(); ++i) { |
| 63 | + PyObject *arg = PyUnicode_FromString(args[i].c_str()); |
| 64 | + PyList_SET_ITEM(argvList, i, arg); // steals ref |
| 65 | + } |
| 66 | + PySys_SetObject("argv", argvList); |
| 67 | + Py_DECREF(argvList); |
| 68 | + |
| 69 | + std::string fullpath = |
| 70 | + m_cwd.empty() ? script : (m_cwd + "/" + script + ".py"); |
| 71 | + FILE* fp = fopen(fullpath.c_str(), "r"); |
| 72 | + if (!fp) { |
| 73 | + std::cerr << "[PyRunner] Failed to open script file: " |
| 74 | + << fullpath << std::endl; |
| 75 | + m_status = 1; |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + int result = PyRun_SimpleFile(fp, fullpath.c_str()); |
| 80 | + fclose(fp); |
| 81 | + |
| 82 | + if (result != 0) { |
| 83 | + std::cerr << "[PyRunner] Python script returned error.\n"; |
| 84 | + m_status = 1; |
44 | 85 | } else { |
45 | | - qWarning() << "Failed to start python process!"; |
| 86 | + std::cerr << "[PyRunner] Script executed successfully.\n"; |
| 87 | + m_status = 0; |
46 | 88 | } |
47 | 89 | } |
48 | 90 |
|
49 | | -qint64 PyRunner::updateLevel(qint64 lid) { |
50 | | - QStringList arguments; |
51 | | - arguments << "tombll_manage_data.py" << "-u" << QString::number(lid); |
52 | | - run(arguments); |
53 | | - return getStatus(); |
| 91 | +int64_t PyRunner::updateLevel(int64_t lid) { |
| 92 | + run("tombll_manage_data", |
| 93 | + {"tombll_manage_data.py", "-u", std::to_string(lid)}); |
| 94 | + return m_status; |
54 | 95 | } |
55 | 96 |
|
56 | | -qint64 PyRunner::syncCards(qint64 lid ) { |
57 | | - QStringList arguments; |
58 | | - arguments << "tombll_manage_data.py" << "-sc"; |
59 | | - run(arguments); |
60 | | - return getStatus(); |
| 97 | +int64_t PyRunner::syncCards(int64_t lid) { |
| 98 | + run("tombll_manage_data", {"tombll_manage_data.py", "-sc"}); |
| 99 | + return m_status; |
61 | 100 | } |
62 | 101 |
|
63 | | -qint64 PyRunner::getStatus() { |
| 102 | +int64_t PyRunner::getStatus() const { |
64 | 103 | return m_status; |
65 | 104 | } |
| 105 | + |
0 commit comments