| 1 |
# /****************************************************************************** |
| 2 |
# * Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
# * * |
| 4 |
# * This file is part of the FreeCAD CAx development system. * |
| 5 |
# * * |
| 6 |
# * This library is free software; you can redistribute it and/or * |
| 7 |
# * modify it under the terms of the GNU Library General Public * |
| 8 |
# * License as published by the Free Software Foundation; either * |
| 9 |
# * version 2 of the License, or (at your option) any later version. * |
| 10 |
# * * |
| 11 |
# * This library is distributed in the hope that it will be useful, * |
| 12 |
# * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
# * GNU Library General Public License for more details. * |
| 15 |
# * * |
| 16 |
# * You should have received a copy of the GNU Library General Public * |
| 17 |
# * License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
# * write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
# * Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
# * * |
| 21 |
# ******************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
import FreeCAD as App |
| 25 |
import FreeCADGui as Gui |
| 26 |
from PySide import QtGui |
| 27 |
from freecad.utils import get_python_exe |
| 28 |
|
| 29 |
|
| 30 |
class RemoteDebugger: |
| 31 |
def __init__(self, parent=None): |
| 32 |
ui = App.getHomePath() + "Ext/freecad/gui/RemoteDebugger.ui" |
| 33 |
self.dialog = Gui.PySideUic.loadUi(ui) |
| 34 |
self.dialog.buttonBox.accepted.connect(self.accept) |
| 35 |
self.dialog.buttonBox.rejected.connect(self.reject) |
| 36 |
|
| 37 |
self.prefs = App.ParamGet("User parameter:BaseApp/Macro/Debugger") |
| 38 |
index = self.prefs.GetInt("TabIndex", 0) |
| 39 |
self.dialog.tabWidget.setCurrentIndex(index) |
| 40 |
address = self.prefs.GetString("VSCodeAddress", "localhost") |
| 41 |
port = self.prefs.GetInt("VSCodePort", 5678) |
| 42 |
self.dialog.lineEditAddress.setText(address) |
| 43 |
self.dialog.spinBoxPort.setValue(port) |
| 44 |
|
| 45 |
def accept(self): |
| 46 |
try: |
| 47 |
index = self.dialog.tabWidget.currentIndex() |
| 48 |
self.prefs.SetInt("TabIndex", index) |
| 49 |
|
| 50 |
if index == 0: # winpdb |
| 51 |
passwd = self.dialog.lineEditPassword.text() |
| 52 |
|
| 53 |
import rpdb2 |
| 54 |
|
| 55 |
rpdb2.start_embedded_debugger(passwd, timeout=30) |
| 56 |
|
| 57 |
elif index == 1: # VS code |
| 58 |
address = self.dialog.lineEditAddress.text() |
| 59 |
port = self.dialog.spinBoxPort.value() |
| 60 |
self.prefs.SetString("VSCodeAddress", address) |
| 61 |
self.prefs.SetInt("VSCodePort", port) |
| 62 |
|
| 63 |
import debugpy |
| 64 |
|
| 65 |
# get_python_exe is needed because debugpy needs a python interpreter to work. |
| 66 |
# It does not have to be FC embedded interpreter. |
| 67 |
# By default it attempts to use Freecad's PID mistaking it for python. |
| 68 |
# https://github.com/microsoft/debugpy/issues/262 |
| 69 |
debugpy.configure(python=get_python_exe()) |
| 70 |
debugpy.listen((address, port)) |
| 71 |
debugpy.wait_for_client() |
| 72 |
|
| 73 |
except Exception as e: |
| 74 |
QtGui.QMessageBox.warning(self.dialog, "Failed to attach", str(e)) |
| 75 |
|
| 76 |
self.dialog.accept() |
| 77 |
|
| 78 |
def reject(self): |
| 79 |
self.dialog.reject() |
| 80 |
|
| 81 |
def exec_(self): |
| 82 |
self.dialog.exec_() |
| 83 |
|
| 84 |
|
| 85 |
def attachToRemoteDebugger(): |
| 86 |
dlg = RemoteDebugger(Gui.getMainWindow()) |
| 87 |
dlg.exec_() |