| 108 | } |
| 109 | |
| 110 | void PythonDebugger::initialize(const QString &pythonExecute, |
| 111 | const QString &fileName, |
| 112 | const QString &projectCachePath) |
| 113 | { |
| 114 | int startPort = 7000; |
| 115 | auto checkPortFree = [](int port) { |
| 116 | QProcess process; |
| 117 | QString cmd = QString("fuser %1/tcp").arg(port); |
| 118 | process.start(cmd); |
| 119 | process.waitForFinished(); |
| 120 | QString ret = process.readAll(); |
| 121 | if (ret.isEmpty()) |
| 122 | return true; |
| 123 | return false; |
| 124 | }; |
| 125 | |
| 126 | while (startPort) { |
| 127 | if (checkPortFree(startPort)) { |
| 128 | break; |
| 129 | } |
| 130 | startPort--; |
| 131 | } |
| 132 | |
| 133 | d->port = startPort; |
| 134 | QString validPort = QString::number(d->port); |
| 135 | QString pid = QString::number(QApplication::applicationPid()); |
| 136 | QString logFolder = projectCachePath + "/dap/pylog"; |
| 137 | QString param = pythonExecute + " -m debugpy --listen " + validPort + |
| 138 | " --wait-for-client --log-to " + logFolder + " " + fileName + " --pid " + pid; |
| 139 | |
| 140 | QStringList options; |
| 141 | options << "-c" << param; |
| 142 | auto env = d->process.processEnvironment(); |
| 143 | env.insert("PYTHONPATH", packageInstallPath(pythonExecute)); |
| 144 | d->process.setProcessEnvironment(env); |
| 145 | d->process.start("/bin/bash", options); |
| 146 | d->process.waitForStarted(); |
| 147 | QThread::msleep(500); // The port may not start listening immediately when Python starts, resulting in the IDE being unable to connect. Wait for 500ms. |
| 148 | } |
| 149 | |
| 150 | void PythonDebugger::slotReceiveClientInfo(const QString &ppid, |
| 151 | const QString &kit, |
nothing calls this directly
no test coverage detected